/*
 * Copyright (C) 2023, 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 3 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, see <https://www.gnu.org/licenses/>.
 */
#include "promptwidget.h"
#include <QDebug>
#define UKUI_STYLE "org.ukui.style"
#define UKUI_FONT_SIZE "systemFontSize"
PromptWidget::PromptWidget(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(360, 180);

    m_networkErrorLayout = new QHBoxLayout(this);

    m_networkWarnIconLabel = new QLabel;
    m_networkErrorIconLayout = new QHBoxLayout();

    m_networkErrorRightLayout = new QVBoxLayout();
    m_networkErrorCode = new QLabel;
    m_networkErrorCode->setAlignment(Qt::AlignCenter);

    m_networkErrorRightRetryButtonLayout = new QHBoxLayout();
    m_retryButton = new QPushButton();
    m_retryButton->setText(tr("Retry"));

    m_lodingIconLabel = new QLabel(this);
    m_lodingIconLabel->setVisible(false);
    m_lodingIconLabel->setAlignment(Qt::AlignCenter);

    m_loadingMovie = new QMovie(":/res/contorl_icons/loadgif23.gif");

    m_loadingMovie->setParent(this);
    m_lodingIconLabel->setMovie(m_loadingMovie);

    addItems();
    initConnect();
}

void PromptWidget::initConnect()
{
    connect(ViewVar::getInstance(), &ViewVar::setFontSizeSignal, this, &PromptWidget::lable2SetFontSizeSlot);
    connect(m_retryButton, &QPushButton::clicked, this, &PromptWidget::changeButtonToLodingIcon);
    connect(m_retryButton, &QPushButton::clicked, Interaction::getInstance(), &Interaction::refresh);
    connect(ViewVar::getInstance(), &ViewVar::monitorThemeChanged, this, &PromptWidget::monitorThemeChanged);
}

void PromptWidget::changeButtonToLodingIcon()
{
    m_retryButton->setVisible(false);
    m_loadingMovie->start();
    m_lodingIconLabel->setVisible(true);
}

void PromptWidget::changeLoadingIconToButton()
{
    m_retryButton->setVisible(true);
    m_lodingIconLabel->setVisible(false);
    if (m_loadingMovie->state() != QMovie::NotRunning) {
        m_loadingMovie->stop();
    }
}

//可能存在全屏后图片无法跟随放大问题
void PromptWidget::monitorThemeChanged(QString themeChanged)
{
    if ("ukui-dark" == themeChanged || "ukui-black" == themeChanged) {
        m_iconPath = ":/res/contorl_icons/network_warn_dark.png";
    } else {
        m_iconPath = ":/res/contorl_icons/network_warn_normal.png";
    }

    if (m_judgeFullScreen == 0) {
        m_networkWarnIconLabel->resize(77, 77);
    } else {
        m_networkWarnIconLabel->resize(154, 154);
    }
    m_networkErrorIcon = QPixmap(m_iconPath);
    m_networkErrorIcon = m_networkErrorIcon.scaled(m_networkWarnIconLabel->width(), m_networkWarnIconLabel->height(),
                                                   Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    m_networkWarnIconLabel->setPixmap(m_networkErrorIcon);
}

void PromptWidget::isFullScreen(int m_isFullScreen)
{
    m_judgeFullScreen = m_isFullScreen;
    initLayout(netErrorWords, m_isFullScreen);
}

void PromptWidget::lable2SetFontSizeSlot(int size)
{
    initLayout(netErrorWords, m_judgeFullScreen);
}

void PromptWidget::initLayout(QString netError, int m_isFullScreen)
{
    netErrorWords = netError;
    // 普通界面大小
    if (m_isFullScreen == 0) {
        this->setFixedSize(360, 140);
        m_networkWarnIconLabel->resize(77, 77);
        //        m_iconPath = ":/res/contorl_icons/network_warn_normal.png";
        m_networkErrorIcon = QPixmap(m_iconPath);
        m_networkErrorIcon =
            m_networkErrorIcon.scaled(m_networkWarnIconLabel->width(), m_networkWarnIconLabel->height(),
                                      Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        m_networkWarnIconLabel->setPixmap(m_networkErrorIcon);

        m_networkErrorCode->setText(netError);
        m_networkErrorCode->setAlignment(Qt::AlignVCenter);
        m_networkErrorCode->resize(122, 20);
        // 设置字号
        QFont networkErrorCodeFont;
        //        m_font.setPixelSize(14);
        m_networkErrorCode->setFont(networkErrorCodeFont);

        m_retryButton->setFixedSize(96, 35);

        m_retryButton->setFont(networkErrorCodeFont);

        m_lodingIconLabel->setFixedSize(96, 35);
        //        m_retryButtonIconPath = ":/res/contorl_icons/retry_normal.png";
        //        m_retryButtonIcon = QPixmap(m_retryButtonIconPath);
        //        m_retryButtonIcon = m_retryButtonIcon.scaled(m_retryButton->width(), m_retryButton->height(),
        //        Qt::IgnoreAspectRatio, Qt::SmoothTransformation); m_retryButton->setIcon(m_retryButtonIcon);
        //        m_retryButton->setIconSize(QSize(m_retryButton->width(),m_retryButton->height()));
        //        m_retryButton->setFlat(true);//按钮背景为透明，但是有边框
    }
    //全屏界面大小
    else {
        this->setFixedSize(632, 280);
        m_networkWarnIconLabel->resize(154, 154);
        //        m_iconPath = ":/res/contorl_icons/network_warn_normal.png";
        m_networkErrorIcon = QPixmap(m_iconPath);
        m_networkErrorIcon =
            m_networkErrorIcon.scaled(m_networkWarnIconLabel->width(), m_networkWarnIconLabel->height(),
                                      Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        m_networkWarnIconLabel->setPixmap(m_networkErrorIcon);

        m_networkErrorCode->setText(netError);
        m_networkErrorCode->setAlignment(Qt::AlignVCenter);
        m_networkErrorCode->resize(280, 28);
        // 设置字号
        m_font.setPixelSize(32);
        m_networkErrorCode->setFont(m_font);

        m_retryButton->setFixedSize(202, 70);
        m_retryButton->setFont(m_font);

        m_lodingIconLabel->setFixedSize(202, 70);
        //        m_retryButtonIconPath = ":/res/contorl_icons/retry_normal.png";
        //        m_retryButtonIcon = QPixmap(m_retryButtonIconPath);
        //        m_retryButtonIcon = m_retryButtonIcon.scaled(m_retryButton->width(), m_retryButton->height(),
        //        Qt::IgnoreAspectRatio, Qt::SmoothTransformation); m_retryButton->setIcon(m_retryButtonIcon);
        //        m_retryButton->setIconSize(QSize(m_retryButton->width(),m_retryButton->height()));
        //        m_retryButton->setFlat(true);//按钮背景为透明，但是有边框
        //    QHBoxLayout *m_networkErrorRightRetryButtonLayout = new QHBoxLayout();
    }
}

void PromptWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    QStyleOption opt;
    opt.init(this);
    painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
    painter.setBrush(QBrush(opt.palette.color(QPalette::Base)));
    painter.setPen(Qt::transparent);
    int radius = ViewVar::SAME_ITEMRADIUS;
    QRect rect = this->rect();
    rect.setWidth(rect.width());
    rect.setHeight(rect.height());
    painter.drawRoundedRect(rect, radius, radius);
}

void PromptWidget::addItems()
{
    m_networkErrorLayout->addStretch(2);

    m_networkErrorLayout->addWidget(m_networkWarnIconLabel);
    m_networkErrorLayout->addStretch(2);
    m_networkErrorRightLayout->addWidget(m_networkErrorCode);
    m_networkErrorRightRetryButtonLayout->addWidget(m_retryButton);
    m_networkErrorRightRetryButtonLayout->addWidget(m_lodingIconLabel);
    m_networkErrorRightLayout->addLayout(m_networkErrorRightRetryButtonLayout);
    m_networkErrorRightRetryButtonLayout->setContentsMargins(0, 0, 0, 25);

    m_networkErrorLayout->addLayout(m_networkErrorRightLayout);
    m_networkErrorLayout->addStretch(2);
    this->setLayout(m_networkErrorLayout);
}
