#include "exportwidget.h"
#include "ui_exportwidget.h"
#include "models/lotrodatmanager.h"

#include <QFileDialog>
#include <QDebug>

ExportWidget::ExportWidget(LotroDatManager* mgr, QSettings* settings, QWidget *parent) :
    QWidget(parent), lotro_manager(mgr), settings(settings),
    ui(new Ui::ExportWidget)
{
    ui->setupUi(this);
    ui->singlefile_radio_common->click();
}

ExportWidget::~ExportWidget()
{
    delete ui;
}

void ExportWidget::addConnections()
{
    connect(lotro_manager, &LotroDatManager::processStarted, this, &ExportWidget::on_lotromanager_process_started, Qt::BlockingQueuedConnection);
    connect(lotro_manager, &LotroDatManager::processFinished, this, &ExportWidget::on_lotromanager_process_finished, Qt::BlockingQueuedConnection);
}

void ExportWidget::on_lotromanager_process_started(QString, QVector<QVariant>)
{
    ui->extract_button_common->setEnabled(false);
    ui->filesbytype_radio_common->setEnabled(false);
    ui->filetype_dds_common->setEnabled(false);
    ui->filetype_fontbin_common->setEnabled(false);
    ui->filetype_jpg_common->setEnabled(false);
    ui->filetype_ogg_common->setEnabled(false);
    ui->filetype_txt_common->setEnabled(false);
    ui->filetype_unknown_common->setEnabled(false);
    ui->filetype_wav_common->setEnabled(false);
    ui->singlefile_radio_common->setEnabled(false);
    ui->single_file_id_common->setEnabled(false);
    ui->target_database_radio_common->setEnabled(false);
    ui->target_files_radio_common->setEnabled(false);
}

void ExportWidget::on_lotromanager_process_finished(QString, QVector<QVariant>)
{
    ui->extract_button_common->setEnabled(true);
    ui->filesbytype_radio_common->setEnabled(true);
    ui->filetype_dds_common->setEnabled(true);
    ui->filetype_fontbin_common->setEnabled(true);
    ui->filetype_jpg_common->setEnabled(true);
    ui->filetype_ogg_common->setEnabled(true);
    ui->filetype_txt_common->setEnabled(true);
    ui->filetype_unknown_common->setEnabled(true);
    ui->filetype_wav_common->setEnabled(true);
    ui->singlefile_radio_common->setEnabled(true);
    ui->single_file_id_common->setEnabled(true);
    ui->target_database_radio_common->setEnabled(true);
    ui->target_files_radio_common->setEnabled(true);

    if (ui->singlefile_radio_common->isChecked())
        emit ui->singlefile_radio_common->click();
    if (ui->filesbytype_radio_common->isChecked())
        emit ui->filesbytype_radio_common->click();

}

void ExportWidget::on_singlefile_radio_common_clicked()
{
    ui->single_file_id_common->setEnabled(true);
    ui->filetype_dds_common->setDisabled(true);
    ui->filetype_fontbin_common->setDisabled(true);
    ui->filetype_jpg_common->setDisabled(true);
    ui->filetype_ogg_common->setDisabled(true);
    ui->filetype_txt_common->setDisabled(true);
    ui->filetype_unknown_common->setDisabled(true);
    ui->filetype_wav_common->setDisabled(true);
}

void ExportWidget::on_filesbytype_radio_common_clicked()
{
    ui->single_file_id_common->setDisabled(true);
    ui->filetype_dds_common->setEnabled(true);
    ui->filetype_fontbin_common->setEnabled(true);
    ui->filetype_jpg_common->setEnabled(true);
    ui->filetype_ogg_common->setEnabled(true);
    ui->filetype_txt_common->setEnabled(true);
    ui->filetype_unknown_common->setEnabled(true);
    ui->filetype_wav_common->setEnabled(true);
}

void ExportWidget::on_extract_button_common_clicked()
{
    bool to_database = ui->target_database_radio_common->isChecked();
    bool is_single_file = ui->singlefile_radio_common->isChecked();

    LOTRO_DAT::FILE_TYPE file_type = LOTRO_DAT::FILE_TYPE::NO_TYPE;
    if (ui->filetype_dds_common->isChecked())
        file_type = LOTRO_DAT::FILE_TYPE(file_type | LOTRO_DAT::DDS);
    if (ui->filetype_fontbin_common->isChecked())
        file_type = LOTRO_DAT::FILE_TYPE(file_type | LOTRO_DAT::FONT);
    if (ui->filetype_jpg_common->isChecked())
        file_type = LOTRO_DAT::FILE_TYPE(file_type | LOTRO_DAT::JPG);
    if (ui->filetype_ogg_common->isChecked())
        file_type = LOTRO_DAT::FILE_TYPE(file_type | LOTRO_DAT::OGG);
    if (ui->filetype_txt_common->isChecked())
        file_type = LOTRO_DAT::FILE_TYPE(file_type | LOTRO_DAT::TEXT);
    if (ui->filetype_unknown_common->isChecked())
        file_type = LOTRO_DAT::FILE_TYPE(file_type | LOTRO_DAT::UNKNOWN);
    if (ui->filetype_wav_common->isChecked())
        file_type = LOTRO_DAT::FILE_TYPE(file_type | LOTRO_DAT::WAV);

    long long file_id = ui->single_file_id_common->text().toLongLong();



    if (is_single_file && !to_database) {
        QString fileName = QFileDialog::getSaveFileName(this->parentWidget(), "Сохранение файла",
                                   settings->value("advanced/export_path", "").toString(), "Все файлы (*)");
        if (fileName.isEmpty())
                return;

        settings->setValue("advanced/export_path", fileName.left(std::max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'))));
        settings->sync();

        qDebug() << "Saving export path to " << settings->value("advanced/export_path", "").toString();

        QMetaObject::invokeMethod(lotro_manager, "extractSingleFile",
                                  Qt::QueuedConnection, Q_ARG(QString, fileName),
                                  Q_ARG(long long, file_id));
    }

    if (is_single_file && to_database) {
        QString fileName = QFileDialog::getSaveFileName(this->parentWidget(), "Сохранение файла",
                                   settings->value("advanced/export_path", "").toString(), "База данных (*.db);;Все файлы (*)");
        if (fileName.isEmpty())
                return;

        settings->setValue("advanced/export_path", fileName.left(std::max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'))));
        settings->sync();

        qDebug() << "Saving export path to " << settings->value("advanced/export_path", "").toString();

        QMetaObject::invokeMethod(lotro_manager, "extractSingleFileToDatabase",
                                  Qt::QueuedConnection, Q_ARG(QString, fileName),
                                  Q_ARG(long long, file_id));
    }

    if (!is_single_file && !to_database) {
        QString folderName = QFileDialog::getExistingDirectory(this->parentWidget(), "Выбор папки для экспорта",
                                   settings->value("advanced/export_path", "").toString(), QFileDialog::ShowDirsOnly) + "/";
        if (folderName.isEmpty())
                return;

        settings->setValue("advanced/export_path", folderName);
        settings->sync();

        qDebug() << "Saving export path to " << settings->value("advanced/export_path", "").toString();


        QMetaObject::invokeMethod(lotro_manager, "extractGrouppedFiles",
                                  Qt::QueuedConnection, Q_ARG(QString, folderName),
                                  Q_ARG(LOTRO_DAT::FILE_TYPE, file_type));
    }

    if (!is_single_file && to_database) {
        QString fileName = QFileDialog::getSaveFileName(this->parentWidget(), "Сохранение файла",
                                   settings->value("advanced/export_path", "").toString(), "База данных (*.db);;Все файлы (*)");
        if (fileName.isEmpty())
                return;

        settings->setValue("advanced/export_path", fileName.left(std::max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'))));
        settings->sync();

        qDebug() << "Saving export path to " << settings->value("advanced/export_path", "").toString();

        QMetaObject::invokeMethod(lotro_manager, "extractGrouppedFilesToDatabase",
                                  Qt::QueuedConnection, Q_ARG(QString, fileName),
                                  Q_ARG(LOTRO_DAT::FILE_TYPE, file_type));
    }
}
