class CraftingSkillWidget extends inkGameController
{
	protected editable var m_amountText : inkTextRef;
	protected editable var m_expFill : inkWidgetRef;
	protected editable var m_perkHolder : inkWidgetRef;
	protected editable var m_levelUpAnimation : inkWidgetRef;
	protected editable var m_expAnimation : inkWidgetRef;
	protected editable var m_nextLevelText : inkTextRef;
	protected editable var m_expPointText1 : inkTextRef;
	protected editable var m_expPointText2 : inkTextRef;
	private var m_levelUpBlackboard : weak< IBlackboard >;
	private var m_playerLevelUpListener : CallbackHandle;
	private var m_isLevelUp : Bool;
	private var m_currentExp : Int32;

	protected event OnInitialize()
	{
		m_levelUpBlackboard = GetBlackboardSystem().Get( GetAllBlackboardDefs().UI_LevelUp );
		m_playerLevelUpListener = m_levelUpBlackboard.RegisterListenerVariant( GetAllBlackboardDefs().UI_LevelUp.level, this, 'OnCharacterLevelUpdated' );
		SetProgress();
		SetLevel();
	}

	protected event OnCharacterProficiencyUpdated( evt : ProficiencyProgressEvent )
	{
		if( evt.type == gamedataProficiencyType.TechnicalAbilitySkill && !( m_isLevelUp ) )
		{
			SetProgress();
			inkWidgetRef.SetVisible( m_expAnimation, true );
			PlayLibraryAnimation( 'exp_points_gain' );
		}
		m_isLevelUp = false;
	}

	protected event OnCharacterLevelUpdated( value : Variant )
	{
		var levelUpData : LevelUpData;
		levelUpData = ( ( LevelUpData )value );
		if( levelUpData.type == gamedataProficiencyType.TechnicalAbilitySkill )
		{
			m_isLevelUp = true;
			SetLevel();
			inkWidgetRef.SetVisible( m_levelUpAnimation, true );
			PlayLibraryAnimation( 'SKILLBAR_lvlup' );
			SetProgress();
		}
	}

	private function SetLevel()
	{
		var playerDevelopmentSystem : weak< PlayerDevelopmentSystem >;
		var puppet : weak< PlayerPuppet >;
		puppet = ( ( PlayerPuppet )( GetPlayerControlledObject() ) );
		playerDevelopmentSystem = ( ( PlayerDevelopmentSystem )( GameInstance.GetScriptableSystemsContainer( ( ( GameObject )( GetOwnerEntity() ) ).GetGame() ).Get( 'PlayerDevelopmentSystem' ) ) );
		inkTextRef.SetText( m_nextLevelText, IntToString( playerDevelopmentSystem.GetProficiencyLevel( puppet, gamedataProficiencyType.TechnicalAbilitySkill ) ) );
		inkTextRef.SetText( m_amountText, IntToString( playerDevelopmentSystem.GetProficiencyLevel( puppet, gamedataProficiencyType.TechnicalAbilitySkill ) ) );
	}

	private function SetProgress()
	{
		var currExp : Int32;
		var addExp : Int32;
		var remainingExp : Int32;
		var totalExp : Int32;
		var currentFill : Float;
		var playerDevelopmentSystem : weak< PlayerDevelopmentSystem >;
		var puppet : weak< PlayerPuppet >;
		var expParams : inkTextParams;
		puppet = ( ( PlayerPuppet )( GetPlayerControlledObject() ) );
		playerDevelopmentSystem = ( ( PlayerDevelopmentSystem )( GameInstance.GetScriptableSystemsContainer( ( ( GameObject )( GetOwnerEntity() ) ).GetGame() ).Get( 'PlayerDevelopmentSystem' ) ) );
		currExp = playerDevelopmentSystem.GetCurrentLevelProficiencyExp( puppet, gamedataProficiencyType.TechnicalAbilitySkill );
		addExp = currExp - m_currentExp;
		m_currentExp = currExp;
		remainingExp = playerDevelopmentSystem.GetRemainingExpForLevelUp( puppet, gamedataProficiencyType.TechnicalAbilitySkill );
		expParams = new inkTextParams;
		expParams.AddNumber( "VALUE", addExp );
		( ( inkText )( inkTextRef.Get( m_expPointText1 ) ) ).SetLocalizedTextScript( "LocKey#42794", expParams );
		( ( inkText )( inkTextRef.Get( m_expPointText2 ) ) ).SetLocalizedTextScript( "LocKey#42794", expParams );
		m_currentExp = currExp;
		totalExp = currExp + remainingExp;
		currentFill = ( ( Float )( currExp ) ) / ( ( Float )( totalExp ) );
		if( playerDevelopmentSystem.IsProficiencyMaxLvl( puppet, gamedataProficiencyType.TechnicalAbilitySkill ) )
		{
			currentFill = 1.0;
		}
		SetFill( currentFill );
	}

	private function SetFill( amount : Float )
	{
		inkWidgetRef.SetScale( m_expFill, Vector2( amount, 1.0 ) );
	}

	private function AddPerk( toAdd : gamedataPerkType )
	{
		var imageWidget : weak< inkImage >;
		imageWidget = ( ( inkImage )( ( ( inkCompoundWidget )( SpawnFromLocal( inkWidgetRef.Get( m_perkHolder ), 'perkImage' ) ) ).GetWidgetByIndex( 0 ) ) );
		InkImageUtils.RequestSetImage( this, imageWidget, "UIIcon." + ( ( String )( toAdd ) ) );
	}

	protected event OnUninitialize()
	{
		if( m_levelUpBlackboard )
		{
			m_levelUpBlackboard.UnregisterListenerVariant( GetAllBlackboardDefs().UI_LevelUp.level, m_playerLevelUpListener );
		}
	}

}

