class ListItemsGroupController extends CodexListItemController
{
	protected editable var m_menuList : inkCompoundRef;
	protected editable var m_foldArrowRef : inkWidgetRef;
	protected editable var m_foldoutButton : inkWidgetRef;
	protected editable var m_foldoutIndipendently : Bool;
	protected var m_menuListController : weak< ListController >;
	private var m_foldoutButtonController : weak< inkButtonController >;
	protected var m_lastClickedData : weak< IScriptable >;
	protected var m_data : array< IScriptable >;
	protected var m_isOpen : Bool;

	protected event OnInitialize()
	{
		super.OnInitialize();
		RegisterToCallback( 'OnAddedToList', this, 'OnAddedToList' );
		inkWidgetRef.SetRotation( m_foldArrowRef, 0.0 );
	}

	protected event OnAddedToList( target : weak< ListItemController > )
	{
		m_menuListController = ( ( ListController )( inkCompoundRef.GetControllerByType( m_menuList, 'inkListController' ) ) );
		m_menuListController.RegisterToCallback( 'OnItemActivated', this, 'OnContentClicked' );
		if( m_foldoutIndipendently )
		{
			m_foldoutButtonController = ( ( inkButtonController )( inkWidgetRef.GetControllerByType( m_foldoutButton, 'inkButtonController' ) ) );
			m_foldoutButtonController.RegisterToCallback( 'OnRelease', this, 'OnFoldoutButtonClicked' );
		}
		else
		{
			RegisterToCallback( 'OnToggledOn', this, 'OnToggledOn' );
			RegisterToCallback( 'OnToggledOff', this, 'OnToggledOff' );
		}
	}

	public virtual function SetData( const data : ref< array< IScriptable > > )
	{
		m_data.Clear();
		m_data = data;
		if( m_data.Size() > 0 )
		{
			m_lastClickedData = m_data[ 0 ];
			if( IsToggled() )
			{
				OpenGroup();
			}
		}
	}

	protected event OnContentClicked( index : Int32, target : ListItemController )
	{
		ProcessToggledOn( target.GetData() );
		CallCustomCallback( 'OnContentClicked' );
	}

	protected virtual function ProcessToggledOn( data : weak< IScriptable > )
	{
		m_lastClickedData = data;
	}

	protected event OnFoldoutButtonClicked( e : inkPointerEvent )
	{
		if( m_foldoutIndipendently )
		{
			if( m_isOpen )
			{
				CloseGroup();
			}
			else
			{
				OpenGroup();
			}
		}
		e.Handle();
		CallCustomCallback( 'OnFoldoutButtonClicked' );
	}

	protected event OnToggledOn( target : weak< ListItemController > )
	{
		OpenGroup();
	}

	protected event OnToggledOff( target : weak< ListItemController > )
	{
		CloseGroup();
	}

	public virtual function OpenGroup()
	{
		m_menuListController.Clear();
		m_menuListController.PushDataList( m_data, true );
		inkCompoundRef.SetVisible( m_menuList, true );
		inkWidgetRef.SetRotation( m_foldArrowRef, 180.0 );
		RemoveNew();
	}

	public virtual function CloseGroup()
	{
		m_menuListController.Clear( true );
		inkCompoundRef.SetVisible( m_menuList, false );
		inkWidgetRef.SetRotation( m_foldArrowRef, 0.0 );
	}

	public function GetLastClicked() : weak< IScriptable >
	{
		return m_lastClickedData;
	}

	public virtual function Select( entry : weak< IScriptable > )
	{
		var findIndex : Int32;
		findIndex = m_menuListController.FindIndex( entry );
		if( findIndex >= 0 )
		{
			m_menuListController.SetToggledIndex( findIndex );
		}
	}

	public virtual function SelectDefault()
	{
		if( m_menuListController.Size() > 0 )
		{
			m_menuListController.SetToggledIndex( 0 );
		}
	}

}

