/***************************************************************************
 **
 **  Copyright (C) 2018-2023 wangjianjun <wangjianjun1018@qq.com>
 **  Contact: https://blog.csdn.net/wocanimei007?type=blog
 **
 **
****************************************************************************/
#include "fancydialog.h"
#include "fancybar.h"
#include "frameless_p.h"

#include <QVBoxLayout>
#include <QSettings>
#include <QPainter>



class FancyDialogPrivate
{
public:
    FancyDialogPrivate();

    FancyBar *fancyBar;
    QWidget *centralWidget;
};

FancyDialogPrivate::FancyDialogPrivate()
{
    fancyBar = nullptr;
    centralWidget = nullptr;
}

FancyDialog::FancyDialog(QWidget *parent, Qt::WindowFlags f)
    : QDialog(parent), d(new FancyDialogPrivate())
{
    QDialog::setWindowFlags(Qt::FramelessWindowHint |
                   Qt::WindowSystemMenuHint |
//                   Qt::WindowMinimizeButtonHint |
                   Qt::Dialog | f
                  );
    d->fancyBar = new FancyBar(this);
    d->fancyBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    d->centralWidget = new QWidget();
    QVBoxLayout *layout = new QVBoxLayout();
    layout->setContentsMargins(1, 0, 1, 1);
    layout->setSpacing(0);
    layout->addWidget(d->fancyBar);
    layout->addWidget(d->centralWidget);
    setLayout(layout);
    resize(800, 600);
}

FancyDialog::~FancyDialog()
{
    delete d;
}

void FancyDialog::setCentralWidget(QWidget *w)
{
    if (d->centralWidget == w || w == nullptr) {
        return;
    }
    if (d->centralWidget) {
        layout()->removeWidget(d->centralWidget);
        d->centralWidget->setParent(nullptr);
        d->centralWidget->deleteLater();
    }
    d->centralWidget = w;
    layout()->addWidget(d->centralWidget);
}

void FancyDialog::setWidgetResizable(bool resizable)
{
    d->fancyBar->setWidgetResizable(resizable);
}

void FancyDialog::setTitleBarHeight(int height)
{
    d->fancyBar->setTitleBarHeight(height);
}

void FancyDialog::setTitleBarColor(const QColor &color)
{
    d->fancyBar->setBackgroundColor(color);
}

void FancyDialog::addAdditionalControl(QAction *action)
{
    d->fancyBar->addAdditionalControl(action, FancyBar::TitlePosition);
}

void FancyDialog::setFixedSize(const QSize &s)
{
    setFixedSize(s.width(), s.height());
}

void FancyDialog::setFixedSize(int w, int h)
{
    d->fancyBar->setWidgetResizable(false);
    QWidget::setFixedSize(w, h);
}

void FancyDialog::setFixedWidth(int w)
{
    setFixedSize(w, this->height());
}

void FancyDialog::setFixedHeight(int h)
{
    setFixedSize(this->width(), h);
}

void FancyDialog::setWindowFlags(Qt::WindowFlags type)
{
    QDialog::setWindowFlags(type);
    d->fancyBar->updateWidgetFlags();
}

void FancyDialog::setBackgroundImage(const QString &path)
{
    m_sBackgroundImagePath = path;
    update();
}

FancyBar* FancyDialog::fancyBar()
{
    return d->fancyBar;
}

void FancyDialog::paintEvent(QPaintEvent *event)
{
//    Q_UNUSED(event);
//    QSettings settings(FramelessPrivate::g_settingsOrganization,
//                       FramelessPrivate::g_settingsApplication);
//    QString skinName = settings.value("skin").toString();

//    if (skinName.isEmpty()) {
//        skinName = QString(":/skin/default");
//    }

//    QPainter painter(this);
//    painter.setPen(Qt::NoPen);
//    painter.setBrush(Qt::gray);
//    painter.drawPixmap(QRect(0, 0, this->width(), this->height()),
//                       QPixmap(skinName));
    Q_UNUSED(event);
    if(!m_sBackgroundImagePath.isEmpty())
    {
        QPainter painter(this);
        painter.setPen(Qt::NoPen);
        painter.setBrush(Qt::gray);
        painter.drawPixmap(QRect(0, 0, this->width(), this->height()),
                           QPixmap(m_sBackgroundImagePath));
    }
}


