/**
    File        : NewAccountWizard.cpp
    Description : Wizard for setting up a new account
    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 "NewAccountWizard.h"

CPage2::CPage2(wxWizard* pParent, wxString* pName, wxString* pPhone)
: wxWizardPageSimple(pParent), m_pName(pName), m_pPhone(pPhone)
{
    wxBoxSizer *pVSizer1 = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer *pHSizer1 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *pHSizer2 = new wxBoxSizer(wxHORIZONTAL);

    wxString msg = _("First, let's get some information about\nthe bank or financial institution that\nyour account is held at.\n");
    pVSizer1->Add(new wxStaticText(this, wxID_ANY, msg), 0, wxALL, 5);

    pVSizer1->AddStretchSpacer(1);

    msg = _("What is the name of the bank or financial\ninstitution that the account is held at?");
    pHSizer1->Add(new wxStaticText(this, wxID_ANY, msg), 2, wxALL, 3);
    pHSizer1->Add(new wxTextCtrl(this,
                                idBankNameCtrl,
                                *pName,
                                wxDefaultPosition,
                                wxDefaultSize,
                                0,
                                wxTextValidator(wxFILTER_ASCII, m_pName) ),
                 1, wxALL, 3);
    pVSizer1->Add(pHSizer1, 0, wxALL, 3);

    msg = _("And what is their phone number?\n(Please use this format : (xxx)xxx-xxxx )");
    pHSizer2->Add(new wxStaticText(this, wxID_ANY, msg), 2, wxALL, 3);
    pHSizer2->Add(new wxTextCtrl(this,
                                idBankPhoneNumberCrtl,
                                *pPhone,
                                wxDefaultPosition,
                                wxDefaultSize,
                                0,
                                wxTextValidator(wxFILTER_ASCII, m_pPhone) ),
                 1, wxALL, 3);
    pVSizer1->Add(pHSizer2, 0, wxALL, 3);

    this->SetSizer(pVSizer1);
}

CPage3::CPage3(wxWizard* pParent, wxString* pName, wxString* pNumber, wxString* pType, wxString* pDate, const wxArrayString AccountTypes, const bool IsNew)
: wxWizardPageSimple(pParent), m_pName(pName), m_pNumber(pNumber), m_pType(pType), m_pDate(pDate)
{
    wxBoxSizer *pVSizer = new wxBoxSizer(wxVERTICAL);
    wxBoxSizer *pHSizer1 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *pHSizer2 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *pHSizer3 = new wxBoxSizer(wxHORIZONTAL);
    wxBoxSizer *pHSizer4 = new wxBoxSizer(wxHORIZONTAL);

    wxString msg = _("Now, let's get some details for\nthe account you are creating.\n");
    pVSizer->Add(new wxStaticText(this, wxID_ANY, msg), 0, wxALL, 3);

    pVSizer->AddStretchSpacer(1);

    msg = _("What is the name of this account?");
    pHSizer1->Add(new wxStaticText(this, wxID_ANY, msg), 2, wxALL, 3);
    pHSizer1->Add(new wxTextCtrl(this,
                                 idAccountNameCtrl,
                                 *pName,
                                 wxDefaultPosition,
                                 wxDefaultSize,
                                 0,
                                 wxTextValidator(wxFILTER_ASCII, m_pName) ),
                  1, wxALL, 3);
    pVSizer->Add(pHSizer1, 0, wxALL | wxEXPAND, 3);

    msg = _("Please select an account type.");
    pHSizer2->Add(new wxStaticText(this, wxID_ANY, msg), 2, wxALL, 3);
    pHSizer2->Add(new wxComboBox(this,
                                 idAccountTypeCtrl,
                                 *pType,
                                 wxDefaultPosition,
                                 wxDefaultSize,
                                 AccountTypes,
                                 wxCB_READONLY,
                                 wxGenericValidator(m_pType) ),
                  1, wxALL | wxALIGN_RIGHT, 3);
    pVSizer->Add(pHSizer2, 0, wxALL | wxEXPAND, 3);

    msg = _("What is the account number?");
    pHSizer3->Add(new wxStaticText(this, wxID_ANY, msg), 2, wxALL, 3);
    pHSizer3->Add(new wxTextCtrl(this,
                                 idAccountNumberCtrl,
                                 *pNumber,
                                 wxDefaultPosition,
                                 wxDefaultSize,
                                 0,
                                 wxTextValidator(wxFILTER_ASCII, m_pNumber) ),
                  1, wxALL | wxALIGN_RIGHT, 3);
    pVSizer->Add(pHSizer3, 0, wxALL | wxEXPAND, 3);

    wxDateTime Date;
    if (IsNew)
        Date = wxDateTime::Today();
    else
        Date.ParseDate(*m_pDate);

    msg = _("What was the date that\nthe account was opened");
    pHSizer4->Add(new wxStaticText(this, wxID_ANY, msg), 2, wxALL, 3);
    pHSizer4->Add(new wxDatePickerCtrl(this,
                                       idDateOpenedCtrl,
                                       Date,
                                       wxDefaultPosition,
                                       wxDefaultSize,
                                       wxDP_DROPDOWN,
                                       wxDefaultValidator),
                  1, wxALL, 3);
    pVSizer->Add(pHSizer4, 0, wxALL | wxEXPAND, 3);

    this->SetSizer(pVSizer);
//    pVSizer->Fit(this);
}

bool CPage3::TransferDataFromWindow()
{
    *m_pName = ( (wxTextCtrl*)(FindWindowById(idAccountNameCtrl) ) )->GetValue();
    *m_pType = ( (wxComboBox*)(FindWindowById(idAccountTypeCtrl) ) )->GetValue();
    *m_pNumber = ( (wxTextCtrl*)(FindWindowById(idAccountNumberCtrl) ) )->GetValue();
    *m_pDate = ( ( (wxDatePickerCtrl*)(FindWindowById(idDateOpenedCtrl) ) )->GetValue() ).Format(_("%m/%d/%Y") );

    if (m_pName->IsEmpty() || m_pType->IsEmpty() || m_pNumber->IsEmpty() || m_pDate->IsEmpty() )
    {
        aceMSGBOX(this, ace::MB_INFO, _("You must enter something in all the fields!"), NULL);
        return false;
    }
    return true;
}

CPage4::CPage4(wxWizard* pParent, double* pBalance)
: wxWizardPageSimple(pParent), m_pBalance(pBalance)
{
    m_StartingBalance = wxString::Format(_("%1.2f"), *pBalance);

    wxBoxSizer *pSizer = new wxBoxSizer(wxVERTICAL);

    wxString msg = _("And finally, what is the starting\nbalance for the account?\n");
    pSizer->Add(new wxStaticText(this, wxID_ANY, msg), 0, wxALL, 3);
    pSizer->Add(new wxTextCtrl(this,
                               idStartingBalanceCtrl,
                               m_StartingBalance,
                               wxDefaultPosition,
                               wxDefaultSize,
                               0,
                               wxTextValidator(wxFILTER_NUMERIC, &m_StartingBalance) ),
                0, wxALL, 3);

    this->SetSizer(pSizer);
}

bool CPage4::TransferDataFromWindow()
{
    m_StartingBalance = ( (wxTextCtrl*)(FindWindowById(idStartingBalanceCtrl) ) )->GetValue();
    if (m_StartingBalance.ToDouble(m_pBalance) )
        return true;
    aceMSGBOX(this, ace::MB_INFO, _("Invalid dollar amount entered!"), NULL);
    return false;
}

CNewAccountWizard::CNewAccountWizard(wxWindow* pParent, IPluginHost* pHost, SAccountInfo* pAI, const bool IsNew)
: wxWizard(pParent, wxID_ANY, _("Add a New Account") ), m_pHost(pHost), m_pAI(pAI)
{
    // get available account types
    wxArrayString AccountTypes;
    pHost->GetAvailableAccountTypes(AccountTypes);

// TODO (USER#1#): add wxWizard::SetBitmap here

    SetUpPage1();
    m_pPage2 = new CPage2(this, &(pAI->BankName), &(pAI->BankPhoneNumber) );
    m_pPage3 = new CPage3(this, &(pAI->AccountName), &(pAI->AccountNumber), &(pAI->AccountType), &(pAI->DateOpened), AccountTypes, IsNew);
    m_pPage4 = new CPage4(this, &(pAI->OpeningBalance) );
    SetUpPage5();

    wxWizardPageSimple::Chain(m_pPage1, m_pPage2);
    wxWizardPageSimple::Chain(m_pPage2, m_pPage3);
    wxWizardPageSimple::Chain(m_pPage3, m_pPage4);
    wxWizardPageSimple::Chain(m_pPage4, m_pPage5);

    GetPageAreaSizer()->Add(m_pPage1);
}

void CNewAccountWizard::SetUpPage1()
{
    m_pPage1 = new wxWizardPageSimple(this);

    wxString msg = _("Welcome to the New Account Setup Wizard.\n\n");
    msg += _("This wizard will help guide you through\n");
    msg += _("the process of creating a new account.\n\n");
    msg += _("Just answer the questions on the following\n");
    msg += _("pages and AcePFA will quickly and easily\n");
    msg += _("set up your new account.\n\n");
    msg += _("To begin, click 'NEXT'");

    new wxStaticText(m_pPage1, wxID_ANY, msg, wxPoint(5,5) );
}

void CNewAccountWizard::SetUpPage5()
{
    m_pPage5 = new wxWizardPageSimple(this);

    wxString msg = _("Finished!\n\nClick 'FINISH' to begin using your\nnew account.");
    new wxStaticText(m_pPage5, wxID_ANY, msg, wxPoint(5,5) );
}

