/**
    File        : Dialogs.cpp
    Description : Common Dialogs
    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 "Dialogs.h"

int ace::MsgBox(wxWindow* pParent, ace::MB_TYPE Type, wxString Msg, bool* pChecked, wxString FileName, int LineNumber)
{
    int RetValue = 0;
    CMsgDlg dlg;
    if (dlg.Create(pParent, Type, Msg, pChecked) )
        RetValue = dlg.ShowModal();
    if (Type == MB_ERROR)
        ace::LogError(Msg, FileName, LineNumber, false);
    if (Type == MB_FAIL)
        ace::LogError(Msg, FileName, LineNumber, true);

    return RetValue;
}

void ace::LogError(wxString msg, wxString FileName, int LineNumber, bool IsFatal)
{
    std::ofstream File;
    File.open("ErrorLog.txt", std::ios::out | std::ios::app);
    if (File.is_open() )
    {
        wxString AppVersion = wxString::Format("Version %u.%u.%u  Revision %u",
                                               AutoVersion::MAJOR,
                                               AutoVersion::MINOR,
                                               AutoVersion::BUILD,
                                               AutoVersion::REVISION);
        File << (GetTimeStamp() ).c_str() << " ; ";
        File << "File: " << FileName.c_str() << " ; ";
        File << "Line: " << LineNumber << "\n";
        File << AppVersion.c_str() <<"\n";
        (IsFatal) ? File << "*** FATAL ERROR ***: " : File << "Error Message: ";
        File << msg.c_str() << "\n\n";
        File.close();
    }
    if (IsFatal)
    {
        wxWindow* win = wxWindow::FindWindowByName("CMainFrame");
        if (win)
            win->Close(true);
    }
}


ace::CMsgDlg::CMsgDlg()
: m_pIsChecked(NULL), m_pCheckBox(NULL), m_idCheckBox(0)
{

}

ace::CMsgDlg::~CMsgDlg()
{

}

bool ace::CMsgDlg::Create(wxWindow* pParent, ace::MB_TYPE Type, wxString& Msg, bool* pChecked)
{
    m_pIsChecked = pChecked;

    if (m_pIsChecked)
    {
        if (*m_pIsChecked)
        {
            return false;
        }
    }

    // load the dialog from the resource
    wxString name;
    switch (Type)
    {
    case ace::MB_WARNING:
        name = _("aceWarningMsgBox");
        break;

    case ace::MB_INFO:
        name = _("aceInfoMsgBox");
        break;

    case ace::MB_ERROR:
    case ace::MB_FAIL:
        name = _("aceErrorMsgBox");
        break;

    case ace::MB_QUESTION:
        name = _("aceQuestionMsgBox");
        break;

    default:
        wxFAIL_MSG(_("Bad Dialog Type ID") );
    }

    if (!wxXmlResource::Get()->LoadDialog(this, pParent, name) )
    {
        ace::LogError("Could not load Dialogs resource", ERRORCODE, true);
    }

// TODO (USER#1#): Change hieght of Message Box based on size of text (e.g. Account Details box from CAccount)
    // set the text of the message box
    wxWindow* pText = wxWindow::FindWindowByLabel(_("LABEL"), this);
    wxASSERT(pText);
    ( (wxStaticText*)pText)->SetLabel(Msg);

    // get the checkbox and its ID if there is one
    if (m_pIsChecked)
    {
        m_pCheckBox = (wxCheckBox*)wxWindow::FindWindowByName(name, this);
        wxASSERT(m_pCheckBox);
        m_idCheckBox = m_pCheckBox->GetId();
    }

    this->Fit();

    // add event handlers if needed
    if (Type == ace::MB_WARNING)
    {
        Connect(wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED,
                wxCommandEventHandler(ace::CMsgDlg::OnOK) );
        Connect(wxID_CANCEL, wxEVT_COMMAND_BUTTON_CLICKED,
                wxCommandEventHandler(ace::CMsgDlg::OnCancel) );
    }
    if (Type == ace::MB_INFO || Type == ace::MB_ERROR || Type == ace::MB_FAIL)
    {
        Connect(wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED,
                wxCommandEventHandler(ace::CMsgDlg::OnOK) );
    }
    if (Type == ace::MB_QUESTION)
    {
        Connect(wxID_YES, wxEVT_COMMAND_BUTTON_CLICKED,
                wxCommandEventHandler(ace::CMsgDlg::OnYes) );
        Connect(wxID_NO, wxEVT_COMMAND_BUTTON_CLICKED,
                wxCommandEventHandler(ace::CMsgDlg::OnNo) );
    }
    if (m_pIsChecked)
    {
        Connect(m_idCheckBox, wxEVT_COMMAND_CHECKBOX_CLICKED,
                wxCommandEventHandler(ace::CMsgDlg::OnRadioButtonSelect) );
    }

    return true;
}

void ace::CMsgDlg::OnRadioButtonSelect(wxCommandEvent &event)
{
    (*m_pIsChecked) = m_pCheckBox->GetValue();
}

void ace::CMsgDlg::OnOK(wxCommandEvent &event)
{
    EndModal(wxID_OK);
}

void ace::CMsgDlg::OnCancel(wxCommandEvent &event)
{
    EndModal(wxID_CANCEL);
}

void ace::CMsgDlg::OnYes(wxCommandEvent &event)
{
    EndModal(wxID_YES);
}

void ace::CMsgDlg::OnNo(wxCommandEvent &event)
{
    EndModal(wxID_NO);
}

