/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2019 KylinSoft  Co., Ltd.
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include "themebutton.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QDebug>
#include <QPainter>

ThemeButton::ThemeButton(const QString &name, QWidget *parent):
    QPushButton(parent),
    themename(name)
{
    mWidth = 200;
    mHeigh = 164;
    setFixedWidth(mWidth);
    mIconLabel = new ThemeIconLabel(QSize(mWidth, mHeigh - 32), this);
    initUi();
    setConnect();
}

ThemeButton::ThemeButton(const QString &name, QPixmap pixmap, QWidget *parent):
    QPushButton(parent),
    themename(name)
{
    mIconLabel = new ThemeIconLabel(pixmap, QSize(mWidth, mHeigh - 32), this);
    initUi();
    setConnect();
}


ThemeButton::ThemeButton(QString name, QStringList iStringList, QWidget *parent):
    themename(name),
    QPushButton(parent)
{
    mIconLabel = new ThemeIconLabel( iStringList, QSize(mWidth, mHeigh - 32), this);
    initUi();
    setConnect();
}

ThemeButton::ThemeButton(QString name, QList<QPixmap> iconList, QWidget *parent):
    themename(name),
    QPushButton(parent)
{
    mIconLabel = new ThemeIconLabel( iconList, QSize(mWidth, mHeigh - 32), this);
    initUi();
    setConnect();
}

ThemeButton::ThemeButton(const QString &name, const QIcon &icon, QSize size, QWidget *parent):
    QPushButton(parent),
    themename(name)
{
    mIconLabel = new ThemeIconLabel(icon, size, this);

    initUi();
    setConnect();
}

void ThemeButton::setIconCenter()
{
    if (mIconLabel) {
        mIconLabel->setIconCenter();
    }
}

void ThemeButton::initUi()
{
    setCheckable(true);
    setFixedWidth(mWidth);
    QVBoxLayout *baseVerLayout = new QVBoxLayout(this);
    baseVerLayout->setSpacing(8);
    baseVerLayout->setContentsMargins(0, 0, 0, 0);

    mIconLabel->changeClickedFlag(false);

    QHBoxLayout *bottomHorLayout = new QHBoxLayout;
    bottomHorLayout->setSpacing(8);
    bottomHorLayout->setContentsMargins(0, 0, 0, 0);

    mNameLabel = new QLabel(this);
    mNameLabel->setText(themename);
    mNameLabel->setAlignment(Qt::AlignTop);

    bottomHorLayout->addStretch();
    bottomHorLayout->setContentsMargins(0, 0, 0, 0);
    bottomHorLayout->addWidget(mNameLabel);
    bottomHorLayout->addStretch();

    baseVerLayout->addWidget(mIconLabel);
    baseVerLayout->addLayout(bottomHorLayout);

    this->setLayout(baseVerLayout);
}

void ThemeButton::btnCheckedSignalsSlot(bool status) {
    this->setChecked(status);
    if (!status) {
        mIconLabel->changeClickedFlag(false);
    }
}

void ThemeButton::enterWidgetSlot() {
    if (!this->isChecked()) {
        mIconLabel->changeClickedFlag(true);
    }
}

void ThemeButton::leaveWidgetSlot() {
    if (!this->isChecked()) {
        mIconLabel->changeClickedFlag(false);
    }
}

void ThemeButton::setConnect()
{
    connect(this, &ThemeButton::clicked, this, [=]() {
        mIconLabel->changeClickedFlag(true);
    });

    connect(this, &ThemeButton::btnCheckedSignals, [=](bool status) {
        btnCheckedSignalsSlot(status);
    });

    connect(mIconLabel,&ThemeIconLabel::enterWidget, [=]() {
        enterWidgetSlot();
    });

    connect(mIconLabel,&ThemeIconLabel::leaveWidget, [=]() {
        leaveWidgetSlot();
    });
}

void ThemeButton::setRadius(int radius)
{
    mRadius = radius;
    mIconLabel->setRadius(radius);
}

void ThemeButton::setRect(QSize size)
{
    mWidth = size.width();
    mHeigh = size.height();
    setFixedWidth(mWidth);
    mIconLabel->setFixedSize(QSize(mWidth, mHeigh - 32));
    mIconLabel->setMaskSize(QSize(mWidth, mHeigh - 32));
}

void ThemeButton::setPixmap(QPixmap pixmap)
{    
    mIconLabel->setIcon(pixmap);
}

void ThemeButton::setBtnClicked(bool status)
{
    this->setChecked(status);
    mIconLabel->changeClickedFlag(status);
}

void ThemeButton::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    painter.setClipping(true);
    painter.setPen(Qt::transparent);

    painter.drawRoundedRect(this->rect(), mRadius, mRadius);


    if (height() != mIconLabel->height() + 8 + mNameLabel->height())
        this->setFixedHeight(mIconLabel->height() + 8 + mNameLabel->height());

}
