/*
 * SPDX-FileCopyrightText: 2026 Laurent Montel <montel@kde.org>
 *
 * SPDX-License-Identifier: LGPL-2.0-or-later
 */

#include "backgroundstylewidget.hpp"
#include "backgroundstyleactionswidget.h"
#include "item/itemutils.hpp"
#include <KLocalizedString>
#include <QButtonGroup>
#include <QHBoxLayout>

using namespace Qt::Literals::StringLiterals;
BackgroundStyleWidget::BackgroundStyleWidget(QWidget *parent)
    : PropertyWidget(parent)
{
    m_widget = new QWidget{parent};
    m_widget->setObjectName(u"m_widget"_s);
    m_group = new QButtonGroup{m_widget};

    auto layout{new QHBoxLayout(m_widget)};
    layout->setObjectName(u"layout"_s);
    layout->setContentsMargins({});
    layout->setAlignment(Qt::AlignLeft);

    auto noneButton{new BackgroundStyleActionsWidget(m_widget)};
    noneButton->setObjectName(u"noneButton"_s);
    noneButton->setProperty("background-style", ItemUtils::convertItemBackgroundTypeEnumToString(Item::BackgroundType::None));
    noneButton->setToolTip(i18nc("@info:tooltip", "None"));
    noneButton->setCheckable(true);
    // Laurent: wait for icons (from kde team)
    noneButton->setIcon(QIcon::fromTheme(u"line_solid"_s));
    m_group->addButton(noneButton);

    auto solidButton{new BackgroundStyleActionsWidget(m_widget)};
    solidButton->setObjectName(u"solidButton"_s);
    solidButton->setProperty("background-style", ItemUtils::convertItemBackgroundTypeEnumToString(Item::BackgroundType::Solid));
    solidButton->setToolTip(i18nc("@info:tooltip", "Solid"));
    solidButton->setCheckable(true);
    // Laurent: wait for icons (from kde team)
    solidButton->setIcon(QIcon::fromTheme(u"line_solid"_s));
    m_group->addButton(solidButton);

    auto diagonalCrossButton{new BackgroundStyleActionsWidget(m_widget)};
    diagonalCrossButton->setObjectName(u"diagonalCrossButton"_s);
    diagonalCrossButton->setProperty("background-style", ItemUtils::convertItemBackgroundTypeEnumToString(Item::BackgroundType::CrossDiagonal));
    diagonalCrossButton->setToolTip(i18nc("@info:tooltip", "Diagonal Cross"));
    diagonalCrossButton->setCheckable(true);
    // Laurent: wait for icons (from kde team)
    diagonalCrossButton->setIcon(QIcon::fromTheme(u"line_solid"_s));
    m_group->addButton(diagonalCrossButton);

    auto diagonalButton{new BackgroundStyleActionsWidget(m_widget)};
    diagonalButton->setObjectName(u"diagonalButton"_s);
    diagonalButton->setProperty("background-style", ItemUtils::convertItemBackgroundTypeEnumToString(Item::BackgroundType::Diagonal));
    diagonalButton->setToolTip(i18nc("@info:tooltip", "Diagonal"));
    diagonalButton->setCheckable(true);
    // Laurent: wait for icons (from kde team)
    diagonalButton->setIcon(QIcon::fromTheme(u"line_solid"_s));
    m_group->addButton(diagonalButton);

    layout->addWidget(noneButton);
    layout->addWidget(solidButton);
    layout->addWidget(diagonalCrossButton);
    layout->addWidget(diagonalButton);

    m_group->button(m_group->id(noneButton))->setChecked(true);
    connect(m_group, &QButtonGroup::idClicked, this, [this]() {
        Q_EMIT changed(value());
    });

    m_widget->hide();
}

QString BackgroundStyleWidget::name() const
{
    return i18n("Background Style");
}

const Property BackgroundStyleWidget::value() const
{
    return Property{m_group->checkedButton()->property("background-style"), Property::Type::BackgroundStyle};
}

void BackgroundStyleWidget::setValue(const QVariant &val)
{
    const auto grpButtons = m_group->buttons();
    for (auto b : grpButtons) {
        if (b->property("background-style").toString() == val.toString()) {
            b->setChecked(true);
            break;
        }
    }
}

#include "moc_backgroundstylewidget.cpp"
