/** Return the rate for a specific currency in a specific day*/
double Model_CurrencyHistory::getDayRate(const int& currencyID, const wxString& DateISO)
{
    if (currencyID == Model_Currency::GetBaseCurrency()->CURRENCYID)
        return 1;

    wxDateTime Date;
    Date.ParseDate(DateISO);
    Model_CurrencyHistory::Data_Set Data = Model_CurrencyHistory::instance().find(Model_CurrencyHistory::CURRENCYID(currencyID),Model_CurrencyHistory::CURRDATE(Date));

    if (!Data.empty())
        return Data.back().CURRVALUE;
    else
    {
        //int Rate = 0, DaysTMP = 999, Days = 999;
        //Model_CurrencyHistory::Data_Set histData = Model_CurrencyHistory::instance().find(Model_CurrencyHistory::CURRENCYID(currencyID));
        //for (auto& hist : histData)
        //{
        //    DaysTMP = abs((Date - Model_CurrencyHistory::CURRDATE(hist)).GetDays());
        //    if (DaysTMP < Days)
        //    {
        //        Days = DaysTMP;
        //        Rate = hist.CURRVALUE;
        //    }
        //}
        //if (Rate != 0)
        //    return Rate;
        //else
        //{
        //    Model_Currency::Data* Currency = Model_Currency::instance().get(currencyID);
        //    return Currency->BASECONVRATE;
        //}
        Model_Currency::Data* Currency = Model_Currency::instance().get(currencyID);
        return Currency->BASECONVRATE;
    }
}
/** Return the last attachment number linked to a specific object */
double Model_CurrencyHistory::getLastRate(const int& currencyID)
{
    Model_CurrencyHistory::Data_Set histData = Model_CurrencyHistory::instance().find(Model_CurrencyHistory::CURRENCYID(currencyID));
    std::stable_sort(histData.begin(), histData.end(), SorterByCURRDATE());

    if (!histData.empty())
        return histData.back().CURRVALUE;
    else
    {
        Model_Currency::Data* Currency = Model_Currency::instance().get(currencyID);
        return Currency->BASECONVRATE;
    }
}
void mmMainCurrencyDialog::ShowCurrencyHistory()
{
    valueListBox_->DeleteAllItems();

    int baseCurrencyID = Option::instance().BaseCurrency();
    if (currencyID_ <= 0 || currencyID_ == baseCurrencyID)
    {
        historyButtonAdd_->Disable();
        historyButtonDelete_->Disable();
        return;
    }
    else
    {
        historyButtonAdd_->Enable();
        historyButtonDelete_->Enable();
    }      

    Model_CurrencyHistory::Data_Set histData = Model_CurrencyHistory::instance().find(Model_CurrencyHistory::CURRENCYID(currencyID_));
    std::stable_sort(histData.begin(), histData.end(), SorterByCURRDATE());
    std::reverse(histData.begin(), histData.end());
    if (!histData.empty())
    {
        int idx = 0;
        for (const auto &d : histData)
        {
            wxListItem item;
            item.SetId(idx);
            item.SetData(d.CURRHISTID);
            valueListBox_->InsertItem(item);
            const wxDate dtdt = Model_CurrencyHistory::CURRDATE(d);
            const wxString dispAmount = wxString::Format(wxT("%f"), d.CURRVALUE);
            valueListBox_->SetItem(idx, 0, mmGetDateForDisplay(dtdt));
            valueListBox_->SetItem(idx, 1, dispAmount);
            const wxString& priceAmount = wxEmptyString;
            valueListBox_->SetItem(idx, 2, priceAmount);
            idx++;
        }
        valueListBox_->RefreshItems(0, --idx);
    }
}