#include "gui/exportwidget.h"
#include "ui_exportwidget.h"
#include "legacyapp.h"

#include <QFileDialog>

ExportWidget::ExportWidget(LegacyApp *_app, QWidget *parent) :
    QWidget(parent), app(_app),
    ui(new Ui::ExportWidget)
{
    ui->setupUi(this);
    ui->singlefile_radio_common->click();
    ui->folder_value_common->setText(app->properties.value("advanced/export_folder", "(не выбрана)").toString());
    ui->folder_value_common->setToolTip(app->properties.value("advanced/export_folder", "Выберите папку, куда будут помещены извлечённые файлы").toString());
}

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

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_change_folder_button_clicked()
{
    QString path = QFileDialog::getExistingDirectory(0, ("Select Output Folder"), QDir::currentPath());
    if (path.length() == 0)
        return;

    app->properties.setValue("advanced/export_folder", path);
    app->properties.sync();
    ui->folder_value_common->setText(path);
    ui->folder_value_common->setToolTip(path);
}

void ExportWidget::on_extract_button_common_clicked()
{
    QString folder = ui->folder_value_common->text();
    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);

    int file_id = ui->single_file_id_common->text().toInt();

    app->ExtractFiles(folder, to_database, is_single_file, file_id, file_type);
}
