class SocialPanelContactsList extends inkLogicController
{
	private editable var m_ListItemName : CName;
	default m_ListItemName = 'contactsListItem';
	private editable var m_ItemsRoot : inkBasePanelRef;
	private var m_ItemsList : array< weak< SocialPanelContactsListItem > >;
	private var m_CurrentContactHash : Int32;
	default m_CurrentContactHash = -1;
	private var m_LastClickedContact : weak< JournalContact >;

	public function RefreshContactsList( const contacts : ref< array< SocialPanelContactInfo > > )
	{
		var currentController : weak< SocialPanelContactsListItem >;
		var i, limit, numItems : Int32;
		for( i = 0, limit = contacts.Size(), numItems = 0; i < limit; i += 1 )
		{
			if( contacts[ i ].Contact )
			{
				AddContactItem( contacts[ i ], numItems );
				numItems += 1;
			}
		}
		while( m_ItemsList.Size() > numItems )
		{
			currentController = m_ItemsList.PopBack();
			currentController.UnregisterFromCallback( 'OnRelease', this, 'OnListItemClicked' );
			inkBasePanelRef.RemoveChild( m_ItemsRoot, currentController.GetRootWidget() );
		}
	}

	private function AddContactItem( contactInfo : SocialPanelContactInfo, currentItem : Int32 )
	{
		var currentController : weak< SocialPanelContactsListItem >;
		if( currentItem < m_ItemsList.Size() )
		{
			currentController = m_ItemsList[ currentItem ];
		}
		else
		{
			currentController = ( ( SocialPanelContactsListItem )( SpawnFromLocal( inkBasePanelRef.Get( m_ItemsRoot ), m_ListItemName ).GetController() ) );
			currentController.RegisterToCallback( 'OnRelease', this, 'OnListItemClicked' );
			m_ItemsList.PushBack( currentController );
		}
		currentController.Setup( contactInfo );
		if( contactInfo.Hash == m_CurrentContactHash )
		{
			currentController.SetToggled( true );
		}
	}

	public function ChooseContact( contactToShowHash : Int32 ) : Bool
	{
		var i, limit, currentlySelected, toSelect, currentHash : Int32;
		for( i = 0, limit = m_ItemsList.Size(), toSelect = -1; i < limit; i += 1 )
		{
			currentHash = m_ItemsList[ i ].GetHash();
			if( currentHash == contactToShowHash )
			{
				toSelect = i;
			}
			if( currentHash == m_CurrentContactHash )
			{
				currentlySelected = i;
			}
		}
		if( toSelect != -1 )
		{
			m_ItemsList[ toSelect ].SetToggled( true );
			m_CurrentContactHash = contactToShowHash;
			if( currentlySelected != -1 )
			{
				m_ItemsList[ currentlySelected ].SetToggled( false );
			}
			return true;
		}
		return false;
	}

	private function OnListItemClicked( e : inkPointerEvent )
	{
		var currController : weak< SocialPanelContactsListItem >;
		currController = ( ( SocialPanelContactsListItem )( e.GetTarget().GetController() ) );
		if( currController )
		{
			m_LastClickedContact = currController.GetContact();
			CallCustomCallback( 'OnContactChangedRequest' );
		}
	}

	public function GetClickedContact() : weak< JournalContact >
	{
		return m_LastClickedContact;
	}

}

class SocialPanelContactsListItem extends inkToggleController
{
	private editable var m_Label : inkTextRef;
	private var m_ContactInfo : SocialPanelContactInfo;

	public function Setup( contactInfo : SocialPanelContactInfo )
	{
		var dummyJournalManager : IJournalManager;
		m_ContactInfo = contactInfo;
		inkTextRef.SetText( m_Label, contactInfo.Contact.GetLocalizedName( dummyJournalManager ) );
		SetToggled( false );
	}

	public function GetHash() : Int32
	{
		return m_ContactInfo.Hash;
	}

	public function GetContact() : weak< JournalContact >
	{
		return m_ContactInfo.Contact;
	}

}

