// ------------------------------------------------------------------------
/*
	\author: Jaap Suter

	Copyright (C) Electronic Arts Canada Inc. 2002.  All rights reserved.	
*/
// ------------------------------------------------------------------------

/*! \file */

#include "reveal/impl/mru_menu_forward.h"
#include "reveal/impl/menu.h"
#include "reveal/impl/util.h"
#include "reveal/impl/tree.h"

#include "reveal/input_state.h"
#include "reveal/renderer.h"
#include "reveal/gui_colors.h"

namespace Reveal {

	namespace Impl {

// -------------------------------------------------------------------------------
void MruMenuForward::Show( int x )
{
	mX = x;
}

// -------------------------------------------------------------------------------
void MruMenuForward::Close()
{
	if ( mMenu ) mMenu->Hide();
	mOpened = false;
}

// -------------------------------------------------------------------------------
bool MruMenuForward::ForwardsToStartMenu() const
{
    return mMenu && mMenu->IsRoot();
}

// -------------------------------------------------------------------------------
bool MruMenuForward::Handle( const InputState& inputState, Tree& tree )
{
	if ( !mMenu ) return false;

	// Check for a click on the lock button;
	if ( ! ForwardsToStartMenu() )
	{
		if ( inputState.Pressed( InputState::LEFT_MOUSE | InputState::CONFIRM ) )
		{
			if ( Util::PointInRect( inputState.MouseX(), inputState.MouseY(),
									mX + GuiMetrics::MRU_FORWARD_LOCK_X, GuiMetrics::MRU_FORWARD_LOCK_Y,
									GuiMetrics::GetItemSize(), GuiMetrics::GetItemSize() ) )
			{
				mLocked = !mLocked;
				return true;
			}
		}
	}

	Widget* widgetThatHandledTheInput = mMenu->Handle( inputState );
	return widgetThatHandledTheInput != NULL;
}

// -------------------------------------------------------------------------------
void MruMenuForward::Render( const Renderer& renderer, const GuiColors& guiColors, DrawChildren drawChildren ) const
{
	if ( mMenu && (drawChildren == DRAW_CHILDREN) ) mMenu->Render( renderer, guiColors );

	renderer.Bar( mX, 0, GuiMetrics::GetWidgetWidth(), GuiMetrics::BAR_HEIGHT, guiColors.mMruButtonColor );
	if ( mMenu )
	{
		if ( mOpened ) renderer.Bar( mX, GuiMetrics::BAR_HEIGHT - GuiMetrics::GetRowHeight(), GuiMetrics::GetWidgetWidth(), GuiMetrics::GetRowHeight(), guiColors.mHighlightedMruButtonColor );
		renderer.String( mX + GuiMetrics::TEXT_BORDER_WIDTH, GuiMetrics::BAR_HEIGHT - GuiMetrics::GetRowHeight() + GuiMetrics::TEXT_BORDER_HEIGHT, mMenu->Name(), guiColors.mTextColor );
				
		if ( ! ForwardsToStartMenu() )
		{
			Reveal::uint32_t lockColor = mLocked ? guiColors.mMruLockedColor : guiColors.mMruUnlockedColor;
			renderer.Bar( mX + GuiMetrics::MRU_FORWARD_LOCK_X, GuiMetrics::MRU_FORWARD_LOCK_Y, GuiMetrics::GetItemSize(), GuiMetrics::GetItemSize(), lockColor );
		}
	}
}

// -------------------------------------------------------------------------------
void MruMenuForward::Open( Tree& t )
{
	assert( mMenu );
	mMenu->Show( mX, GuiMetrics::BAR_HEIGHT - mMenu->Height() );
	t.SetOpenedMru( this );	
	t.SetWidgetWithFocus( mMenu );
	mOpened = true;
}

	} // Namespace Impl;
} // Namespace Reveal;
