/* ***** BEGIN LICENSE BLOCK *****
Version: MPL 1.1/LGPL 2.1/GPL 2.0

The contents of this file are subject to the Mozilla Public License Version 
1.1 (the "License"); you may not use this file except in compliance with
...
for the specific language governing rights and limitations under the
License.

The Original Code is for Luminous Forts.

The Initial Developer of the Original Code is Hekar Khani.
Portions created by the Hekar Khani are Copyright (C) 2010
Hekar Khani. All Rights Reserved.

Contributor(s):
  Hekar Khani <hekark@gmail.com>

Alternatively, the contents of this file may be used under the terms of
either of the GNU General Public License Version 2 or later (the "GPL"),
...
the terms of any one of the MPL, the GPL or the LGPL.

***** END LICENSE BLOCK ***** */


#include "cbase.h"
#include <vgui/IScheme.h>
#include <vgui/ISurface.h>
#include "vgui/IInput.h"
#include "vgui_bitmapbutton.h"
#include "ienginevgui.h"

#include <vgui_controls/Controls.h>

#include "GUI_MainMenu.h"
#include "GUI_Titlescreen.h"

CREATE_GUI_INTERFACE( TitleScreenInterface, titlescreen );

//-------------------- Background Image ---------------------//
CBackgroundImage::CBackgroundImage( Panel *Parent, VPANEL parent ) :
	ImagePanel( Parent, "BackgroundImage" )
{
	SetParent( parent );
	int w, h;
	surface()->GetScreenSize( w, h );

	SetSize( w, h );

	SetVisible( true );
	SetProportional( true );

	SetShouldScaleImage( true );
}

CBackgroundImage::~CBackgroundImage()
{
}

CTitleScreen::CTitleScreen( vgui::VPANEL parent ) :
	BaseClass( NULL, "TitleScreen" )
{
	VPANEL gamepanel = enginevgui->GetPanel( PANEL_GAMEUIDLL );
	SetParent( gamepanel );

	int sw, sh;
	surface()->GetScreenSize( sw, sh );
	SetBounds( 0, 0, sw, sh );

#ifdef MOD_BACKGROUND_VIDEO
	m_pLoopedBackground = new CBinkVideoPanel( 0, 0, sw, sh, GetVPanel() );
	m_pLoopedBackground->SetRepeat( true );
#endif

#ifdef MOD_BACKGROUND_IMAGE
	m_pBackground = new CBackgroundImage( this, GetVPanel() );
	m_pBackground->SetVisible( true );
#endif

#ifdef MOD_DEV
	m_pTitle = new Label( this, "Title_Label", "#lf_titlescreen_title_dev" );
#else
	m_pTitle = new Label( this, "Title_Label", "#lf_titlescreen_title" );
#endif

	SetProportional( true );
	SetMouseInputEnabled( false );
	SetKeyBoardInputEnabled( false );

	ipanel()->MoveToFront( GetVPanel() );

	InitSettings();
}

CTitleScreen::~CTitleScreen()
{
#ifdef MOD_BACKGROUND_IMAGE
	m_pBackground->SetParent( NULL );
	delete m_pBackground;
#endif

	delete m_pTitle;
}

void CTitleScreen::InitSettings()
{
	int sw, sh;
	surface()->GetScreenSize( sw, sh );
	SetBounds( 0, 0, sw, sh );

#ifdef MOD_BACKGROUND_VIDEO
	m_pLoopedBackground->SetPos( 0, 0 );
	m_pLoopedBackground->SetSize( sw, sh );
	m_pLoopedBackground->Activate();
	m_pLoopedBackground->Play( "media/valve.bik" );
#endif

#ifdef MOD_BACKGROUND_IMAGE
	m_pBackground->SetImage( "background/background_arora.vmt" );
	m_pBackground->SetAlpha( 32 );
	m_pBackground->SetPos( 0, 0 );
	m_pBackground->SetSize( sw, sh );
#endif

	m_pTitle->SetSize( sw, 128 );
	m_pTitle->SetPos( 0, sh / 4.25 );
	
	if ( !lf_title_mainmenu_justifyleft.GetBool() )
	{
		m_pTitle->SetContentAlignment( vgui::Label::a_center );
	}
	else
	{
		m_pTitle->SetPos( lf_title_mainmenu_justifyleft_offsetx.GetInt(), sh / 5 );
		m_pTitle->SetContentAlignment( vgui::Label::a_west );
	}

	vgui::HScheme scheme = vgui::scheme()->LoadSchemeFromFile( "resource/SourceScheme.res", "SourceScheme" );
	HFont treeFont = vgui::scheme()->GetIScheme( scheme )->GetFont( "LFTitle" );
	m_pTitle->SetFont( treeFont );
}

void CTitleScreen::ShowPanel( bool bShow )
{
	if ( BaseClass::IsVisible() == bShow )
		return;

	if ( bShow )
	{
		SetVisible( true );
		RequestFocus();
	}
	else
	{
		SetVisible( false );
		SetMouseInputEnabled( false );
	}
}

void CTitleScreen::OnClose()
{
#ifdef MOD_BACKGROUND_VIDEO
	m_pLoopedBackground->OnClose();
#endif

	BaseClass::OnClose();
}

void CTitleScreen::OnScreenSizeChanged( int oldwide, int oldtall )
{
	InitSettings();
}

void CTitleScreen::OnThink( void )
{
	// if ( engine->IsConnected() ) // True in loading screen also
	if ( engine->IsInGame() )
	{
		SetAlpha( 64 );
#ifdef MOD_BACKGROUND_VIDEO
	if ( m_pLoopedBackground->IsPlaying() )
	{
		m_pLoopedBackground->Stop();
	}
#endif
	}
	else
	{
		SetAlpha( 255 );
	}
}