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

CInFile::CInFile(wxString& FileName, bool Create)
: m_FileName(FileName)
{
    m_iFile.open(m_FileName.fn_str(), std::ios::in | std::ios::binary);
}

CInFile::~CInFile()
{
    if (m_iFile.is_open() )
    {
        m_iFile.close();
    }
}

int CInFile::GetSizeOfNextRecord()
{
    if (!m_iFile.is_open() )
    {
        return -1;
    }
    int* pSize;
    char chSize[sizeof(int) ];
    m_iFile.read(chSize, sizeof(int) );
    pSize = reinterpret_cast<int*>(chSize);
    return *pSize;
}

bool CInFile::ReadObject(STransaction& Transaction)
{
    bool OK = true;

    // transaction number (ULONG)
    OK = OK && Read<ULONG>(Transaction.TransactionNumber);

    // transfer number (ULONG)
    OK = OK && Read<ULONG>(Transaction.TransferNumber);

    // account name (wxString)
    OK = OK && ReadString(Transaction.AccountName);

    // transfer account (wxString)
    OK = OK && ReadString(Transaction.TransferAccount);

    // date (wxString)
    OK = OK && ReadString(Transaction.Date);

    // type (ULONG)
    OK = OK && Read<ULONG>(Transaction.Type);

    // check number (ULONG)
    OK = OK && Read<ULONG>(Transaction.CheckNumber);

    // payee (wxString)
    OK = OK && ReadString(Transaction.Payee);

    // amount (double)
    OK = OK && Read<double>(Transaction.Amount);

    // category (wxString)
    OK = OK && ReadString(Transaction.Category);

    // sub category (wxString)
    OK = OK && ReadString(Transaction.SubCategory);

    // comment (wxString)
    OK = OK && ReadString(Transaction.Comment);

    // status (ULONG)
    OK = OK && Read<ULONG>(Transaction.Status);

    // is deposit (bool)
    OK = OK && Read<bool>(Transaction.IsDeposit);

    // balance (double)
    OK = OK && Read<double>(Transaction.Balance);

    // ImageFileNames (std::vector<wxString> )

    return OK;
}

bool CInFile::ReadObject(SReoccuringTransaction& Transaction)
{
    bool OK = true;

    // number (ULONG)
    OK = OK && Read<ULONG>(Transaction.Number);

    // name (wxString)
    OK = OK && ReadString(Transaction.Name);

    // type (ULONG)
    OK = OK && Read<ULONG>(Transaction.Type);

    // frequency (ULONG)
    OK = OK && Read<ULONG>(Transaction.Frequency);

    // next occurance (wxString)
    OK = OK && ReadString(Transaction.NextOccurance);

    // last occurance (wxString)
    OK = OK && ReadString(Transaction.LastOccurance);

    // est amount (double)
    OK = OK && Read<double>(Transaction.EstAmount);

    // last amount (double)
    OK = OK && Read<double>(Transaction.LastAmount);

    // transaction type (ULONG)
    OK = OK && Read<ULONG>(Transaction.TransactionType);

    // pay from account name (wxString)
    OK = OK && ReadString(Transaction.PayFromAccountName);

    // deposit to account name (wxString)
    OK = OK && ReadString(Transaction.DepositToAccountName);

    // category (wxString)
    OK = OK && ReadString(Transaction.Category);

    // sub category (wxString)
    OK = OK && ReadString(Transaction.SubCategory);

    return OK;
}

bool CInFile::ReadObject(SAccountInfo& ai)
{
    bool OK = true;

    // account name (wxString)
    OK = OK && ReadString(ai.AccountName);

    // account number (wxString)
    OK = OK && ReadString(ai.AccountNumber);

    // account type (wxString)
    OK = OK && ReadString(ai.AccountType);

    // bank name (wxString)
    OK = OK && ReadString(ai.BankName);

    // bank phone number (wxString)
    OK = OK && ReadString(ai.BankPhoneNumber);

    // balance (double)
    OK = OK && Read<double>(ai.Balance);

    // date opened (wxString)
    OK = OK && ReadString(ai.DateOpened);

    // opening balance (double)
    OK = OK && Read<double>(ai.OpeningBalance);

    // last statement balance (double)
    OK = OK && Read<double>(ai.LastStatementBalance);

    // last statement date (wxString)
    OK = OK && ReadString(ai.LastStatementDate);

    return OK;
}





COutFile::COutFile(wxString FileName, bool Trunc, bool Create)
: m_FileName(FileName)
{
    if (Trunc)
        m_oFile.open(m_FileName.fn_str(), std::ios::out | std::ios::binary | std::ios::trunc);
    else
        m_oFile.open(m_FileName.fn_str(), std::ios::out | std::ios::binary);
}

COutFile::~COutFile()
{
    if (m_oFile.is_open() )
    {
        m_oFile.close();
    }
}

bool COutFile::WriteObject(STransaction& Transaction)
{
    bool OK = true;

    // transaction number (ULONG)
    OK = OK && Write<ULONG>(Transaction.TransactionNumber);

    // transfer number (ULONG)
    OK = OK && Write<ULONG>(Transaction.TransferNumber);

    // account name (wxString)
    OK = OK && WriteString(Transaction.AccountName);

    // transfer account (wxString)
    OK = OK && WriteString(Transaction.TransferAccount);

    // date (wxString)
    OK = OK && WriteString(Transaction.Date);

    // transaction type (ULONG)
    OK = OK && Write<ULONG>(Transaction.Type);

    // check number (ULONG)
    OK = OK && Write<ULONG>(Transaction.CheckNumber);

    // payee (wxString)
    OK = OK && WriteString(Transaction.Payee);

    // amount (double)
    OK = OK && Write<double>(Transaction.Amount);

    // category (wxString)
    OK = OK && WriteString(Transaction.Category);

    // sub category (wxString)
    OK = OK && WriteString(Transaction.SubCategory);

    // comment (wxString)
    OK = OK && WriteString(Transaction.Comment);

    // status (ULONG)
    OK = OK && Write<ULONG>(Transaction.Status);

    // is deposit (bool)
    OK = OK && Write<bool>(Transaction.IsDeposit);

    // balance (double)
    OK = OK && Write<double>(Transaction.Balance);

    // ImageFileNames (std::vector<wxString> )

    return OK;
}

bool COutFile::WriteObject(SReoccuringTransaction& Transaction)
{
    bool OK = true;

    // number (ULONG)
    OK = OK && Write<ULONG>(Transaction.Number);

    // name (wxString)
    OK = OK && WriteString(Transaction.Name);

    // type (ULONG)
    OK = OK && Write<ULONG>(Transaction.Type);

    // frequency (ULONG)
    OK = OK && Write<ULONG>(Transaction.Frequency);

    // next occurance (wxString)
    OK = OK && WriteString(Transaction.NextOccurance);

    // last occurance (wxString)
    OK = OK && WriteString(Transaction.LastOccurance);

    // est amount (double)
    OK = OK && Write<double>(Transaction.EstAmount);

    // last amount (double)
    OK = OK && Write<double>(Transaction.LastAmount);

    // transaction type (ULONG)
    OK = OK && Write<ULONG>(Transaction.TransactionType);

    // pay from account name (wxString)
    OK = OK && WriteString(Transaction.PayFromAccountName);

    // deposit to account name (wxString)
    OK = OK && WriteString(Transaction.DepositToAccountName);

    // category (wxString)
    OK = OK && WriteString(Transaction.Category);

    // sub category (wxString)
    OK = OK && WriteString(Transaction.SubCategory);

    return OK;
}

bool COutFile::WriteObject(SAccountInfo& ai)
{
    bool OK = true;

    // account name (wxString)
    OK = OK && WriteString(ai.AccountName);

    // account number (wxString)
    OK = OK && WriteString(ai.AccountNumber);

    // account type (wxString)
    OK = OK && WriteString(ai.AccountType);

    // bank name (wxString)
    OK = OK && WriteString(ai.BankName);

    // bank phone number (wxString)
    OK = OK && WriteString(ai.BankPhoneNumber);

    // balance (double)
    OK = OK && Write<double>(ai.Balance);

    // date opened (wxString)
    OK = OK && WriteString(ai.DateOpened);

    // opening balance (double)
    OK = OK && Write<double>(ai.OpeningBalance);

    // last statement balance (double)
    OK = OK && Write<double>(ai.LastStatementBalance);

    // last statement date (wxString)
    OK = OK && WriteString(ai.LastStatementDate);

    return OK;
}
