enum AttributeButtonState
{
	Default = 0,
	Hover = 1,
}

class CharacterCreationAttributeData extends IScriptable
{
	var label : String;
	var desc : String;
	var value : Int32;
	var attribute : gamedataStatType;
	var icon : CName;
	var maxValue : Int32;
	var minValue : Int32;
	var maxed : Bool;
	var atMinimum : Bool;

	public function SetValue( val : Int32 )
	{
		value = val;
	}

	public function SetMaxed( val : Bool )
	{
		maxed = val;
	}

	public function SetAtMinimum( val : Bool )
	{
		atMinimum = val;
	}

}

class characterCreationStatsAttributeBtn extends inkLogicController
{
	editable var m_value : inkTextRef;
	editable var m_label : inkTextRef;
	editable var m_icon : inkImageRef;
	editable var m_buttons : inkImageRef;
	editable var m_selector : inkImageRef;
	editable var m_addBtnhitArea : inkWidgetRef;
	editable var m_minusBtnhitArea : inkWidgetRef;
	editable var m_minMaxLabel : inkWidgetRef;
	editable var m_minMaxLabelText : inkTextRef;
	var data : CharacterCreationAttributeData;
	var animating : Bool;
	var m_minusEnabled : Bool;
	var m_addEnabled : Bool;
	var m_maxed : Bool;
	private var m_isPlusOrMinusBtnHoveredOver : Bool;
	private var m_inputDisabled : Bool;

	protected event OnInitialize()
	{
		inkWidgetRef.RegisterToCallback( m_addBtnhitArea, 'OnRelease', this, 'OnAdd' );
		inkWidgetRef.RegisterToCallback( m_minusBtnhitArea, 'OnRelease', this, 'OnMinus' );
		inkWidgetRef.RegisterToCallback( m_addBtnhitArea, 'OnHoverOver', this, 'OnPlusHoverOver' );
		inkWidgetRef.RegisterToCallback( m_addBtnhitArea, 'OnHoverOut', this, 'OnPlusHoverOut' );
		inkWidgetRef.RegisterToCallback( m_minusBtnhitArea, 'OnHoverOver', this, 'OnMinusHoverOver' );
		inkWidgetRef.RegisterToCallback( m_minusBtnhitArea, 'OnHoverOut', this, 'OnMinusHoverOut' );
		GetRootWidget().RegisterToCallback( 'OnHoverOver', this, 'OnHitAreaOnHoverOver' );
		GetRootWidget().RegisterToCallback( 'OnHoverOut', this, 'OnHitAreaOnHoverOut' );
		m_addEnabled = true;
		m_minusEnabled = true;
		m_isPlusOrMinusBtnHoveredOver = false;
		m_inputDisabled = false;
	}

	protected event OnUninitialize()
	{
		inkWidgetRef.UnregisterFromCallback( m_addBtnhitArea, 'OnRelease', this, 'OnAdd' );
		inkWidgetRef.UnregisterFromCallback( m_minusBtnhitArea, 'OnRelease', this, 'OnMinus' );
		inkWidgetRef.UnregisterFromCallback( m_addBtnhitArea, 'OnHoverOver', this, 'OnHitAreaOnHoverOver' );
		inkWidgetRef.UnregisterFromCallback( m_addBtnhitArea, 'OnHoverOut', this, 'OnHitAreaOnHoverOut' );
		inkWidgetRef.UnregisterFromCallback( m_minusBtnhitArea, 'OnHoverOver', this, 'OnHitAreaOnHoverOver' );
		inkWidgetRef.UnregisterFromCallback( m_minusBtnhitArea, 'OnHoverOut', this, 'OnHitAreaOnHoverOut' );
		GetRootWidget().UnregisterFromCallback( 'OnHoverOver', this, 'OnHitAreaOnHoverOver' );
		GetRootWidget().UnregisterFromCallback( 'OnHoverOut', this, 'OnHitAreaOnHoverOut' );
	}

	public function SetInputDisabled( disable : Bool )
	{
		if( m_inputDisabled != disable )
		{
			m_isPlusOrMinusBtnHoveredOver = false;
			RefreshVisibility();
		}
		m_inputDisabled = disable;
	}

	public function Refresh()
	{
		inkTextRef.SetText( m_value, ( ( String )( data.value ) ) );
		animating = false;
		RefreshVisibility();
	}

	public function Increment()
	{
		if( animating )
		{
			return;
		}
		animating = true;
		CallCustomCallback( 'OnValueIncremented' );
	}

	public function Decrement()
	{
		if( animating )
		{
			return;
		}
		animating = true;
		CallCustomCallback( 'OnValueDecremented' );
	}

	public function SetData( attribute : gamedataStatType, value : Int32 )
	{
		var str : String;
		var record : UICharacterCreationAttribute_Record;
		var statsRecord : Stat_Record;
		data = new CharacterCreationAttributeData;
		str = EnumValueToString( "gamedataStatType", ( ( Int32 )( attribute ) ) );
		record = TweakDBInterface.GetUICharacterCreationAttributeRecord( TDBID.Create( "UICharacterCreationGeneral." + str ) );
		data.value = value;
		data.attribute = attribute;
		data.icon = record.IconPath();
		data.desc = record.Description();
		statsRecord = record.Attribute();
		data.label = statsRecord.LocalizedName();
		inkTextRef.SetText( m_label, data.label );
		switch( attribute )
		{
			case gamedataStatType.Intelligence:
				inkImageRef.SetTexturePart( m_icon, 'ico_int' );
			break;
			case gamedataStatType.Cool:
				inkImageRef.SetTexturePart( m_icon, 'ico_cool' );
			break;
			case gamedataStatType.Strength:
				inkImageRef.SetTexturePart( m_icon, 'ico_body' );
			break;
			case gamedataStatType.Reflexes:
				inkImageRef.SetTexturePart( m_icon, 'ico_ref' );
			break;
			case gamedataStatType.TechnicalAbility:
				inkImageRef.SetTexturePart( m_icon, 'ico_tech' );
			break;
		}
	}

	private function RefreshVisibility()
	{
		if( m_isPlusOrMinusBtnHoveredOver )
		{
			if( !( m_addEnabled ) )
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_locked_minus_idle' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks_left' );
			}
			else if( !( m_minusEnabled ) )
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_locked_plus_idle' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks_right' );
			}
			else
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_idle' );
			}
		}
		else
		{
			if( m_addEnabled && m_minusEnabled )
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_idle' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks' );
			}
			else if( !( m_addEnabled ) )
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_locked_minus_idle' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks' );
			}
			else
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_locked_plus_idle' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks' );
			}
		}
	}

	protected event OnMinus( e : inkPointerEvent )
	{
		if( !( m_inputDisabled ) )
		{
			if( e.IsAction( 'click' ) )
			{
				if( m_minusEnabled )
				{
					PlaySound( 'MapPin', 'OnEnable' );
					Decrement();
				}
				else
				{
					PlaySound( 'MapPin', 'OnDisable' );
				}
			}
		}
	}

	protected event OnAdd( e : inkPointerEvent )
	{
		if( !( m_inputDisabled ) )
		{
			if( e.IsAction( 'click' ) )
			{
				if( m_addEnabled )
				{
					PlaySound( 'MapPin', 'OnCreate' );
					Increment();
				}
				else
				{
					PlaySound( 'MapPin', 'OnDisable' );
				}
			}
		}
	}

	protected event OnHitAreaOnHoverOver( e : inkPointerEvent )
	{
		if( !( m_inputDisabled ) )
		{
			GetRootWidget().SetState( 'SemiHover' );
			CallCustomCallback( 'OnBtnHoverOver' );
		}
	}

	protected event OnHitAreaOnHoverOut( e : inkPointerEvent )
	{
		if( !( m_inputDisabled ) )
		{
			GetRootWidget().SetState( 'Default' );
			CallCustomCallback( 'OnBtnHoverOut' );
		}
	}

	protected event OnPlusHoverOver( e : inkPointerEvent )
	{
		if( !( m_inputDisabled ) )
		{
			if( m_minusEnabled && m_addEnabled )
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_hover_plus' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks_right' );
			}
			else if( !( m_addEnabled ) )
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_locked_minus_hover' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks_right' );
			}
			else if( !( m_minusEnabled ) )
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_locked_plus_hover' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks_right' );
			}
			m_isPlusOrMinusBtnHoveredOver = true;
		}
	}

	protected event OnPlusHoverOut( e : inkPointerEvent )
	{
		if( !( m_inputDisabled ) )
		{
			m_isPlusOrMinusBtnHoveredOver = false;
			RefreshVisibility();
		}
	}

	protected event OnMinusHoverOver( e : inkPointerEvent )
	{
		if( !( m_inputDisabled ) )
		{
			if( m_minusEnabled && m_addEnabled )
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_hover_minus' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks_left' );
			}
			else if( !( m_minusEnabled ) )
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_locked_plus_hover' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks_left' );
			}
			else if( !( m_addEnabled ) )
			{
				inkImageRef.SetTexturePart( m_buttons, 'points_locked_minus_hover' );
				inkImageRef.SetTexturePart( m_selector, 'cell_perks_left' );
			}
			m_isPlusOrMinusBtnHoveredOver = true;
		}
	}

	protected event OnMinusHoverOut( e : inkPointerEvent )
	{
		if( !( m_inputDisabled ) )
		{
			m_isPlusOrMinusBtnHoveredOver = false;
			RefreshVisibility();
		}
	}

	public function ManageBtnVisibility( addEnabled : Bool, minusEnabled : Bool )
	{
		m_addEnabled = addEnabled;
		m_minusEnabled = minusEnabled;
		RefreshVisibility();
	}

	public function ManageLabel( atMin : Bool, atMax : Bool )
	{
		inkWidgetRef.SetVisible( m_minMaxLabel, atMin || atMax );
		if( atMax )
		{
			inkTextRef.SetText( m_minMaxLabelText, "LocKey#42807" );
		}
		else if( atMin )
		{
			inkTextRef.SetText( m_minMaxLabelText, "LocKey#42808" );
		}
	}

}

