#include "AppManagerSelectComboxItem.h"

#include <QIcon>
#include <QHBoxLayout>
#include <QGSettings>

AppManagerSelectComboxItem::AppManagerSelectComboxItem(std::shared_ptr<ISelectBox> box, QLabel* label, QWidget* parent)
    : ISelectComboxItem(box, label),
      QFrame(parent)
{
    initUi();
    initSlots();
}

void AppManagerSelectComboxItem::initUi()
{
    setFixedSize(512, 60);
    setFrameShape(QFrame::Shape::Box);

    m_pDisplayLabel->setFixedSize(120, 36);
    m_pDisplayLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    m_pSelectBox->getWidget()->setFixedSize(347, 36);

    auto hLayout = new QHBoxLayout;
    hLayout->setSpacing(0);
    hLayout->setContentsMargins(0, 0, 0, 0);
    hLayout->addItem(new QSpacerItem(16, 20, QSizePolicy::Fixed));
    hLayout->addWidget(m_pDisplayLabel);
    hLayout->addItem(new QSpacerItem(10, 20, QSizePolicy::Fixed));
    hLayout->addWidget(m_pSelectBox->getWidget());
    hLayout->addItem(new QSpacerItem(16, 20, QSizePolicy::Fixed));

    setLayout(hLayout);

    QString theme = "ukui-default";
    if (QGSettings::isSchemaInstalled("org.ukui.style")) {
        QGSettings* gst = new QGSettings("org.ukui.style");
        if (gst->keys().contains("styleName")) {
            theme = gst->get("style-name").toString();
        }
    }

    auto pal = palette();
    if (theme == "ukui-default" || theme == "ukui-light") {
        pal.setBrush(QPalette::Base, QColor("#eeeeee"));
    } else if (theme == "ukui-dark") {
        pal.setBrush(QPalette::Base, QColor("#333333"));
    }
    setPalette(pal);
}

void AppManagerSelectComboxItem::initSlots()
{
}

QWidget* AppManagerSelectComboxItem::getWidget()
{
    return this;
}

AppSelectComboxWithDescItem::AppSelectComboxWithDescItem(std::shared_ptr<ISelectBox> box, QLabel* label, QLabel* hintLabel, QWidget* parent)
    : ISelectComboxItem(box, label),
      m_pHintLabel(hintLabel),
      QFrame(parent)
{
    initUi();
    initSlots();
}

void AppSelectComboxWithDescItem::initUi()
{
    setFixedSize(512, 90);
    setFrameShape(QFrame::Shape::Box);

    m_pDisplayLabel->setFixedSize(120, 36);
    m_pDisplayLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    m_pSelectBox->getWidget()->setFixedSize(347, 36);
    m_pSelectBox->setEnabled(false);

    auto itemHLayout = new QHBoxLayout();
    itemHLayout->setSpacing(0);
    itemHLayout->setContentsMargins(0, 0, 0, 0);
    itemHLayout->addItem(new QSpacerItem(16, 20, QSizePolicy::Fixed));
    itemHLayout->addWidget(m_pDisplayLabel);
    itemHLayout->addItem(new QSpacerItem(10, 20, QSizePolicy::Fixed));
    itemHLayout->addWidget(m_pSelectBox->getWidget());
    itemHLayout->addItem(new QSpacerItem(16, 20, QSizePolicy::Fixed));

    m_pHintIconLabel = new QLabel;
    m_pHintIconLabel->setPixmap(QIcon::fromTheme("dialog-warning").pixmap(QSize(16, 16)));

    m_pHintLabel->setFixedSize(325, 28);
    m_pHintLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    auto hintPl = m_pHintLabel->palette();
    QColor color = hintPl.color(QPalette::PlaceholderText);
    hintPl.setColor(QPalette::Text, color);
    m_pHintLabel->setPalette(hintPl);

    auto hintHLayout = new QHBoxLayout();
    hintHLayout->setSpacing(0);
    hintHLayout->setContentsMargins(0, 0, 0, 0);
    hintHLayout->addSpacerItem(new QSpacerItem(148, 10, QSizePolicy::Fixed));
    hintHLayout->addWidget(m_pHintIconLabel);
    hintHLayout->addSpacerItem(new QSpacerItem(3, 10, QSizePolicy::Fixed));
    hintHLayout->addWidget(m_pHintLabel);
    hintHLayout->addSpacerItem(new QSpacerItem(16, 10, QSizePolicy::Expanding));

    auto vLayout = new QVBoxLayout();
    vLayout->setSpacing(0);
    vLayout->setContentsMargins(0, 0, 0, 0);
    vLayout->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Fixed));
    vLayout->addLayout(itemHLayout);
    vLayout->addSpacerItem(new QSpacerItem(10, 5, QSizePolicy::Fixed));
    vLayout->addLayout(hintHLayout);
    vLayout->addSpacerItem(new QSpacerItem(10, 16, QSizePolicy::Fixed));

    setLayout(vLayout);

    QString theme = "ukui-default";
    if (QGSettings::isSchemaInstalled("org.ukui.style")) {
        QGSettings* gst = new QGSettings("org.ukui.style");
        if (gst->keys().contains("styleName")) {
            theme = gst->get("style-name").toString();
        }
    }

    auto pal = palette();
    if (theme == "ukui-default" || theme == "ukui-light") {
        pal.setBrush(QPalette::Base, QColor("#eeeeee"));
    } else if (theme == "ukui-dark") {
        pal.setBrush(QPalette::Base, QColor("#333333"));
    }
    setPalette(pal);
}

void AppSelectComboxWithDescItem::initSlots()
{
}

QWidget* AppSelectComboxWithDescItem::getWidget()
{
    return this;
}

