/*
  Copyright 2010-2011 Grégory Soutadé
  This file is part of KissCount.
  KissCount 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.
  KissCount 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 KissCount.  If not, see .
*/
#include "GenerateDialog.h"
enum {BUTTON_OK_ID=1, BUTTON_CANCEL_ID, YEAR_FROM_ID, MONTH_FROM_ID, YEAR_TO_ID, MONTH_TO_ID};
BEGIN_EVENT_TABLE(GenerateDialog, wxDialog)
EVT_BUTTON(BUTTON_OK_ID, GenerateDialog::OnOK)
EVT_BUTTON(BUTTON_CANCEL_ID, GenerateDialog::OnCancel)
EVT_CHOICE(YEAR_FROM_ID, GenerateDialog::OnYearFromChange)
EVT_CHOICE(YEAR_TO_ID, GenerateDialog::OnYearToChange)
END_EVENT_TABLE()
GenerateDialog::GenerateDialog(KissCount* kiss, wxUI *parent, int month, int year) : wxDialog(&(*parent), wxID_ANY, _("Generate month")), _kiss(kiss), _wxUI(parent)
{
    wxGridBagSizer *gridBagSizer;
    wxStaticText* label;
    std::map >::iterator it;
    int i, a, toSelect=-1;
    wxDateTime curDate;
    wxCommandEvent event;
    std::vector::iterator monthIt;
    wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
    gridBagSizer = new wxGridBagSizer(4, 5);
    curDate.SetToCurrent();
    label = new wxStaticText(this, wxID_ANY, _("From "));
    gridBagSizer->Add(label, wxGBPosition(0, 0));
    _yearFrom = new wxChoice(this, YEAR_FROM_ID);
    gridBagSizer->Add(_yearFrom, wxGBPosition(0, 1));
    _monthFrom = new wxChoice(this, MONTH_FROM_ID);
    gridBagSizer->Add(_monthFrom, wxGBPosition(0, 2));
    label = new wxStaticText(this, wxID_ANY, _("To "));
    gridBagSizer->Add(label, wxGBPosition(1, 0));
    _yearTo = new wxChoice(this, YEAR_TO_ID);
    gridBagSizer->Add(_yearTo, wxGBPosition(1, 1));
    _monthTo = new wxChoice(this, MONTH_TO_ID);
    gridBagSizer->Add(_monthTo, wxGBPosition(1, 2));
    wxButton* ok = new wxButton(this, BUTTON_OK_ID, _("OK"));
    wxButton* cancel = new wxButton(this, BUTTON_CANCEL_ID, _("Cancel"));
    gridBagSizer->Add(ok, wxGBPosition(3, 3));
    gridBagSizer->Add(cancel, wxGBPosition(3, 4));
    _ops = _kiss->GetAllOperations();
    _yearFrom->Append(wxT(""));
    _monthFrom->Append(wxT(""));
    for(i=1, it = _ops.begin(); it != _ops.end(); it++, i++)
    {
        _yearFrom->Append(wxString::Format(wxT("%d"), it->first));
        if (year == it->first)
            toSelect = i;
    }
    if (toSelect != -1)
    {
        _yearFrom->Select(toSelect);
        OnYearFromChange(event);
        toSelect=0;
        if (month != -1)
	{
            for(i=0; i<(int)_monthFrom->GetCount(); i++)
	    {
                if (_monthFrom->GetString(i) == months[month])
		{
                    toSelect = i;
                    break;
		}
	    }
	}
        _monthFrom->Select(toSelect);
    }
    else
    {
        _yearFrom->Select(0);
        OnYearFromChange(event);
    }
    for(i=2000; i<=2050; i++)
        _yearTo->Append(wxString::Format(wxT("%d"), i));
    if (year == -1)
    {
        _yearTo->Select(curDate.GetYear()-2000);
        OnYearToChange(event);
        _monthTo->Select(curDate.GetMonth()-1);
    }
    else
    {
        if (month == 11)
	    year++;
	_yearTo->Select(year-2000);
        OnYearToChange(event);
        if (month == -1)
            _monthTo->Select(0);
        else
	{
            for(a=0, i=0, monthIt=_ops[year].begin(); monthIt!=_ops[year].end() && i<12; i++)
	    {
                if (i != *monthIt) 
		{
                    a++;
                    continue;
		}
                if (*monthIt > month)
                    break;
                monthIt++;
	    }
            _monthTo->Select(a);
	}
    }
    hbox->Add(gridBagSizer, 0, wxGROW|wxALL, 10);
    SetSizer(hbox);
    Fit();
    Center();
}
void GenerateDialog::OnYearFromChange(wxCommandEvent& event)
{
    wxString years = _yearFrom->GetString(_yearFrom->GetCurrentSelection());
    int year;
    std::vector::iterator it;
    _monthFrom->Clear();
  
    if (!years.Length()) 
    {
        _monthFrom->Append(wxT(""));
        return;
    }
    year = wxAtoi(years);
    for(it=_ops[year].begin(); it!=_ops[year].end(); it++)
        _monthFrom->Append(months[*it]);
    _monthFrom->Select(0);
    Layout();
}
void GenerateDialog::OnYearToChange(wxCommandEvent& event)
{
    int year, i, ok;
    std::vector::iterator it;
    _monthTo->Clear();
  
    year = wxAtoi(_yearTo->GetString(_yearTo->GetCurrentSelection()));
    for (i=0; i<12; i++)
    {
        ok = 1;
        for(it=_ops[year].begin(); it!=_ops[year].end(); it++)
	{
            if (*it == i)
	    {
                ok=0; break;
	    }
	}
        if (ok)
            _monthTo->Append(months[i]);
    }
    _monthTo->Select(0);
    Layout();
}
void GenerateDialog::OnOK(wxCommandEvent& event)
{  
    int monthFrom, yearFrom, monthTo, yearTo, i;
    if (!_yearFrom->GetString(_yearFrom->GetCurrentSelection()).Length())
    {
        monthFrom = -1;
        yearFrom = -1;
    }
    else
    {
        for (i=0; i<12; i++)
	{
            if (months[i] == _monthFrom->GetString(_monthFrom->GetCurrentSelection()))
	    {
                monthFrom = i;
                break;
	    }
	}
        yearFrom = wxAtoi(_yearFrom->GetString(_yearFrom->GetCurrentSelection()));
    }
    for (i=0; i<12; i++)
    {
        if (months[i] == _monthTo->GetString(_monthTo->GetCurrentSelection()))
	{
            monthTo = i;
            break;
	}
    }
    yearTo = wxAtoi(_yearTo->GetString(_yearTo->GetCurrentSelection()));
    Close();
    _kiss->GenerateMonth(monthFrom, yearFrom, monthTo, yearTo);
}
void GenerateDialog::OnCancel(wxCommandEvent& event)
{
    Close();
}