/*
 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "projectoutputwidget.h"
#include "ui_projectoutputwidget.h"
#include "../mainwindow.h"
#include "../project.h"
#include "../iconsmanager.h"
#include "../systemconsts.h"

#include <QFileDialog>


ProjectOutputWidget::ProjectOutputWidget(const QString &name, const QString &group, QWidget *parent) :
    SettingsWidget(name,group,parent),
    ui(new Ui::ProjectOutputWidget)
{
    ui->setupUi(this);
}

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

void ProjectOutputWidget::doLoad()
{
    ui->txtOutputDir->setText(pMainWindow->project()->options().exeOutput);
    ui->txtObjOutputDir->setText(pMainWindow->project()->options().objectOutput);
    ui->grpAutosaveCompileLog->setChecked(pMainWindow->project()->options().logOutputEnabled);
    ui->txtCompileLog->setText(pMainWindow->project()->options().logOutput);
    ui->grpOverrideOutput->setChecked(pMainWindow->project()->options().overrideOutput);
    ui->txtOutputFilename->setText(pMainWindow->project()->options().overridenOutput);
}

void ProjectOutputWidget::doSave()
{
    pMainWindow->project()->options().exeOutput = ui->txtOutputDir->text();
    pMainWindow->project()->options().objectOutput = ui->txtObjOutputDir->text();
    pMainWindow->project()->options().logOutputEnabled = ui->grpAutosaveCompileLog->isChecked();
    pMainWindow->project()->options().logOutput = ui->txtCompileLog->text();
    pMainWindow->project()->options().overrideOutput = ui->grpOverrideOutput->isChecked();
    pMainWindow->project()->options().overridenOutput = ui->txtOutputFilename->text();
    pMainWindow->project()->saveOptions();
}

void ProjectOutputWidget::on_btnOutputDir_triggered(QAction *)
{
    QString dirName = QFileDialog::getExistingDirectory(
                this,
                tr("Executable output directory"),
                pMainWindow->project()->directory());
    if (!dirName.isEmpty())
        ui->txtOutputDir->setText(dirName);
}


void ProjectOutputWidget::on_btnObjOutputDir_triggered(QAction *)
{
    QString dirName = QFileDialog::getExistingDirectory(
                this,
                tr("Object files output directory"),
                pMainWindow->project()->directory());
    if (!dirName.isEmpty())
        ui->txtObjOutputDir->setText(dirName);
}


void ProjectOutputWidget::on_btnCompileLog_triggered(QAction *)
{
    QString fileName = QFileDialog::getOpenFileName(
                this,
                tr("Log file"),
                pMainWindow->project()->directory(),
                tr("All files (%1)").arg(ALL_FILE_WILDCARD));
    if (!fileName.isEmpty() ) {
        ui->txtCompileLog->setText(fileName);
    }
}

void ProjectOutputWidget::updateIcons(const QSize &size)
{
    pIconsManager->setIcon(ui->btnCompileLog, IconsManager::ACTION_FILE_OPEN_FOLDER);
    pIconsManager->setIcon(ui->btnObjOutputDir, IconsManager::ACTION_FILE_OPEN_FOLDER);
    pIconsManager->setIcon(ui->btnOutputDir, IconsManager::ACTION_FILE_OPEN_FOLDER);
}