//========= Copyright © 1996-2005, Sander Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "hl2wars/gameui/ui_mission_select.h"
#include <KeyValues.h>
#include <filesystem.h>
#include <vgui_controls/listpanel.h>
#include <vgui/IVGui.h>
#include <vgui_controls/button.h>
#include <vgui_controls/label.h>
#include <vgui_controls/imagepanel.h>
#include <vgui_bitmapimage.h>
//#include "cdll_client_int.h"
//#include "FileSystem.h"
//#include "iloadingdisc.h"

// memdbgon must be the last include file in a .cpp file!!! 
#include "tier0/memdbgon.h"
PanelGlobals(CMissionSelect,CMissionSelectPanel,missionselect);

#define TREE_TEXT_COLOR		Color( 200, 255, 200, 255 )
#define LIST_TEXT_COLOR		TREE_TEXT_COLOR

extern IFileSystem *filesystem;

//ConVar cl_showmissionselect("cl_showmissionselect", "0", FCVAR_CLIENTDLL, "Sets the state of missionselect <state>"); 
CON_COMMAND(ToggleMissionSelectPanel,NULL)
{
	ToggleVisibility(missionselect->GetPanel());
	missionselect->Activate(); 
}
CMissionSelectPanel::CMissionSelectPanel( vgui::VPANEL parent ) : BaseClass( NULL, "missionselect" )
{
	SetParent(parent);
	vgui::HScheme scheme = vgui::scheme()->LoadSchemeFromFile("resource/SourceScheme.res", "SourceScheme");
	SetScheme( scheme );
	//LoadControlSettings("Resource/UI/MissionSelect.res");
	CenterThisPanelOnScreen();//keep in mind, hl2 supports widescreen 
	SetVisible(false);//made visible on command later 

	//Other useful options
	//SetSizeable(false);
	//SetMoveable(false);

	m_pSelected = NULL;

	m_pMapPreviewImage = NULL;
	m_pMapBlackImage = NULL;

	vgui::ivgui()->AddTickSignal( GetVPanel(), 100 );

	// create the list panel
	m_pMapList = new vgui::ListPanel( this, "maplist");
	m_pMapList->SetMultiselectEnabled( false );
	m_pMapList->AddColumnHeader( 0, "name", "Missions", 200, ListPanel::COLUMN_RESIZEWITHWINDOW );

	// create the label to put the map description in
	m_pMapInfo = new Label( this, "mapinfo", "Select a mission from the left.");
	m_pMapInfo->SetVisible( true );

	m_pMapInfoTitle = new Label( this, "mapinfotitle", "Select a mission from the left.");
	m_pMapInfoTitle->SetVisible( true );

	m_pMapPreview = new ImagePanel( this, "mappreview" );
	m_pMapPreview->SetVisible( true );

	m_pMapBlack = new ImagePanel( this, "black" );
	m_pMapPreview->SetVisible( true );
	m_pMapBlack->SetZPos(-1);

	// create launch button
	m_pLaunch = new Button( this, "Launch", "Launch", this, "launch");
	m_pLaunch->SetVisible( true );

	// load the control settings
	LoadControlSettings("Resource/UI/MissionSelect.res");

	// refresh maplist
	RefreshMapsList();
} 

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMissionSelectPanel::ApplySchemeSettings( IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );

	// map list
	m_pMapList->SetFont( pScheme->GetFont( "DefaultVerySmall" ) );
	m_pMapList->SetFgColor( LIST_TEXT_COLOR );

	m_pMapPreview->SetShouldScaleImage( true );
	m_pMapBlack->SetShouldScaleImage( true );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMissionSelectPanel::Reset( void )
{
	m_pMapList->DeleteAllItems();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMissionSelectPanel::RefreshMapsList( void )
{
	KeyValues *pMissionData = new KeyValues( "data" );

	FileFindHandle_t findHandle;
	const char *pFilename = filesystem->FindFirstEx( "maps/hlw_*.bsp", "GAME", &findHandle );
	while ( pFilename != NULL )
	{
		pMissionData->Clear();

		char *mapname;
		mapname = (char *)malloc( (strlen( pFilename ) - 3) * sizeof( char ) );
		Q_strncpy(&mapname[0], pFilename, strlen( pFilename ) - 3);
		mapname[ strlen( pFilename ) - 4 ] = '\0';
		
		char tempfile[MAX_PATH];
		Q_snprintf( tempfile, sizeof( tempfile ), "resource/maps/%s.txt", mapname );

		if ( !pMissionData->LoadFromFile( filesystem, tempfile, "GAME" ) )
		{
			DevMsg( 1, "Error! CMissionSelectPanel::RefreshMapsList: couldn't load file %s.\n", tempfile );
		}

		pMissionData->SetString( "name", mapname );
		m_pMapList->AddItem( pMissionData, 0, false, true );

		/*KeyValues *pMissionData = new KeyValues( mapname );
		if ( !pMissionData->LoadFromFile( filesystem, tempfile, "GAME" ) )
		{
			DevMsg( 1, "Error! CMissionSelectPanel::RefreshMapsList: couldn't load file %s.\n", tempfile );
		}

		KeyValues *pData = new KeyValues( "data" );
		pData->SetString( "name", mapname );

		if( pMissionData )
			pData->SetString( "map_description", pMissionData->GetString("map_description") );
		else
			pData->SetString( "map_description", "No description available." );

		pMapList->AddItem( pData, 0, false, true );

		if( pMissionData )
			pMissionData->deleteThis();*/

		pFilename = filesystem->FindNext( findHandle );
	}
	filesystem->FindClose( findHandle );	

	if( pMissionData )
		pMissionData->deleteThis();
}

//-----------------------------------------------------------------------------
// Purpose: Handles things like behavior buttons
//-----------------------------------------------------------------------------
void CMissionSelectPanel::OnCommand(const char *command) 
{ 
	if (!stricmp(command, "launch")) 
	{ 
		if( !m_pSelected )
			return;

		// hide panel
		ToggleVisibility(missionselect->GetPanel());

		// set singleplayer
		engine->ClientCmd( "maxplayers 1" );
		engine->ClientCmd( "sv_lan 1" );
		engine->ClientCmd( "sv_hl2wars_gamemode 2" );

		// create the map command
		char command[MAX_PATH];
		Q_snprintf( command, sizeof( command ), "map %s", m_pSelected->GetString("name") );

		engine->ClientCmd( "progress_enable" );	// see the progress
		engine->ClientCmd( command );	// load the map

		return;
	} 

	BaseClass::OnCommand(command); 
} 

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CMissionSelectPanel::OnTick()
{
	//RefreshMapsList();
	int itemid = m_pMapList->GetSelectedItem( 0 );
	if( itemid != -1 )
	{
		KeyValues *selected = m_pMapList->GetItem( itemid );
		if( selected != m_pSelected )
		{
			m_pSelected = selected;

			if( m_pMapPreviewImage )
			{
				delete m_pMapPreviewImage;
				m_pMapPreviewImage = NULL;
				delete m_pMapBlackImage;
				m_pMapBlackImage = NULL;
			}

			m_pMapPreviewImage = new BitmapImage( m_pMapPreview->GetVPanel(), m_pSelected->GetString( "map_preview", "VGUI/units/unit_unknown") ); 
			m_pMapBlackImage = new BitmapImage( m_pMapPreview->GetVPanel(), "VGUI/black" ); 

			m_pMapInfoTitle->SetText( m_pSelected->GetString( "map_title", "No title available!") );
			m_pMapPreview->SetImage( m_pMapPreviewImage );
			m_pMapBlack->SetImage( m_pMapBlackImage );
			m_pMapInfo->SetText( m_pSelected->GetString( "map_description", "No description available!") );
		}
	}
	else
	{
		m_pSelected = NULL;
	}
}



