class DropdownListController extends inkLogicController
{
	protected editable var m_listContainer : inkCompoundRef;
	private var m_ownerController : weak< IScriptable >;
	private var m_triggerButton : weak< DropdownButtonController >;
	private var m_displayContext : DropdownDisplayContext;
	private var m_activeElement : weak< DropdownElementController >;
	private var m_listOpened : Bool;
	private var m_data : array< DropdownItemData >;

	protected event OnInitialize()
	{
		RegisterToCallback( 'OnHoverOut', this, 'OnHoverOut' );
		RegisterToCallback( 'OnRelease', this, 'OnRelease' );
		Close();
	}

	protected event OnRelease( evt : inkPointerEvent )
	{
		if( evt.GetTarget() == GetRootWidget() )
		{
			if( evt.IsAction( 'click' ) )
			{
				Close();
			}
		}
	}

	protected event OnHoverOut( evt : inkPointerEvent )
	{
		if( evt.GetTarget() == GetRootWidget() )
		{
			if( m_listOpened )
			{
				Close();
			}
		}
	}

	public function Setup( owner : weak< inkLogicController >, const data : ref< array< DropdownItemData > >, optional triggerButton : DropdownButtonController )
	{
		m_ownerController = ( ( weak< weak< IScriptable > > )( owner ) );
		m_triggerButton = triggerButton;
		SetupData( data );
	}

	public function Setup( owner : weak< inkGameController >, const data : ref< array< DropdownItemData > >, optional triggerButton : DropdownButtonController )
	{
		m_ownerController = ( ( weak< weak< IScriptable > > )( owner ) );
		m_triggerButton = triggerButton;
		SetupData( data );
	}

	public function Setup( owner : weak< inkLogicController >, displayContext : DropdownDisplayContext, optional triggerButton : DropdownButtonController )
	{
		m_ownerController = ( ( weak< weak< IScriptable > > )( owner ) );
		m_triggerButton = triggerButton;
		if( m_displayContext != displayContext )
		{
			SetupData( SortingDropdownData.GetContextDropdownOptions( displayContext ) );
		}
		m_displayContext = displayContext;
	}

	public function Setup( owner : weak< inkGameController >, displayContext : DropdownDisplayContext, optional triggerButton : DropdownButtonController )
	{
		m_ownerController = ( ( weak< weak< IScriptable > > )( owner ) );
		m_triggerButton = triggerButton;
		if( m_displayContext != displayContext )
		{
			SetupData( SortingDropdownData.GetContextDropdownOptions( displayContext ) );
		}
		m_displayContext = displayContext;
	}

	private function SetupData( const data : ref< array< DropdownItemData > > )
	{
		var i : Int32;
		var item : DropdownElementController;
		m_data = data;
		inkCompoundRef.RemoveAllChildren( m_listContainer );
		for( i = 0; i < data.Size(); i += 1 )
		{
			item = ( ( DropdownElementController )( SpawnFromLocal( inkCompoundRef.Get( m_listContainer ), 'dropdownElement' ).GetController() ) );
			item.RegisterToCallback( 'OnRelease', this, 'OnDropdownItemClicked' );
			item.Setup( data[ i ] );
			if( i == 0 )
			{
				item.SetActive( true );
				m_activeElement = item;
			}
		}
	}

	public function GetDisplayContext() : DropdownDisplayContext
	{
		return m_displayContext;
	}

	public function GetData() : array< DropdownItemData >
	{
		return m_data;
	}

	public function SetTriggerButton( triggerButton : DropdownButtonController )
	{
		m_triggerButton = triggerButton;
	}

	public function Open()
	{
		inkCompoundRef.SetVisible( m_listContainer, true );
		if( m_triggerButton )
		{
			m_triggerButton.SetOpened( true );
		}
		m_listOpened = true;
	}

	public function Close()
	{
		inkCompoundRef.SetVisible( m_listContainer, false );
		if( m_triggerButton )
		{
			m_triggerButton.SetOpened( false );
		}
		m_listOpened = false;
	}

	public function Toggle()
	{
		if( m_listOpened )
		{
			Close();
		}
		else
		{
			Open();
		}
	}

	public function IsOpened() : Bool
	{
		return m_listOpened;
	}

	protected event OnDropdownItemClicked( evt : inkPointerEvent )
	{
		var dropdownEvent : DropdownItemClickedEvent;
		var targetWidget : weak< inkWidget >;
		var controller : DropdownElementController;
		if( evt.IsAction( 'click' ) )
		{
			targetWidget = evt.GetCurrentTarget();
			controller = ( ( DropdownElementController )( targetWidget.GetController() ) );
			if( m_activeElement )
			{
				m_activeElement.SetActive( false );
			}
			m_activeElement = controller;
			controller.SetActive( true );
			dropdownEvent = new DropdownItemClickedEvent;
			dropdownEvent.owner = m_ownerController;
			dropdownEvent.identifier = controller.GetIdentifier();
			dropdownEvent.triggerButton = m_triggerButton;
			if( ( ( weak< weak< inkGameController > > )( m_ownerController ) ) )
			{
				( ( weak< weak< inkGameController > > )( m_ownerController ) ).QueueEvent( dropdownEvent );
			}
			else if( ( ( weak< weak< inkLogicController > > )( m_ownerController ) ) )
			{
				( ( weak< weak< inkLogicController > > )( m_ownerController ) ).QueueEvent( dropdownEvent );
			}
			Close();
		}
	}

}

class DropdownItemClickedEvent extends Event
{
	var owner : weak< IScriptable >;
	var triggerButton : weak< DropdownButtonController >;
	var identifier : Variant;
}

