/**
    File        : Settings.cpp
    Description : Global application settings
    Author      : Raymond Marsh
    Created     : 08/01/2009
    Copyright 2009, 2010 Raymond Marsh

    This program is distributed under the terms of the GNU General Public License.
    To view a copy of the license see the file 'copying.txt'

    This file is part of the Ace Personal Finance Assistant Suite.

    Ace Personal Finance Assistant is free software: you can redistribute it and/or modify it under
    the terms of the GNU General Public License as published by the Free Software Foundation, either
    version 3 of the License, or (at your option) any later version.

    Ace Personal Finance Assistant is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along with Ace Personal Finance Assistant.
    If not, see <http://www.gnu.org/licenses/>.
**/

#include "Settings.h"

/* List of key / values
     GLOBAL.FirstRun                bool            Is this the first time the program has been run?
     GLOBAL.ReoccuringSet           bool            Are reoccuring transactions set up?
     GLOBAL.Language                int             Language to display text in
     COLORS.MainBGColor             wxColour        Top level wxFrame backgound color
     COLORS.DialogBGColor           wxColor         All dialog window background color
     WARNINGS.IsCategory            bool            Has a category been entered in a transaction
*/

namespace ace
{
COptions* g_pOptions;

bool COptions::Initialize()
{
    // set options filename
#ifndef _DEBUG
    m_Filename = wxStandardPaths::Get().GetUserConfigDir();
    m_Filename += _("\\AcePFA\\AceOptions.adf");
#else
    wxString s = wxStandardPaths::Get().GetUserConfigDir();
    m_Filename = _("Data\\AceOptions.adf");
#endif

    // defaults
    m_DefaultMainBGColor.Set(27, 137, 40);
    m_DefaultDialogBGColor.Set(27, 137, 40);
    m_DefaultHideReconciledTransactionsOnStart = false;
    m_DefaultLanguage = 0;

    GLOBAL.FirstRun = false;
    GLOBAL.ReoccuringSet = false;
    GLOBAL.HideReconciledTransactionsOnStart = m_DefaultHideReconciledTransactionsOnStart;
    GLOBAL.Language = m_DefaultLanguage;
    COLORS.MainBGColor = m_DefaultMainBGColor;
    COLORS.DialogBGColor = m_DefaultDialogBGColor;
    WARNINGS.IsCategory = true;

    return true;
}

bool COptions::LoadOptions()
{
    if (!wxFile::Exists(m_Filename) )
    {
        wxFileOutputStream FOS(m_Filename);
        FOS.Close();
    }

    wxString str;

    wxFileInputStream FIS(m_Filename);
    wxFileConfig *pConfig = new wxFileConfig(FIS);
    wxConfigBase::Set(pConfig);
    pConfig->DontCreateOnDemand();
    pConfig->Read(_("/GLOBAL/FirstRun"), &GLOBAL.FirstRun, true);
    pConfig->Read(_("/GLOBAL/ReoccuringSet"), &GLOBAL.ReoccuringSet);
    pConfig->Read(_("/GLOBAL/HideReconciledTransactionsOnStart"), &GLOBAL.HideReconciledTransactionsOnStart);
    pConfig->Read(_("/GLOBAL/Language"), &GLOBAL.Language, 0);
    if (pConfig->Read(_("/COLORS/MainBGColor"), &str) )
    {
        wxFromString(str, &COLORS.MainBGColor);
    }
    if (pConfig->Read(_("/COLORS/DialogBGColor"), &str) )
    {
        wxFromString(str, &COLORS.DialogBGColor);
    }
    pConfig->Read(_("/WARNINGS/IsCategory"), &WARNINGS.IsCategory, true);

    return true;
}

bool COptions::SaveOptions()
{
    wxFileStream FOS(m_Filename);
    wxFileConfig *pConfig = (wxFileConfig*)wxConfigBase::Get();

    pConfig->Write(_("/GLOBAL/FirstRun"), GLOBAL.FirstRun);
    pConfig->Write(_("/GLOBAL/ReoccuringSet"), GLOBAL.ReoccuringSet);
    pConfig->Write(_("/GLOBAL/HideReconciledTransactionsOnStart"), GLOBAL.HideReconciledTransactionsOnStart);
    pConfig->Write(_("/GLOBAL/Language"), GLOBAL.Language);
    pConfig->Write(_("/COLORS/MainBGColor"), wxToString(COLORS.MainBGColor) );
    pConfig->Write(_("/COLORS/DialogBGColor"), wxToString(COLORS.DialogBGColor) );
    pConfig->Write(_("/WARNINGS/IsCategory"), WARNINGS.IsCategory);

    pConfig->Save(FOS);
    return true;

}

bool COptions::RestoreDefaults()
{
    GLOBAL.HideReconciledTransactionsOnStart = m_DefaultHideReconciledTransactionsOnStart;
    GLOBAL.Language = m_DefaultLanguage;
    COLORS.MainBGColor = m_DefaultMainBGColor;
    COLORS.DialogBGColor = m_DefaultDialogBGColor;
    WARNINGS.IsCategory = true;

    return true;
}


}; // end namespace ace

