class CurrencyChangeInventoryCallback extends InventoryScriptCallback
{
	var m_notificationQueue : weak< ItemsNotificationQueue >;

	public export override function OnItemQuantityChanged( item : ItemID, diff : Int32, total : Uint32, flaggedAsSilent : Bool )
	{
		if( ItemID.IsOfTDBID( item, T"Items.money" ) && !( flaggedAsSilent ) )
		{
			m_notificationQueue.PushCurrencyNotification( diff, total );
		}
	}

}

import class CurrencyUpdateNotificationViewData extends GenericNotificationViewData
{
	import var diff : Int32;
	import var total : Uint32;

	public export override function CanMerge( data : GenericNotificationViewData ) : Bool
	{
		var compareTo : CurrencyUpdateNotificationViewData;
		compareTo = ( ( CurrencyUpdateNotificationViewData )( data ) );
		if( compareTo )
		{
			total = compareTo.total;
			diff = diff + compareTo.diff;
			return true;
		}
		return false;
	}

}

enum CurrencyNotificationAnimState
{
	Inactive = 0,
	Intro = 1,
	Active = 2,
	Outro = 3,
}

class CurrencyNotification extends GenericNotificationController
{
	private editable var m_CurrencyUpdateAnimation : CName;
	private editable var m_CurrencyDiff : inkTextRef;
	private editable var m_CurrencyTotal : inkTextRef;
	private editable var m_total_animator : weak< inkTextValueProgressController >;
	private var m_currencyData : CurrencyUpdateNotificationViewData;
	private var m_animProxy : inkAnimProxy;
	private var m_animState : CurrencyNotificationAnimState;
	default m_animState = CurrencyNotificationAnimState.Inactive;
	private var blackboard : weak< IBlackboard >;
	private var uiSystemBB : UI_SystemDef;
	private var uiSystemId : CallbackHandle;

	protected event OnInitialize()
	{
		super.OnInitialize();
		blackboard = GetBlackboardSystem().Get( GetAllBlackboardDefs().UI_System );
		uiSystemBB = GetAllBlackboardDefs().UI_System;
		uiSystemId = blackboard.RegisterListenerBool( uiSystemBB.IsInMenu, this, 'OnMenuUpdate' );
		RegisterToCallback( 'OnItemChanged', this, 'OnDataUpdate' );
	}

	protected event OnUninitialize()
	{
		super.OnUninitialize();
		blackboard.UnregisterListenerBool( uiSystemBB.IsInMenu, uiSystemId );
	}

	protected event OnDataUpdate()
	{
		UpdateData();
	}

	protected event OnMenuUpdate( value : Bool )
	{
		UpdateData();
	}

	public export override function SetNotificationData( notificationData : GenericNotificationViewData )
	{
		m_currencyData = ( ( CurrencyUpdateNotificationViewData )( notificationData ) );
		UpdateData();
		super.SetNotificationData( notificationData );
	}

	private function UpdateData()
	{
		var animOptions : inkAnimOptions;
		inkTextRef.SetText( m_CurrencyDiff, ( ( String )( m_currencyData.diff ) ) );
		inkTextRef.SetText( m_CurrencyTotal, ( ( String )( m_currencyData.total ) ) );
		if( m_animState == CurrencyNotificationAnimState.Intro )
		{
			return;
		}
		m_total_animator = ( ( inkTextValueProgressController )( inkTextRef.GetController( m_CurrencyTotal ) ) );
		m_total_animator.SetDelay( 3.25 );
		m_total_animator.SetDuration( 1.0 );
		m_total_animator.SetBaseValue( ( ( Float )( m_currencyData.total ) ) - ( ( Float )( m_currencyData.diff ) ) );
		m_total_animator.SetTargetValue( ( ( Float )( m_currencyData.total ) ) );
		m_total_animator.PlaySetAnimation().RegisterToCallback( inkanimEventType.OnFinish, this, 'OnMainAnimationOver' );
		if( m_animState == CurrencyNotificationAnimState.Inactive )
		{
			animOptions.toMarker = 'intro_end';
			m_animProxy = PlayLibraryAnimation( m_CurrencyUpdateAnimation, animOptions );
			m_animProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnIntoOver' );
			m_animState = CurrencyNotificationAnimState.Intro;
		}
		else
		{
			m_animProxy.Stop();
			PlayActiveAnim();
		}
	}

	private function PlayActiveAnim()
	{
		var animOptions : inkAnimOptions;
		animOptions.fromMarker = 'intro_end';
		animOptions.toMarker = 'outro_start';
		m_animProxy = PlayLibraryAnimation( m_CurrencyUpdateAnimation, animOptions );
		m_animState = CurrencyNotificationAnimState.Active;
	}

	protected event OnIntoOver( e : inkAnimProxy )
	{
		PlayActiveAnim();
	}

	protected event OnOutroOver( e : inkAnimProxy )
	{
		m_animState = CurrencyNotificationAnimState.Inactive;
		m_animProxy = NULL;
		m_total_animator = NULL;
	}

	protected event OnMainAnimationOver( e : inkAnimProxy )
	{
		var animOptions : inkAnimOptions;
		m_animProxy.Stop();
		animOptions.fromMarker = 'outro_start';
		m_animProxy = PlayLibraryAnimation( m_CurrencyUpdateAnimation, animOptions );
		m_animProxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnOutroOver' );
		m_animState = CurrencyNotificationAnimState.Outro;
	}

}

