#include "main_window.hh"
#include "ui_main_window.h"
#include "candle_chart_model.hh"
#include "base_utility.hh"
#include <QFileDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui_(new Ui::MainWindow)
{
    ui_->setupUi(this);

    helpDialog = new HelpDialog(this);

    connect(ui_->inputFileButton, SIGNAL(pressed()),
            this, SLOT(inputFileButtonPressed()));

    connect(ui_->outputFolderButton, SIGNAL(pressed()),
            this, SLOT(outputFolderButtonPressed()));

    for (auto& scale : chartScales) {
        if (scale == ChartScale::NONE) continue;
        ui_->outputChartScaleBox->addItem(chartScaleToString(scale));
        ui_->outputSplitPeriodBox->addItem(chartScaleToString(scale));
    }

    ui_->outputSplitPeriodBox->addItem("NONE");

    for (auto& pair : currencyPairs) {
        if (pair == CurrencyPair::NONE) continue;
        ui_->outputPairBox->addItem(currencyPairToString(pair));        
    }

    connect(ui_->outputPairBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(currencyPairSelected()));

    connect(ui_->outputChartScaleBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(chartScaleSelected()));

    ui_->inputFormatEdit->setText(defaultFormat);
    ui_->inputSeparatorEdit->setText(defaultSeparator);
    ui_->outputSplitPeriodBox->setCurrentIndex(chartScales.size()-1);
    ui_->outputChartScaleBox->setCurrentIndex(1);
    ui_->outputPairBox->setCurrentIndex(0);
}

MainWindow::~MainWindow()
{
    delete ui_;
}

void MainWindow::on_actionExit_triggered()
{
    close();
}

void MainWindow::on_actionConvert_triggered()
{
    FormatConvertor convertor;

    convertor.inputFile(ui_->inputFileEdit->text());
    convertor.inputFormat(ui_->inputFormatEdit->text());

    convertor.outputFolder(ui_->outputFolderEdit->text());
    convertor.outputPrefix(ui_->outputPrefixEdit->text());
    convertor.outputSufix(ui_->outputSufixEdit->text());

    convertor.currencyPair(currencyPairs[ui_->outputPairBox->currentIndex()+1]);
    convertor.chartScale(chartScales[ui_->outputChartScaleBox->currentIndex()]);
    convertor.splitPeriod(chartScales[ui_->outputSplitPeriodBox->currentIndex()]);

    QString error;
    if (convertor.convert(error))
        QMessageBox::information(this, tr("Success"), tr("Conversion was successful."));
    else
        QMessageBox::critical(this, tr("Conversion error"), error);
}

void MainWindow::on_actionHelp_triggered()
{
    helpDialog->show();
}

void MainWindow::on_actionAbout_triggered()
{
    QMessageBox::about(
        this, tr("About data convertor"),
        tr("Forex historical data format convertor (v0.0-devel)\n\n"
           "By Petr Kacer <kacerpetr@gmail.com>")
    );
}

void MainWindow::inputFileButtonPressed()
{
    QString inputFile = QFileDialog::getOpenFileName(this, tr("Select input file"));
    if (!inputFile.isEmpty()) ui_->inputFileEdit->setText(inputFile);
}
void MainWindow::outputFolderButtonPressed()
{
    QString folder = QFileDialog::getExistingDirectory(this, tr("Select output folder"));
    if (!folder.isEmpty()) ui_->outputFolderEdit->setText(folder);
}

void MainWindow::chartScaleSelected()
{
    setOutputPrefix();
}

void MainWindow::currencyPairSelected()
{
    setOutputPrefix();
}

void MainWindow::setOutputPrefix()
{
    ChartScale scale = chartScales[ui_->outputChartScaleBox->currentIndex()];
    CurrencyPair pair = currencyPairs[ui_->outputPairBox->currentIndex()+1];
    QString prefix = createOutputPrefix(scale, pair);
    ui_->outputPrefixEdit->setText(prefix);
}
