/****************************************************************************
*                                                                           *
* Project 64 - A Nintendo 64 emulator.                                      *
* http://www.pj64-emu.com/                                                  *
* Copyright (C) 2012 Project64. All rights reserved.                        *
*                                                                           *
* License:                                                                  *
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html                        *
*                                                                           *
****************************************************************************/
#include "stdafx.h"
#include "SettingsType-RomDatabase.h"

CIniFile * CSettingTypeRomDatabase::m_SettingsIniFile = NULL;
CIniFile * CSettingTypeRomDatabase::m_GlideIniFile    = NULL;
stdstr   * CSettingTypeRomDatabase::m_SectionIdent    = NULL;

CSettingTypeRomDatabase::CSettingTypeRomDatabase(LPCSTR Name, int DefaultValue, bool DeleteOnDefault ) :
	m_KeyName(StripNameSection(Name)),
	m_DefaultStr(""),
	m_DefaultValue(DefaultValue),
	m_DefaultSetting(Default_Constant),
	m_DeleteOnDefault(DeleteOnDefault),
	m_GlideSetting(IsGlideSetting(Name))
{
}

CSettingTypeRomDatabase::CSettingTypeRomDatabase(LPCSTR Name, bool DefaultValue, bool DeleteOnDefault ) :
	m_KeyName(StripNameSection(Name)),
	m_DefaultStr(""),
	m_DefaultValue(DefaultValue),
	m_DefaultSetting(Default_Constant),
	m_DeleteOnDefault(DeleteOnDefault),
	m_GlideSetting(IsGlideSetting(Name))
{
}

CSettingTypeRomDatabase::CSettingTypeRomDatabase(LPCSTR Name, LPCSTR DefaultValue, bool DeleteOnDefault ) :
	m_KeyName(StripNameSection(Name)),
	m_DefaultStr(DefaultValue),
	m_DefaultValue(0),
	m_DefaultSetting(Default_Constant),
	m_DeleteOnDefault(DeleteOnDefault),
	m_GlideSetting(IsGlideSetting(Name))
{
}

CSettingTypeRomDatabase::CSettingTypeRomDatabase(LPCSTR Name, SettingID DefaultSetting, bool DeleteOnDefault ) :
	m_KeyName(Name),
	m_DefaultStr(""),
	m_DefaultValue(0),
	m_DefaultSetting(DefaultSetting),
	m_DeleteOnDefault(DeleteOnDefault),
	m_GlideSetting(IsGlideSetting(Name))
{
}

CSettingTypeRomDatabase::~CSettingTypeRomDatabase()
{
}

void CSettingTypeRomDatabase::Initilize( void )
{
	m_SettingsIniFile = new CIniFile(g_Settings->LoadString(SupportFile_RomDatabase).c_str());
	m_GlideIniFile = new CIniFile(g_Settings->LoadString(SupportFile_Glide64RDB).c_str());

	g_Settings->RegisterChangeCB(Game_IniKey,NULL,GameChanged);
	
	m_SectionIdent = new stdstr(g_Settings->LoadString(Game_IniKey));
}

void CSettingTypeRomDatabase::CleanUp( void )
{
	g_Settings->UnregisterChangeCB(Game_IniKey,NULL,GameChanged);
	if (m_SettingsIniFile)
	{
		delete m_SettingsIniFile;
		m_SettingsIniFile = NULL;
	}
	if (m_GlideIniFile)
	{
		delete m_GlideIniFile;
		m_GlideIniFile = NULL;
	}
	if (m_SectionIdent)
	{
		delete m_SectionIdent;
		m_SectionIdent = NULL;
	}
}

void CSettingTypeRomDatabase::GameChanged ( void * /*Data */ )
{
	if (m_SectionIdent)
	{
		*m_SectionIdent = g_Settings->LoadString(Game_IniKey);
	}
}

bool CSettingTypeRomDatabase::Load ( int Index, bool & Value ) const
{
	DWORD temp_value = Value;
	bool bRes = Load(Index,temp_value);
	Value = temp_value != 0;
	return bRes;
}

bool CSettingTypeRomDatabase::Load ( int Index, ULONG & Value ) const
{
	bool bRes = false;
	if (m_GlideSetting)
	{
		bRes = m_GlideIniFile->GetNumber(Section(),m_KeyName.c_str(),Value,Value);
	}
	else 
	{
		bRes = m_SettingsIniFile->GetNumber(Section(),m_KeyName.c_str(),Value,Value);
	}
	if (!bRes)
	{
		LoadDefault(Index,Value);
	}
	return bRes;
}

bool CSettingTypeRomDatabase::Load ( int Index, stdstr & Value ) const
{
	stdstr temp_value;
	bool bRes = false;
	if (m_GlideSetting)
	{
		bRes = m_GlideIniFile->GetString(Section(),m_KeyName.c_str(),m_DefaultStr,temp_value);
	}
	else
	{
		bRes = m_SettingsIniFile->GetString(Section(),m_KeyName.c_str(),m_DefaultStr,temp_value);
	}
	if (bRes)
	{
		Value = temp_value;
	}
	else 
	{
		LoadDefault(Index,Value);
	}
	return bRes;
}


//return the default values
void CSettingTypeRomDatabase::LoadDefault ( int /*Index*/, bool & Value ) const
{
	if (m_DefaultSetting != Default_None)
	{
		if (m_DefaultSetting == Default_Constant)
		{
			Value = m_DefaultValue != 0;
		} else {
			g_Settings->LoadBool(m_DefaultSetting,Value);
		}
	}
}

void CSettingTypeRomDatabase::LoadDefault ( int /*Index*/, ULONG & Value  ) const
{
	if (m_DefaultSetting != Default_None)
	{
		if (m_DefaultSetting == Default_Constant)
		{
			Value = m_DefaultValue;
		} else {
			g_Settings->LoadDword(m_DefaultSetting,Value);
		}
	}
}

void CSettingTypeRomDatabase::LoadDefault ( int /*Index*/, stdstr & Value ) const
{
	if (m_DefaultSetting != Default_None)
	{
		if (m_DefaultSetting == Default_Constant)
		{
			Value = m_DefaultStr;
		} else {
			g_Settings->LoadString(m_DefaultSetting,Value);
		}
	}
}


//Update the settings
void CSettingTypeRomDatabase::Save ( int /*Index*/, bool Value )
{
	if (!g_Settings->LoadBool(Setting_RdbEditor))
	{
		return;
	}
	if (m_DeleteOnDefault)
	{	
		Notify().BreakPoint(__FILEW__,__LINE__); 
	}
	if (m_GlideSetting)
	{
		m_GlideIniFile->SaveNumber(Section(),m_KeyName.c_str(),Value);
	}
	else 
	{
		m_SettingsIniFile->SaveNumber(Section(),m_KeyName.c_str(),Value);
	}
}

void CSettingTypeRomDatabase::Save ( int Index, ULONG Value )
{
	if (!g_Settings->LoadBool(Setting_RdbEditor))
	{
		return;
	}
	if (m_DeleteOnDefault)
	{	
		ULONG defaultValue = 0;
		LoadDefault(Index,defaultValue);
		if (defaultValue == Value)
		{
			Delete(Index);
			return;
		}
	}
	if (m_GlideSetting)
	{
		m_GlideIniFile->SaveNumber(Section(),m_KeyName.c_str(),Value);
	}
	else
	{
		m_SettingsIniFile->SaveNumber(Section(),m_KeyName.c_str(),Value);
	}
}

void CSettingTypeRomDatabase::Save ( int /*Index*/, const stdstr & Value )
{
	if (!g_Settings->LoadBool(Setting_RdbEditor))
	{
		return;
	}
	if (m_GlideSetting)
	{
		m_GlideIniFile->SaveString(Section(),m_KeyName.c_str(),Value.c_str());
	}
	else
	{
		m_SettingsIniFile->SaveString(Section(),m_KeyName.c_str(),Value.c_str());
	}
}

void CSettingTypeRomDatabase::Save ( int /*Index*/, const char * Value )
{
	if (!g_Settings->LoadBool(Setting_RdbEditor))
	{
		return; 
	}
	if (m_GlideSetting)
	{
		m_GlideIniFile->SaveString(Section(),m_KeyName.c_str(),Value);
	}
	else
	{
		m_SettingsIniFile->SaveString(Section(),m_KeyName.c_str(),Value);
	}
}

void CSettingTypeRomDatabase::Delete ( int /*Index*/ )
{
	if (!g_Settings->LoadBool(Setting_RdbEditor))
	{
		return; 
	}
	if (m_GlideSetting)
	{
		m_GlideIniFile->SaveString(Section(),m_KeyName.c_str(),NULL);
	}
	else
	{
		m_SettingsIniFile->SaveString(Section(),m_KeyName.c_str(),NULL);
	}
}

bool CSettingTypeRomDatabase::IsGlideSetting (LPCSTR Name)
{
	if (_strnicmp(Name,"Glide64-",8) == 0)
	{
		return true;
	}
	return false;
}

LPCSTR CSettingTypeRomDatabase::StripNameSection (LPCSTR Name)
{
	if (_strnicmp(Name,"Glide64-",8) == 0)
	{
		return &Name[8];
	}
	return Name;
}
