class PhoneContactHiddenEvent extends Event
{
	var HiddenItem : weak< inkVirtualCompoundItemController >;
}

class FakePhoneContactItemVirtualController extends PhoneContactItemVirtualController
{
	private editable var m_dots : inkWidgetRef;

	protected event OnDataChanged( value : Variant )
	{
		m_contactData = ( ( ContactData )( ( ( IScriptable )value ) ) );
		inkTextRef.SetText( m_label, m_contactData.localizedName );
		inkTextRef.SetVisible( m_label, m_contactData.localizedName != "" );
		inkTextRef.SetFontStyle( m_label, 'Regular' );
		inkWidgetRef.SetVisible( m_dots, m_contactData.localizedName == "" );
		if( m_animProxySelection )
		{
			m_animProxySelection.GotoStartAndStop( true );
			m_animProxySelection = NULL;
		}
		m_pulse.Stop();
		m_root.SetState( 'Disabled' );
	}

}

class PhoneContactItemVirtualController extends inkVirtualCompoundItemController
{
	protected editable var m_label : inkTextRef;
	private editable var m_preview : inkTextRef;
	private editable var m_msgCount : inkTextRef;
	private editable var m_msgIndicator : inkWidgetRef;
	private editable var m_questFlag : inkWidgetRef;
	private editable var m_regFlag : inkWidgetRef;
	private editable var m_replyAlertIcon : inkWidgetRef;
	[ category = "INPUT HINTS" ]
	private editable var m_callInputHint : inkWidgetRef;
	[ category = "INPUT HINTS" ]
	private editable var m_chatInputHint : inkWidgetRef;
	[ category = "INPUT HINTS" ]
	private editable var m_separtor : inkWidgetRef;
	protected var m_animProxySelection : inkAnimProxy;
	private var m_animProxyHide : inkAnimProxy;
	protected var m_contactData : ContactData;
	protected var m_pulse : PulseAnimation;
	private var isQuestImportant : Bool;
	private var isUnread : Bool;
	private var m_isCallingEnabled : Bool;
	protected var m_root : weak< inkWidget >;

	public function GetContactData() : ContactData
	{
		return m_contactData;
	}

	protected event OnInitialize()
	{
		RegisterToCallback( 'OnSelected', this, 'OnSelected' );
		RegisterToCallback( 'OnDeselected', this, 'OnDeselected' );
		m_pulse = new PulseAnimation;
		m_pulse.Configure( inkWidgetRef.Get( m_questFlag ), 1.0, 0.2, 0.60000002 );
		m_root = GetRootWidget();
	}

	protected event OnDataChanged( value : Variant )
	{
		m_contactData = ( ( ContactData )( ( ( IScriptable )value ) ) );
		inkTextRef.SetText( m_label, m_contactData.localizedName );
		if( m_contactData.hasValidTitle )
		{
			inkTextRef.SetText( m_preview, m_contactData.localizedPreview );
		}
		else
		{
			if( m_contactData.playerIsLastSender )
			{
				inkTextRef.SetText( m_preview, ( GetLocalizedTextByKey( 'UI-Phone-LabelYou' ) + ": " ) + GetLocalizedText( m_contactData.lastMesssagePreview ) );
			}
			else
			{
				inkTextRef.SetText( m_preview, m_contactData.lastMesssagePreview );
			}
		}
		SetTimeText( m_contactData.timeStamp );
		inkWidgetRef.SetVisible( m_replyAlertIcon, m_contactData.playerCanReply );
		inkTextRef.SetVisible( m_msgCount, !( m_contactData.playerCanReply ) );
		if( m_animProxySelection )
		{
			m_animProxySelection.GotoStartAndStop( true );
			m_animProxySelection = NULL;
		}
		m_pulse.Stop();
		if( m_contactData.questRelated || m_contactData.hasQuestImportantReply )
		{
			inkWidgetRef.SetVisible( m_questFlag, true );
			m_pulse.Start();
			isQuestImportant = true;
		}
		else
		{
			inkWidgetRef.SetVisible( m_questFlag, false );
			isQuestImportant = false;
		}
		inkWidgetRef.SetVisible( m_regFlag, !( isQuestImportant ) );
		m_root.SetState( 'Default' );
		inkTextRef.SetFontStyle( m_label, 'Medium' );
		if( m_contactData.type != MessengerContactType.Contact )
		{
			if( m_contactData.unreadMessages.Size() > 0 )
			{
				isUnread = true;
				inkWidgetRef.SetVisible( m_msgIndicator, true );
				inkTextRef.SetFontStyle( m_preview, 'Medium' );
				inkTextRef.SetFontStyle( m_label, 'Semi-Bold' );
			}
			else
			{
				isUnread = false;
				inkWidgetRef.SetVisible( m_msgIndicator, false );
				m_root.SetState( 'Disabled' );
				inkTextRef.SetFontStyle( m_preview, 'Regular' );
				inkTextRef.SetFontStyle( m_label, 'Regular' );
			}
			if( m_contactData.playerCanReply )
			{
				m_root.SetState( 'Reply' );
			}
		}
		if( m_contactData.type == MessengerContactType.Contact && !( m_contactData.isCallable ) )
		{
			m_root.SetState( 'Disabled' );
		}
		if( isQuestImportant )
		{
			m_root.SetState( 'Quest' );
		}
		inkWidgetRef.SetVisible( m_callInputHint, m_contactData.isCallable );
		inkWidgetRef.SetVisible( m_chatInputHint, m_contactData.hasMessages || m_contactData.playerCanReply );
		inkWidgetRef.SetVisible( m_separtor, inkWidgetRef.IsVisible( m_chatInputHint ) && inkWidgetRef.IsVisible( m_callInputHint ) );
	}

	public function Hide()
	{
		if( m_animProxyHide != NULL )
		{
			m_animProxyHide.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
			m_animProxyHide.GotoStartAndStop( true );
			m_animProxyHide = NULL;
		}
		PlayScaleToRemoveAnimation();
		m_animProxyHide.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnHideAnimFinished' );
	}

	public function SetCallingEnabled( enabled : Bool )
	{
		m_isCallingEnabled = enabled;
	}

	public function Refresh( select : Bool )
	{
		OnDataChanged( m_contactData );
		if( select )
		{
			OnSelected( NULL, false );
		}
	}

	protected event OnHideAnimFinished( proxy : inkAnimProxy )
	{
		var evt : PhoneContactHiddenEvent;
		m_animProxyHide.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
		m_animProxyHide.GotoStartAndStop( true );
		m_animProxyHide = NULL;
		evt = new PhoneContactHiddenEvent;
		evt.HiddenItem = this;
		QueueEvent( evt );
		GetRootWidget().SetVisible( false );
		GetRootWidget().SetScale( Vector2( 1.0, 1.0 ) );
	}

	private function SetTimeText( time : GameTime )
	{
		var textParams : inkTextParams;
		textParams = new inkTextParams;
		inkTextRef.SetText( m_msgCount, "{TIME,time,hh:mm}" );
		textParams.AddTime( "TIME", time );
		inkTextRef.SetTextParameters( m_msgCount, textParams );
	}

	protected event OnSelected( itemController : weak< inkVirtualCompoundItemController >, discreteNav : Bool )
	{
		var animOptions : inkAnimOptions;
		if( m_animProxySelection )
		{
			m_animProxySelection.GotoStartAndStop( true );
			m_animProxySelection = NULL;
		}
		animOptions.loopInfinite = true;
		animOptions.loopType = inkanimLoopType.Cycle;
		m_animProxySelection = PlayLibraryAnimation( 'loopSelected', animOptions );
		m_root.SetState( 'Active' );
		if( m_contactData.type != MessengerContactType.Contact )
		{
			if( !( isUnread ) )
			{
				m_root.SetState( 'DisabledActive' );
			}
			if( m_contactData.playerCanReply )
			{
				m_root.SetState( 'ReplyActive' );
			}
		}
		if( m_contactData.type == MessengerContactType.Contact && !( m_contactData.isCallable ) )
		{
			m_root.SetState( 'DisabledActive' );
		}
		if( isQuestImportant )
		{
			m_root.SetState( 'QuestActive' );
		}
		inkWidgetRef.SetOpacity( m_callInputHint, ( ( m_isCallingEnabled ) ? ( 1.0 ) : ( 0.30000001 ) ) );
	}

	protected event OnDeselected( itemController : weak< inkVirtualCompoundItemController > )
	{
		if( m_animProxySelection )
		{
			m_animProxySelection.GotoStartAndStop( true );
			m_animProxySelection = NULL;
		}
		m_root.SetState( 'Default' );
		if( m_contactData.type != MessengerContactType.Contact )
		{
			if( !( isUnread ) )
			{
				m_root.SetState( 'Disabled' );
			}
			if( m_contactData.playerCanReply )
			{
				m_root.SetState( 'Reply' );
			}
		}
		if( m_contactData.type == MessengerContactType.Contact && !( m_contactData.isCallable ) )
		{
			m_root.SetState( 'Disabled' );
		}
		if( isQuestImportant )
		{
			m_root.SetState( 'Quest' );
		}
	}

	public function OpenInChat()
	{
		if( m_animProxySelection )
		{
			m_animProxySelection.GotoStartAndStop( true );
			m_animProxySelection = NULL;
		}
		m_animProxySelection = PlayLibraryAnimation( 'contactSelected' );
	}

	public function PlayScaleToRemoveAnimation()
	{
		var animDef : inkAnimDef;
		var animData : inkAnimScale;
		if( m_animProxyHide != NULL )
		{
			m_animProxyHide.Stop();
		}
		animData = new inkAnimScale;
		animData.SetDuration( 0.2 );
		animData.SetStartDelay( 0.25 );
		animData.SetStartScale( Vector2( 1.0, 1.0 ) );
		animData.SetEndScale( Vector2( 1.0, 0.0 ) );
		animData.SetType( inkanimInterpolationType.Quintic );
		animData.SetMode( inkanimInterpolationMode.EasyOut );
		animDef = new inkAnimDef;
		animDef.AddInterpolator( animData );
		m_animProxyHide = GetRootWidget().PlayAnimation( animDef );
	}

}

