/**
    File        : AccountManager.h
    Description : Manages all user accounts
    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/>.
**/

#ifndef ACCOUNTMANAGER_H_INCLUDED
#define ACCOUNTMANAGER_H_INCLUDED

#include <wx/Notebook.h>
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/arrstr.h>
//#include <wx/file.h>
//#include <wx/ffile.h>
//#include <wx/tokenzr.h>
//#include <wx/sstream.h>
//#include <wx/wfstream.h>
//#include <wx/txtstrm.h>
#include <wx/datectrl.h>
#include <wx/valtext.h>
#include <wx/valgen.h>
#include <wx/statline.h>
#include <wx/combobox.h>
#include <wx/listbox.h>
#include <wx/stdpaths.h>
#include <vector>
#include "IPlugin.h"
#include "Data.h"
#include "File.h"
#include "Account.h"
#include "NewAccountWizard.h"
#include "Dialogs.h"

class CAccountManager : public wxNotebook, public CAccountManagerBase
{
    public:
    CAccountManager(wxWindow* pParent, IPluginHost* pHost);
    ~CAccountManager();

    int LoadAccountsFromDisk();
    bool RecordTransaction(STransaction T);
    bool SaveAccountsToDisk();
    void ResetAccountCounter() { m_iterCurrentAccount = m_AccountList.begin(); }
    bool HasMoreAccounts();
    void TransferFunds(const STransaction FromTransaction);
    void SetLastTransferNumber(ULONG num)     { m_LastTransferNumber = num; }
    ULONG SelectAccount(wxString* pToAccount, wxString Msg);
    void SetPageLabel(size_t& PageNum, wxString& Label);
    size_t GetNewPageNum()                    { return ++m_LastPageNumAssigned; }

    // getters
    CAccount* GetAccountByName(wxString AccountNameToGet);
    const CAccount* GetNextAccount();
    const wxArrayString& GetAccountNames()    { return m_AccountNames; }
    const int GetNumberOfAccounts() const     { return m_AccountList.size(); }
    const CDataBase* GetGlobalDataObject()    { return m_pHost->GetGlobalDataObject(); }
    const ULONG GetNextTransferNumber()       { return ++m_LastTransferNumber; }
    const ULONG GetLastTransferNumber() const { return m_LastTransferNumber; }

    // event handlers
    void OnNewAccount(wxCommandEvent& event);
    void OnDeleteAccount(wxCommandEvent& event);
    void OnEditAccount(wxCommandEvent& event);

    private:
    // id's
    enum
    {
        idAccountManagerWindow = 200,
    };

    // child controls

    // private data
    wxWindow*                         m_pParent;
    IPluginHost*                      m_pHost;
    int                               m_NumAccountsLoaded;
    std::vector<CAccount*>            m_AccountList;
    std::vector<CAccount*>::iterator  m_iterCurrentAccount;
    wxArrayString                     m_AccountNames;
    ULONG                             m_LastTransferNumber;
    size_t                            m_LastPageNumAssigned;
    wxString                          m_AccountListFilename;
};

class CStartPage : public wxPanel
{
    public:
    CStartPage(wxWindow* pParent);
    ~CStartPage() { }
    void OnPaint(wxPaintEvent& paint_event);
};

class CNewAccountDlg : public wxDialog
{
    public:
    CNewAccountDlg(CAccountManager* pParent, IPluginHost* pHost, SAccountInfo* pAI);
    ~CNewAccountDlg() { }

    private:
    // id's
    enum
    {
        idAccountNameCtrl = 210,
        idAccountNumberCtrl,
        idAccountTypeCtrl,
        idBankNameCtrl,
        idBankPhoneNumberCrtl,
        idDateOpenedCtrl,
        idStartingBalanceCtrl,
    };

    // event handlers
    void OnSave(wxCommandEvent& event);
    void OnCancel(wxCommandEvent& event);

    // child controls
    wxDatePickerCtrl*   m_pDateOpenedCtrl;

    // private data
    CAccountManager*    m_pParent;
    IPluginHost*        m_pHost;
    SAccountInfo*       m_pAI;
    wxString            m_StartingBalance;
    wxString            m_DateOpened;
};

class CSelectAccountDlg : public wxDialog
{
    public:
    CSelectAccountDlg(CAccountManager* pParent, wxString Msg, wxArrayString AccountNames, wxString* pAccountName);
    ~CSelectAccountDlg() { }

    private:
    // id's
    enum
    {
        idAccountListCtrl = 220,
    };

    // event handlers
    void OnOK(wxCommandEvent& event);
    void OnCancel(wxCommandEvent& event);

    // child controls
    wxListBox*           m_pAccountListCtrl;

    // private data
    CAccountManager*     m_pParent;
    wxString*            m_pAccountName;
};

#endif // ACCOUNTMANAGER_H_INCLUDED
