class ShardItemVirtualController extends inkVirtualCompoundItemController
{
	private editable var m_icon : inkWidgetRef;
	private editable var m_label : inkTextRef;
	private editable var m_counter : inkTextRef;
	private editable var m_collapseIcon : inkWidgetRef;
	private editable var m_isNewFlag : inkWidgetRef;
	private var m_entryData : ShardEntryData;
	private var m_nestedListData : VirutalNestedListData;
	private var m_activeItemSync : weak< CodexListSyncData >;
	private var m_isActive : Bool;
	private var m_isItemHovered : Bool;
	private var m_isItemToggled : Bool;
	private var m_isItemCollapsed : Bool;
	private var m_clicked : Bool;

	protected event OnInitialize()
	{
		RegisterToCallback( 'OnToggledOn', this, 'OnToggledOn' );
		RegisterToCallback( 'OnSelected', this, 'OnSelected' );
		RegisterToCallback( 'OnDeselected', this, 'OnDeselected' );
		RegisterToCallback( 'OnToggledOff', this, 'OnToggledOff' );
		inkWidgetRef.SetRotation( m_collapseIcon, 90.0 );
	}

	protected event OnUnnitialize()
	{
		UnregisterFromCallback( 'OnToggledOn', this, 'OnToggledOn' );
		UnregisterFromCallback( 'OnSelected', this, 'OnSelected' );
		UnregisterFromCallback( 'OnDeselected', this, 'OnDeselected' );
		UnregisterFromCallback( 'OnToggledOff', this, 'OnToggledOff' );
	}

	public function OnDataChanged( value : Variant )
	{
		m_nestedListData = ( ( VirutalNestedListData )( ( ( IScriptable )value ) ) );
		m_entryData = ( ( ShardEntryData )( m_nestedListData.m_data ) );
		m_activeItemSync = m_entryData.m_activeDataSync;
		inkTextRef.SetText( m_counter, ( "(" + ( ( String )( m_entryData.m_counter ) ) ) + ")" );
		CheckIsNew();
		inkTextRef.SetText( m_label, m_entryData.m_title );
		UpdateState();
	}

	protected event OnContactSyncData( evt : ShardSyncBackEvent )
	{
		UpdateState();
	}

	protected event OnEntrySelected( evt : ShardEntrySelectedEvent )
	{
		if( m_entryData.m_newEntries.Contains( ( ( Int32 )( evt.m_hash ) ) ) )
		{
			m_entryData.m_newEntries.Remove( ( ( Int32 )( evt.m_hash ) ) );
		}
		CheckIsNew();
	}

	public function CheckIsNew()
	{
		if( m_entryData.m_newEntries.Size() > 0 )
		{
			inkWidgetRef.SetState( m_isNewFlag, 'isNew' );
			inkTextRef.SetState( m_label, 'isNew' );
			inkWidgetRef.SetState( m_icon, 'isNew' );
			inkWidgetRef.SetState( m_collapseIcon, 'isNew' );
		}
		else
		{
			inkWidgetRef.SetState( m_isNewFlag, 'Default' );
			inkTextRef.SetState( m_label, 'Default' );
			inkWidgetRef.SetState( m_icon, 'Default' );
			inkWidgetRef.SetState( m_collapseIcon, 'Default' );
		}
	}

	protected event OnToggledOn( itemController : weak< inkVirtualCompoundItemController > )
	{
		var evt : ShardSelectedEvent;
		evt = new ShardSelectedEvent;
		evt.m_entryHash = m_entryData.m_hash;
		evt.m_level = m_nestedListData.m_level;
		evt.m_group = m_nestedListData.m_isHeader;
		evt.m_data = m_entryData;
		QueueEvent( evt );
	}

	protected event OnToggledOff( itemController : weak< inkVirtualCompoundItemController > )
	{
		UpdateState();
	}

	protected event OnSelected( itemController : weak< inkVirtualCompoundItemController >, discreteNav : Bool )
	{
		m_isItemHovered = true;
		UpdateState();
		if( discreteNav )
		{
			SetCursorOverWidget( GetRootWidget() );
		}
	}

	protected event OnDeselected( itemController : weak< inkVirtualCompoundItemController > )
	{
		m_isItemHovered = false;
		UpdateState();
	}

	private function UpdateState()
	{
		if( ( m_activeItemSync.m_level == m_nestedListData.m_level ) && m_nestedListData.m_isHeader )
		{
			GetRootWidget().SetState( 'SubActive' );
		}
		else if( ( m_activeItemSync.m_entryHash == m_entryData.m_hash ) && !( m_nestedListData.m_isHeader ) )
		{
			GetRootWidget().SetState( 'Active' );
		}
		else if( m_isItemHovered )
		{
			GetRootWidget().SetState( 'Hover' );
		}
		else
		{
			GetRootWidget().SetState( 'Default' );
		}
		if( m_nestedListData && m_activeItemSync )
		{
			m_isItemCollapsed = !( m_activeItemSync.m_toggledLevels.Contains( m_nestedListData.m_level ) );
		}
		if( inkWidgetRef.IsValid( m_collapseIcon ) )
		{
			inkWidgetRef.SetRotation( m_collapseIcon, ( ( m_isItemCollapsed ) ? ( 90.0 ) : ( 180.0 ) ) );
		}
	}

}

