#include "lhcolordialog.h"

#include <QTimer>
#include <QLabel>
#include <QDialogButtonBox>
#include <QPushButton>

LHColorDialog::LHColorDialog(Skin sk, const QString& qss, QWidget* parent) :
    QDialog(parent),
    IDropShadowable(this),
    m_pLayout(new QHBoxLayout),
    m_skinQss(qss)
{
    //设置无边框属性
    setWindowFlag(Qt::FramelessWindowHint);
    //设置背景透明属性
    setAttribute(Qt::WA_TranslucentBackground, true);

    createColorDialog(sk);
}
// call this constructor
LHColorDialog::LHColorDialog(Skin sk, const QString& qss, const QColor& initial, QWidget* parent) :
    QDialog(parent),
    IDropShadowable(this),
    m_pLayout(new QHBoxLayout),
    m_skinQss(qss)
{
    //设置无边框属性
    setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint);
    //设置背景透明属性
    setAttribute(Qt::WA_TranslucentBackground, true);

    createColorDialog(sk, initial);
}

LHColorDialog::~LHColorDialog()
{
    qInfo() << "LHColorDialog: destructor...";
}

void LHColorDialog::setStandardColor(int idx, const QColor &clr)
{
    if (nullptr != m_pColorDlg) {
        m_pColorDlg->setStandardColor(idx, clr);
    }
}

void LHColorDialog::setCustomColor(int idx, const QColor &clr)
{
    if (nullptr != m_pColorDlg) {
        m_pColorDlg->setCustomColor(idx, clr);
    }
}

void LHColorDialog::createColorDialog(Skin sk, const QColor& initial)
{
    QHBoxLayout* pLay = new QHBoxLayout(this);
    QWidget* pMain = new QWidget(this);
    pMain->setLayout(m_pLayout);
    // XXX:加载样式表
    QFile* pf = nullptr;
    if (sk == emDARK) {
        pf = new QFile(":/skins/dark.qss", this);
    } else if (sk == emBRIGHT) {
        pf = new QFile(":/skins/bright.qss", this);
    }

    if (!pf && !m_skinQss.isEmpty()) {// 加载自定义样式
        pMain->setStyleSheet(m_skinQss);
    }
    if (pf && pf->open(QIODevice::ReadOnly)) {
        QString str(QString::fromUtf8(pf->readAll()));
        pMain->setStyleSheet(str);
        pf->close();
    }
    pLay->addWidget(pMain);
    pLay->setMargin(SHADOW_RADIUS);

    m_pColorDlg = new QColorDialog(initial, this);
    if (nullptr != m_pColorDlg) {
        customDialog();
        setButtonName();

        connect(m_pColorDlg, &QColorDialog::colorSelected, this, [this](const QColor& clr) {
            m_selColor = clr;
        });
        connect(m_pColorDlg, &QColorDialog::currentColorChanged, this, [this](const QColor& clr) {
            m_selColor = clr;
        });
        connect(m_pColorDlg, &QDialog::finished, this, [this](int code) {
            m_selColor = m_pColorDlg->selectedColor();
            if (code == QDialog::Accepted) {
                accept();
            } else {
                reject();
            }
        });
        m_pColorDlg->hide();
        m_pColorDlg->setOptions(QColorDialog::DontUseNativeDialog);
        m_pColorDlg->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);

        m_pLayout->addWidget(m_pColorDlg);
        m_pLayout->setMargin(0);

        m_pColorDlg->show();
        m_pColorDlg->move(0, 0);
        pMain->resize(m_pColorDlg->size());
        this->resize(m_pColorDlg->width() + 2 * SHADOW_RADIUS, m_pColorDlg->height() + 2 * SHADOW_RADIUS);

        BoxShadow shadows = {0, 0, SHADOW_RADIUS, 0, QColor(0, 0, 0, 90), QSize(0, 0), QImage()};
        SetDropShadow(shadows, pMain->size());
    }
    setMinimumHeight(2 * SHADOW_RADIUS);
    setMinimumWidth(2 * SHADOW_RADIUS);
}

void LHColorDialog::setButtonName()
{
    if (nullptr == m_pColorDlg) return;
    QList<QPushButton*> objs = m_pColorDlg->findChildren<QPushButton*>();
    for (auto& btn : objs) {
        if (btn->text() == "确定") {// Ok
            btn->setObjectName("btn_ok");
        } else if (btn->text() == "取消") {// Cancel
            btn->setObjectName("btn_cancel");
        }
    }
}

void LHColorDialog::customDialog()
{
    if (nullptr == m_pColorDlg) return;
    auto pLay = m_pColorDlg->layout();
    if (!pLay) return;
    pLay->setMargin(16);// 设置窗口外层边距16px
    // 拉伸颜色图
    auto pTopLay = pLay->itemAt(0);
    if (pTopLay && pTopLay->layout()) {
        auto pTop = pTopLay->layout();
        auto pRightLay = pTop->itemAt(1);
        if (pRightLay && pRightLay->layout()) {
            auto pPickLay = pRightLay->layout()->itemAt(0);
            if (QBoxLayout* pPick = qobject_cast<QBoxLayout*>(pPickLay->layout())) {
                pPick->takeAt(pPick->count() - 1);// 拿掉弹簧
                auto cLay = pPick->itemAt(0)->layout();
                if (QWidget* pWdg = cLay->itemAt(1)->widget()) {
                    pWdg->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
                }
            }
        }
    }
    // 调整按钮
    int nLay = pLay->count();
    auto subLay = pLay->itemAt(nLay - 1);
    if (subLay && subLay->widget()) {
        auto pWidget = subLay->widget();
        QDialogButtonBox* pbb = qobject_cast<QDialogButtonBox*>(pWidget);
        if (!pbb) return;
        pbb->clear();

        QPushButton* cancel = pbb->addButton("取消", QDialogButtonBox::NoRole);
        connect(cancel, &QPushButton::clicked, m_pColorDlg, &QColorDialog::reject);
        QPushButton* empty = pbb->addButton("", QDialogButtonBox::NoRole);
        empty->setObjectName("empty");
        empty->setVisible(false);
        empty->setEnabled(false);
        QPushButton* ok = pbb->addButton("确定", QDialogButtonBox::NoRole);
        connect(ok, &QPushButton::clicked, m_pColorDlg, &QColorDialog::accept);
        ok->setDefault(true);
        QPushButton* empty2 = pbb->addButton("", QDialogButtonBox::NoRole);
        empty2->setObjectName("empty");
        empty2->setVisible(false);
        empty2->setEnabled(false);

        auto pBtnLayout = pbb->layout();
        if (QBoxLayout* pboxlay = qobject_cast<QBoxLayout*>(pBtnLayout)) {
            pboxlay->setSpacing(8);
        }
    }
}

void LHColorDialog::mousePressEvent(QMouseEvent *event)
{
    if (Qt::LeftButton == event->button()) {
        m_bMousePress = true;
        m_prePos = event->globalPos()-this->pos();
    }
    event->accept();
}

void LHColorDialog::mouseReleaseEvent(QMouseEvent *event)
{
    m_bMousePress = false;
    event->accept();
}

void LHColorDialog::mouseMoveEvent(QMouseEvent *event)
{
    if (m_bMousePress && (Qt::LeftButton & event->buttons())) {
        this->move(event->globalPos() - m_prePos);
    }
    event->accept();
}