/*
    This file is part of Photoflare.

    Photoflare 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.

    Photoflare 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 Photoflare.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "pointersettingswidget.h"
#include "ui_pointersettingswidget.h"
#include "../Settings.h"

PointerSettingsWidget::PointerSettingsWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PointerSettingsWidget)
{
    ui->setupUi(this);

    connect(ui->strokeWidth, SIGNAL(valueChanged(int)), this, SIGNAL(settingsChanged()));
    connect(ui->checkBoxStroke, SIGNAL(clicked()), this, SIGNAL(settingsChanged()));
    connect(ui->checkBoxFill, SIGNAL(clicked()), this, SIGNAL(settingsChanged()));
}

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

void PointerSettingsWidget::setIconTheme(bool dark)
{
    auto p = [dark](const char *name) {
        QString path = QStringLiteral(":/icons/assets/icons/selections/") + QLatin1String(name);
        if (dark)
            path.replace(QLatin1String(":/icons/"), QLatin1String(":/icons-dark/"));
        return QIcon(path);
    };
    ui->square_sel->setIcon(p("square_sel.png"));
    ui->circle_sel->setIcon(p("circle_sel.png"));
    ui->lasso_sel->setIcon(p("lasso_sel.png"));
}

void PointerSettingsWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    if (e->type() == QEvent::LanguageChange)
        ui->retranslateUi(this);
}

bool PointerSettingsWidget::stroke() const
{
    return ui->checkBoxStroke->isChecked();
}

bool PointerSettingsWidget::fill() const
{
    return ui->checkBoxFill->isChecked();
}

bool PointerSettingsWidget::selectionIsEllipse() const
{
    return ui->circle_sel->isChecked();
}

bool PointerSettingsWidget::selectionIsLasso() const
{
    return ui->lasso_sel->isChecked();
}

void PointerSettingsWidget::on_square_sel_clicked(bool checked)
{
    if(checked)
    {
        ui->square_sel->setChecked(true);
        ui->circle_sel->setChecked(false);
        ui->lasso_sel->setChecked(false);
        emit settingsChanged();
    }
}

void PointerSettingsWidget::on_circle_sel_clicked(bool checked)
{
    if(checked)
    {
        ui->square_sel->setChecked(false);
        ui->circle_sel->setChecked(true);
        ui->lasso_sel->setChecked(false);
        emit settingsChanged();
    }
}

void PointerSettingsWidget::on_lasso_sel_clicked(bool checked)
{
    if(checked)
    {
        ui->square_sel->setChecked(false);
        ui->circle_sel->setChecked(false);
        ui->lasso_sel->setChecked(true);
        emit settingsChanged();
    }
}

void PointerSettingsWidget::on_checkBoxStroke_clicked(bool checked)
{
    if(checked)
    {
        ui->checkBoxFill->setChecked(false);
    }
}


void PointerSettingsWidget::on_checkBoxFill_clicked(bool checked)
{
    if(checked)
    {
        ui->checkBoxStroke->setChecked(false);
    }
}

int PointerSettingsWidget::strokeWidth() const
{
    return ui->strokeWidth->value();
}


void PointerSettingsWidget::on_strokeWidth_valueChanged(int width)
{
    Q_UNUSED(width)
}

void PointerSettingsWidget::saveSettings() const
{
    SETTINGS->setValue("toolSettings/pointer/stroke", stroke());
    SETTINGS->setValue("toolSettings/pointer/fill", fill());
    SETTINGS->setValue("toolSettings/pointer/strokeWidth", strokeWidth());
    const QString sel = selectionIsLasso() ? "lasso" : selectionIsEllipse() ? "ellipse" : "rect";
    SETTINGS->setValue("toolSettings/pointer/selection", sel);
}

void PointerSettingsWidget::loadSettings()
{
    ui->checkBoxStroke->setChecked(SETTINGS->value("toolSettings/pointer/stroke", false).toBool());
    ui->checkBoxFill->setChecked(SETTINGS->value("toolSettings/pointer/fill", false).toBool());
    ui->strokeWidth->setValue(SETTINGS->value("toolSettings/pointer/strokeWidth", 1).toInt());
    const QString sel = SETTINGS->value("toolSettings/pointer/selection", "rect").toString();
    ui->square_sel->setChecked(sel == "rect");
    ui->circle_sel->setChecked(sel == "ellipse");
    ui->lasso_sel->setChecked(sel == "lasso");
}
