/**
    File        : ReoccuringTransactions.h
    Description : Lists and manages reoccuring transactions
    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 REOCCURINGTRANSACTIONS_H_INCLUDED
#define REOCCURINGTRANSACTIONS_H_INCLUDED

#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/choicdlg.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/textctrl.h>
#include <wx/statline.h>
#include <wx/stattext.h>
#include <wx/radiobox.h>
#include <wx/combobox.h>
#include <wx/listbox.h>
#include <wx/button.h>
#include <wx/dialog.h>
#include <wx/dcclient.h>
#include <wx/scrolwin.h>
#include <wx/menu.h>
#include <wx/debug.h>
#include <vector>
#include <algorithm>
#include "IPlugin.h"
#include "Base.h"
#include "Data.h"
#include "Dialogs.h"
#include "File.h"

class CReoccuringTransactionListDlg;

class CHeaderPanel : public wxPanel
{
    public:
    CHeaderPanel(wxWindow* pParent, wxWindowID id);
    ~CHeaderPanel();

    private:
    // id's

    // event handlers
    void OnPaint(wxPaintEvent& paint_event);

    // private data
    wxWindow*     m_pParent;
};

class CTransactionListPanel : public wxScrolledWindow
{
    public:
    CTransactionListPanel(wxWindow* pParent, CReoccuringTransactionListDlg* pParentDlg, wxWindowID id);
    ~CTransactionListPanel();
    void ResetCurrentTransaction()             { m_pCurrentTransaction = NULL; }

    private:
    void SetScrollPosition();
    bool HitTest(wxMouseEvent& event, /*ULONG& RowNumber, */SReoccuringTransaction** ppTransaction);

    // id's
    enum
    {
        idEditTransaction = 8000,
        idDeleteTransaction,
        idRecordTransaction,
    };

    // event handlers
    void OnDraw(wxDC &DC);
    void OnTransactionLeftDoubleClick(wxMouseEvent& event);
    void OnTransactionLeftClick(wxMouseEvent& event);
    void OnTransactionRightClick(wxMouseEvent& event);
    void OnViewTransaction(wxCommandEvent& event);
    void OnDeleteTransaction(wxCommandEvent& event);
    void OnRecordTransaction(wxCommandEvent& event);

    // private data
    wxWindowID                            m_ID;
    wxWindow*                             m_pParent;
    CReoccuringTransactionListDlg*        m_pParentDlg;
    SReoccuringTransaction*               m_pCurrentTransaction;
    ULONG                                 m_yCount;
    std::map<ULONG, ULONG>                m_LineMap;
    int                                   m_LinesPerScreen;
    int                                   m_LineHeight;

};

class CReoccuringTransactionListDlg : public wxDialog
{
    public:
    CReoccuringTransactionListDlg(wxWindow* pParent, CAccountManagerBase* pAM);
    ~CReoccuringTransactionListDlg();
    bool SaveTransaction(SReoccuringTransaction& Transaction);
    bool AddNewTransaction(SReoccuringTransaction& Transaction);
    bool DeleteTransaction(ULONG TransactionNumber);
    size_t GetNumberOfTransactions()                       { return m_vTransactionList.size(); }
    SReoccuringTransaction& GetCurrentTransaction()        { return *m_iCurrentTransaction; }
    bool RecordNewTransaction(STransaction& p)             { return m_pAM->RecordTransaction(p); }
    ULONG GetNextTransactionNumber()                       { return ++m_LastTransactionNumber; }
    const wxArrayString GetAccountNames()                  { return m_AccountNames; }
    SReoccuringTransaction* GetTransaction(const ULONG TransactionNumber);
    void UpdateTransaction(SReoccuringTransaction* pTransaction);
    void DeleteCurrentTransaction();
//    void RefreshAllTransactionLists();
    void SortTransactions();
    void ResetTransactionCounter()                         { m_iCurrentTransaction = m_vTransactionList.begin(); }
    bool HasMoreTransactions();
    const SReoccuringTransaction& GetNextTransaction();
    CAccountBase* GetAccountByName(wxString Name)          { return m_pAM->GetAccountByName(Name); }

    // event handlers
    void OnNewTransaction(wxCommandEvent &event);
    void OnRecordTransaction(wxCommandEvent& event);
    void OnExit(wxCommandEvent &event);

    private:
    bool LoadTransactionsFromDisk();
    bool SaveTransactionsToDisk();

    // id's
    enum
    {
        idAddBtn = 8050,
        idViewBtn,
        idDeleteBtn,
        idExitBtn,
        idDebitListPanel,
        idDepositListPanel,
        idTransferListPanel,
    };

    // controls
    CTransactionListPanel*                          m_pTransactionListPanel;

    // private data
    wxWindow*                                       m_pParent;
    CAccountManagerBase*                            m_pAM;
    std::vector<SReoccuringTransaction>             m_vTransactionList;
    std::vector<SReoccuringTransaction>::iterator   m_iCurrentTransaction;
    ULONG                                           m_LastTransactionNumber;
    wxArrayString                                   m_AccountNames;
    wxString                                        m_Filename;
};

class CViewReoccuringTransactionDlg : public wxDialog
{
    public:
    CViewReoccuringTransactionDlg(wxWindow* pParent, SReoccuringTransaction& Transaction,
                                  wxArrayString AccountNames, const bool IsNew, const bool CanEdit);
    ~CViewReoccuringTransactionDlg();
    void ShowControls(const ULONG type);
    virtual bool TransferDataFromWindow();

    private:
    // id's
    enum
    {
        idRadioBoxCtrl = 8075,
        idNameCtrl,
        idFrequencyCtrl,
        idLastDateCtrl,
        idNextDateCtrl,
        idLastAmountCtrl,
        idEstAmountCtrl,
        idPayFromAccountCtrl,
        idDepositToAccountCtrl,
        idCategoryCtrl,
        idSubCategoryCtrl
    };

    // event handlers
    void OnSave(wxCommandEvent &event);
    void OnCategorySelection(wxCommandEvent &event);
    void OnRadioButtonSelect(wxCommandEvent &event);

    // controls
    wxRadioBox*        m_pRadioBox;
    wxTextCtrl*        m_pNameCtrl;
    wxComboBox*        m_pFrequencyCtrl;
    wxDatePickerCtrl*  m_pLastDateCtrl;
    wxDatePickerCtrl*  m_pNextDateCtrl;
    wxTextCtrl*        m_pLastAmountCtrl;
    wxTextCtrl*        m_pEstAmountCtrl;
    wxComboBox*        m_pPayFromAccountCtrl;
    wxComboBox*        m_pDepositToAccountCtrl;
    wxComboBox*        m_pCategoryCtrl;
    wxComboBox*        m_pSubCategoryCtrl;

    // private data
    wxWindow*                       m_pParent;
    wxArrayString                   m_pAccountNames;
    SReoccuringTransaction*         m_pTransaction;
    bool                            m_IsNew;
    bool                            m_CanEdit;
    wxString                        m_LastRadioBoxSelection;
    wxString                        m_EstAmount;
    wxString                        m_Frequency;
};

#endif // REOCCURINGTRANSACTIONS_H_INCLUDED
