class NewPerksAttributeButtonController extends inkLogicController
{
	private editable var m_attributePointsButton : inkWidgetRef;
	private editable var m_attributeText : inkTextRef;
	private editable var m_currentText : inkTextRef;
	private editable var m_textGhost : inkTextRef;
	private editable var m_requirementText : inkTextRef;
	private editable var m_buttonWidget : inkWidgetRef;
	private var m_buttonHintsController : weak< ButtonHints >;
	private var m_totalPoints : Int32;
	private var m_initData : NewPerksScreenInitData;
	private var m_isHovered : Bool;
	default m_isHovered = false;
	private var m_isPressed : Bool;
	default m_isPressed = false;
	private var idleAnimProxy : inkAnimProxy;

	protected event OnInitialize()
	{
		inkWidgetRef.RegisterToCallback( m_attributePointsButton, 'OnPress', this, 'OnAttributeInvestPress' );
		inkWidgetRef.RegisterToCallback( m_attributePointsButton, 'OnHold', this, 'OnAttributeInvestHold' );
		inkWidgetRef.RegisterToCallback( m_attributePointsButton, 'OnRelease', this, 'OnAttributeInvestRelease' );
		inkWidgetRef.RegisterToCallback( m_attributePointsButton, 'OnHoverOver', this, 'OnAttributeInvestHoverOver' );
		inkWidgetRef.RegisterToCallback( m_attributePointsButton, 'OnHoverOut', this, 'OnAttributeInvestHoverOut' );
	}

	protected event OnUninitialize()
	{
		inkWidgetRef.UnregisterFromCallback( m_attributePointsButton, 'OnPress', this, 'OnAttributeInvestPress' );
		inkWidgetRef.UnregisterFromCallback( m_attributePointsButton, 'OnHold', this, 'OnAttributeInvestHold' );
		inkWidgetRef.UnregisterFromCallback( m_attributePointsButton, 'OnRelease', this, 'OnAttributeInvestRelease' );
		inkWidgetRef.UnregisterFromCallback( m_attributePointsButton, 'OnHoverOver', this, 'OnAttributeInvestHoverOver' );
		inkWidgetRef.UnregisterFromCallback( m_attributePointsButton, 'OnHoverOut', this, 'OnAttributeInvestHoverOut' );
	}

	public function SetData( initData : NewPerksScreenInitData, buttonHintsController : weak< ButtonHints > )
	{
		m_initData = initData;
		m_buttonHintsController = buttonHintsController;
		inkTextRef.SetText( m_attributeText, GetLocalizedText( PerkAttributeHelper.GetShortNameLocKey( m_initData.perkMenuAttribute ) ) );
	}

	public function PlayIdleAnimation()
	{
		var playbackOptions : inkAnimOptions;
		StopIdleAnimation();
		if( ( m_isHovered || ( m_totalPoints <= 0 ) ) || !( inkWidgetRef.IsInteractive( m_buttonWidget ) ) )
		{
			return;
		}
		playbackOptions.executionDelay = 1.0;
		playbackOptions.loopType = inkanimLoopType.Cycle;
		playbackOptions.loopInfinite = true;
		idleAnimProxy = PlayLibraryAnimation( 'tier_button_anim_idle', playbackOptions );
	}

	public function StopIdleAnimation()
	{
		if( idleAnimProxy && idleAnimProxy.IsPlaying() )
		{
			idleAnimProxy.GotoEndAndStop();
		}
	}

	public function SetValues( currentPoints : Int32, requiredPoints : Int32, totalPoints : Int32 )
	{
		inkTextRef.SetText( m_currentText, IntToString( currentPoints ) );
		inkTextRef.SetText( m_requirementText, IntToString( requiredPoints ) );
		inkTextRef.SetText( m_textGhost, IntToString( currentPoints ) );
		m_totalPoints = totalPoints;
		UpdateState();
	}

	public function SetInteractive( value : Bool )
	{
		inkWidgetRef.SetInteractive( m_buttonWidget, value );
		if( !( value ) )
		{
			m_buttonHintsController.RemoveButtonHint( 'upgrade_attribute_button' );
		}
	}

	private function UpdateState()
	{
		if( m_totalPoints > 0 )
		{
			PlayIdleAnimation();
			inkWidgetRef.SetState( m_attributePointsButton, ( ( m_isHovered ) ? ( 'Hover' ) : ( 'Default' ) ) );
		}
		else
		{
			StopIdleAnimation();
			inkWidgetRef.SetState( m_attributePointsButton, ( ( m_isHovered ) ? ( 'DisabledHover' ) : ( 'Disabled' ) ) );
		}
	}

	private function UpdateCursorData()
	{
		var cursorData : MenuCursorUserData;
		if( m_isPressed && ( m_totalPoints > 0 ) )
		{
			cursorData = new MenuCursorUserData;
			cursorData.SetAnimationOverride( 'hoverOnHoldToComplete' );
			cursorData.AddAction( 'upgrade_attribute_button' );
			SetCursorContext( 'Hover', cursorData );
			PlayRumbleLoop( RumbleStrength.SuperLight );
		}
		else
		{
			SetCursorContext( 'Hover' );
			StopRumbleLoop( RumbleStrength.SuperLight );
		}
	}

	protected event OnAttributeInvestPress( evt : inkPointerEvent )
	{
		if( evt.IsAction( 'upgrade_attribute_button' ) )
		{
			m_isPressed = true;
			UpdateCursorData();
			if( m_totalPoints > 0 )
			{
				PlaySound( 'Attributes', 'OnStart' );
				inkWidgetRef.SetState( m_attributePointsButton, 'Press' );
			}
			else
			{
				PlaySound( 'Attributes', 'OnFail' );
				PlayLibraryAnimation( 'tier_button_anim_locked' );
			}
		}
	}

	protected event OnAttributeInvestHold( evt : inkPointerEvent )
	{
		var progress : Float;
		var holdFinishedEvent : NewPerksTabAttributeInvestHoldFinished;
		if( evt.IsAction( 'upgrade_attribute_button' ) )
		{
			if( !( m_isPressed ) )
			{
				m_isPressed = true;
				UpdateCursorData();
			}
			progress = MinF( evt.GetHoldProgress(), 1.0 );
			if( progress >= 1.0 )
			{
				holdFinishedEvent = new NewPerksTabAttributeInvestHoldFinished;
				holdFinishedEvent.attribute = m_initData.perkMenuAttribute;
				QueueEvent( holdFinishedEvent );
				m_isPressed = false;
				UpdateCursorData();
				UpdateState();
				if( m_totalPoints > 0 )
				{
					PlayLibraryAnimation( 'tier_button_anim_hoverClick' );
				}
			}
		}
	}

	protected event OnAttributeInvestRelease( evt : inkPointerEvent )
	{
		if( evt.IsAction( 'upgrade_attribute_button' ) )
		{
			m_isPressed = false;
			UpdateCursorData();
			UpdateState();
		}
	}

	protected event OnAttributeInvestHoverOver( evt : inkPointerEvent )
	{
		m_isHovered = true;
		UpdateState();
		UpdateCursorData();
		StopIdleAnimation();
		if( m_totalPoints > 0 )
		{
			m_buttonHintsController.AddButtonHint( 'upgrade_attribute_button', GetLocalizedText( "LocKey#49715" ) );
		}
	}

	protected event OnAttributeInvestHoverOut( evt : inkPointerEvent )
	{
		HoverOut();
	}

	public function HoverOut()
	{
		m_isHovered = false;
		m_isPressed = false;
		UpdateState();
		UpdateCursorData();
		PlayIdleAnimation();
		m_buttonHintsController.RemoveButtonHint( 'upgrade_attribute_button' );
	}

}

