/*
 * 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>

TipWidget::TipWidget(QWidget *parent) : QDialog(parent)
{
    w = new QWidget(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, 4);
    effect->setColor(QColor(0, 0, 0, 64));
    effect->setBlurRadius(8);
    w->setGraphicsEffect(effect);
    w->setStyleSheet("QWidget{background-color:#FFFFFF;border-radius:6px;}");
}

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

void TipWidget::setLightTheme()
{
    w->setStyleSheet("QWidget{background-color:#FFFFFF;border-radius:6px;}");
}

void TipWidget::setBlackTheme()
{
    w->setStyleSheet("QWidget{background-color:#262626;border-radius:6px;}");
}

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/closeDoc_active.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();
}
