#include "MW4Headers.hpp"
#include <adept\control_mapping.hpp>

#include "MWOptions.hpp"

//#############################################################################
//##############################     MWOptions   ##############################
//#############################################################################

MWOptions::MWOptions(MemoryStream *stream) :
	Plug(Plug::DefaultData)
{
	*stream >> m_difficultyLevel;
	*stream >> m_useHeat;
	*stream >> m_unlimmitedAmmo;
	*stream >> m_useInvincibility;
	*stream >> m_friendlyFire;
	*stream >> m_splashDamage;
	*stream >> m_weaponJam;
	*stream >> m_ammobayFire;
// MSL 5.05 Advance Mode
	*stream >> m_advanceMode;
// MSL 5.05 Armor Mode
	*stream >> m_armorMode;
	*stream >> m_defaultView;
	*stream >> m_resolution;
	*stream >> m_contrast;
	*stream >> m_brightness;
	*stream >> m_gamma;
	*stream >> m_sfxVolume;
	*stream >> m_musicVolume;
	*stream >> m_bettyOn;
	*stream >> m_audioHardware;
	*stream >> m_forceFeedback;
	*stream >> m_playerInsignia;
	*stream >> m_teamInsignia;
	*stream >> m_playerName;
	*stream >> m_teamName;
	*stream >> m_password;
	*stream >> m_closingCinemaName;

	//if we have this file then we should use its res
	NotationFile options_ini("options.ini", NotationFile::Standard, true);
	Page *page = options_ini.FindPage("graphics options");
	if(page)
	{
		int x_res = 800;
		page->GetEntry("screenwidth", &x_res);

		switch(x_res)
		{
		case 1600:
			m_resolution = Res1600x1200;
			break;
		case 1280:
			m_resolution = Res1280x1024;
			break;
		case 1024:
			m_resolution = Res1024x768;
			break;
		case 800:
			m_resolution = Res800x600;
			break;
		case 640:
			m_resolution = Res640x480;
			break;
		}
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MWOptions::ConstructOptionsStream(NotationFile *note_file, Stuff::MemoryStream *stream)
{
	Check_Pointer(stream);

	//Read in Game Options
	Page *page = NULL;
	
	if(note_file)
		page = note_file->FindPage("Game Options");

	int difficulty_level = 0;
	int use_heat = 1;
	int unlimmited_ammo = 0;
	int use_invincibility = 0;
	int friendly_fire = 1;
	int splash_damage = 1;
	int weapon_jam = 1;
	int ammo_bay_fire = 1;
	int advance_mode = 1;
	int armor_mode = 1;
	int default_view = 1;

	if(page)
	{
		page->GetEntry("DifficultyLevel", &difficulty_level);
		page->GetEntry("UseHeat", &use_heat);
		page->GetEntry("UnlimmitedAmmo", &unlimmited_ammo);
		page->GetEntry("UseInvincibility", &use_invincibility);
		page->GetEntry("FriendlyFire", &friendly_fire);
		page->GetEntry("SplashDamage", &splash_damage);
		page->GetEntry("WeaponJam", &weapon_jam);
		page->GetEntry("AmmoBayFire", &ammo_bay_fire);
		page->GetEntry("AdvanceMode", &advance_mode);
		page->GetEntry("ArmorMode", &armor_mode);
		page->GetEntry("DefalultView", &default_view);
	}

	Resource control_resource;
	DynamicMemoryStream control_stream;

	Verify (Adept::g_ControlList);

	*stream << difficulty_level;
	*stream << use_heat;
	*stream << unlimmited_ammo;
	*stream << use_invincibility;
	*stream << friendly_fire;
	*stream << splash_damage;
	*stream << weapon_jam;
	*stream << ammo_bay_fire;
	*stream << advance_mode;
	*stream << armor_mode;
	*stream << default_view;

	//Read in Video Options
	if(note_file)
		page = note_file->FindPage("Video Options");

	int resolution = Res800x600;
	Scalar contrast = 0.5f;
	Scalar brightness = 0.075f;
	Scalar gamma = 0.11756f;

	if(page)
	{
		page->GetEntry("Resolution", &resolution);
		page->GetEntry("Contrast", &contrast);
		page->GetEntry("Brightness", &brightness);
		page->GetEntry("Gamma", &gamma);
	}

	*stream << resolution;
	*stream << contrast;
	*stream << brightness;
	*stream << gamma;


	//Audio Options
	if(note_file)
		page = note_file->FindPage("Audio Options");

	int sfx_volume = 80;
	int music_volume = 80;
	int betty_on = 1;
	int force_feedback = 1;	
	int audio_hardware = 1;
		
	if(page)
	{

	}

	*stream << sfx_volume;
	*stream << music_volume;
	*stream << betty_on;
	*stream << audio_hardware;
	*stream << force_feedback;

	if(note_file)
		page = note_file->FindPage("Player Options");

	int player_insignia = 1;
	int team_insignia = 1;

	if(page)
	{
		page->GetEntry("PlayerInsignia", &player_insignia);
		page->GetEntry("TeamInsignia", &team_insignia);
	}

	*stream << player_insignia;
	*stream << team_insignia;

	Stuff::MString player_name="Player Name";
	Stuff::MString team_name="";
	Stuff::MString password="";

	*stream << player_name;
	*stream << team_name;
	*stream << password;
	
	MString end_cinema = "";
	*stream << end_cinema;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MWOptions::SaveToStream(Stuff::MemoryStream *stream)
{
	Check_Object(this);
	Check_Pointer(stream);

	*stream << m_difficultyLevel;
	*stream << m_useHeat;
	*stream << m_unlimmitedAmmo;
	*stream << m_useInvincibility;
	*stream << m_friendlyFire;
	*stream << m_splashDamage;
	*stream << m_weaponJam;
	*stream << m_ammobayFire;
// MSL 5.05 Advance Mode
	*stream << m_advanceMode;
// MSL 5.06 Armor Mode
	*stream << m_armorMode;
	*stream << m_defaultView;
	*stream << m_resolution;
	*stream << m_contrast;
	*stream << m_brightness;
	*stream << m_gamma;	
	*stream << m_sfxVolume;
	*stream << m_musicVolume;
	*stream << m_bettyOn;
	*stream << m_audioHardware;
	*stream << m_forceFeedback;
	*stream << m_playerInsignia;
	*stream << m_teamInsignia;
	*stream << m_playerName;
	*stream << m_teamName;
	*stream << m_password;
	*stream << m_closingCinemaName;

	NotationFile options_ini("options.ini", NotationFile::Standard, true);
	Page *page = options_ini.GetPage("graphics options");
	if(page)
	{
		int x_res = 800;
		page->GetEntry("screenwidth", &x_res);

		switch(m_resolution)
		{
		case Res1600x1200:
			{
				int x = 1600;
				int y = 1200;
				page->SetEntry("ScreenWidth", x);
				page->SetEntry("ScreenHeight", y);
				break;
			}
		case Res1280x1024:
			{
				int x = 1280;
				int y = 1024;
				page->SetEntry("ScreenWidth", x);
				page->SetEntry("ScreenHeight", y);
				break;
			}
		case Res1024x768:
			{
				int x = 1024;
				int y = 768;
				page->SetEntry("ScreenWidth", x);
				page->SetEntry("ScreenHeight", y);
				break;
			}
		case Res800x600:
			{
				int x = 800;
				int y = 600;
				page->SetEntry("ScreenWidth", x);
				page->SetEntry("ScreenHeight", y);
				break;
			}
		case Res640x480:
			{
				int x = 640;
				int y = 480;
				page->SetEntry("ScreenWidth", x);
				page->SetEntry("ScreenHeight", y);
				break;
			}
		}

		options_ini.Save();
	}
}