/*
 * Copyright (C) 2020, 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 "tipwidget.h"

#include <QLabel>
#include <QTimer>
#include <QGSettings>
#include <QHBoxLayout>
#include <QGraphicsDropShadowEffect>
#include <QPainter>
#include <QPalette>
#include <QPainterPath>

RoundedWidget::RoundedWidget(QWidget *parent) : QWidget(parent)
{
    backgroundColor = QColor(255, 255, 255);
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_NoSystemBackground);
}

void RoundedWidget::setBackgroundColor(const QColor &color)
{
    backgroundColor = color;
    update();
}

void RoundedWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 设置背景色
    QPalette palette = this->palette();
    palette.setColor(QPalette::Window, backgroundColor);
    this->setPalette(palette);

    // 使用QPainterPath绘制圆角矩形
    QPainterPath path;
    QRect rect = this->rect();
    path.addRoundedRect(rect, 6, 6);

    // 填充路径
    painter.fillPath(path, backgroundColor);

    // 设置裁剪区域为圆角矩形
    painter.setClipPath(path);
}

TipWidget::TipWidget(QWidget *parent) : QDialog(parent)
{
    w = new RoundedWidget(this);
    w->setAccessibleName("kylin-aiassistant_TipWidget_w");
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    w->setFixedHeight(48);

    // 暂时移除阴影效果，看看是否是阴影导致的问题
    // QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect(w);
    // effect->setOffset(0, 2);
    // effect->setColor(QColor(0, 0, 0, 32));
    // effect->setBlurRadius(6);
    // w->setGraphicsEffect(effect);
}

TipWidget::~TipWidget()
{
    if (w) {
        delete w;
        w = nullptr;
    }
}

void TipWidget::setLightTheme()
{
    RoundedWidget *roundedWidget = qobject_cast<RoundedWidget*>(w);
    if (roundedWidget) {
        roundedWidget->setBackgroundColor(QColor(255, 255, 255));
    }
}

void TipWidget::setBlackTheme()
{
    RoundedWidget *roundedWidget = qobject_cast<RoundedWidget*>(w);
    if (roundedWidget) {
        roundedWidget->setBackgroundColor(QColor(38, 38, 38));
    }
}

void TipWidget::showTip(QString tip, int show_time, QSize size, QWidget *parent, TipWidget::TipType type)
{
    TipWidget tw(parent);

    QLabel iconLabel(tw.getWidget());

    iconLabel.setFixedSize(24, 24);

    switch (type) {
    case OK:
        iconLabel.setStyleSheet(QString("border-image:url(:/html/html/img/tips.svg);"));
        break;
    case WARNING:
        iconLabel.setStyleSheet(QString("border-image:url(:/icon/icon/newBuiltTips.svg);"));
        break;
    case ERROR:
        iconLabel.setStyleSheet(QString("border-image:url(:/icon/icon/error-tips.svg)"));
        break;
    default:
        break;
    }

    QLabel textLabel(tw.getWidget());

    textLabel.setText(tip);
//    textLabel.setStyleSheet("font-family: Noto Sans CJK SC; font-size: 14px; font-weight: 400;");

    QHBoxLayout hLayout;
    hLayout.setContentsMargins(10,0,10,0);
    hLayout.addWidget(&iconLabel);
    hLayout.addWidget(&textLabel);
    hLayout.setAlignment(Qt::AlignHCenter);
    tw.getWidget()->setLayout(&hLayout);

    QTimer::singleShot(show_time, [&tw](){
        tw.accept();
    });

    tw.getWidget()->show();
    if (parent) {
//        QSize size = (parent->size() - tw.getWidget()->size()) / 2;
//        tw.getWidget()->move(parent->mapToGlobal(QPoint(0, 0)) + QPoint(size.width(), size.height()));
    }
    tw.exec();
}
