/*
 * Copyright (C) 2019 Tianjin KYLIN Information Technology 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, 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 <http://www.gnu.org/licenses/&gt;.
 *
 */
#include "ukmedia_osd_display_widget.h"
#include <QHBoxLayout>
#include <QGuiApplication>
#include <QScreen>
#include <QDebug>
#include <QStyleOption>
#include <QPainter>
#include <QPainterPath>

extern double transparency;
UkmediaOsdDisplayWidget::UkmediaOsdDisplayWidget(QWidget *parent) : QWidget (parent)
{
    osdWidget = new QWidget(this);
    iconButton = new QPushButton(this);
    ret = 0;
    iconButton->setFocusPolicy(Qt::NoFocus);
    UkmediaOsdDisplayWidgetInit();
//    this->setMouseTracking(false);
//    setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint|Qt::X11BypassWindowManagerHint);
//    this->setWindowFlags(Qt::FramelessWindowHint|Qt::ToolTip);
    this->setWindowFlags(Qt::WindowStaysOnTopHint|Qt::Popup|Qt::ToolTip|Qt::FramelessWindowHint);
}

/*!
 * \brief
 * \details
 * 通过dbus获取任务栏位置
 */
int UkmediaOsdDisplayWidget::getPanelPosition(QString str)
{
    QDBusInterface interface( "com.ukui.panel.desktop",
                              "/",
                              "com.ukui.panel.desktop",
                              QDBusConnection::sessionBus() );
    QDBusReply<int> reply = interface.call("GetPanelPosition", str);
    if (reply < 0)
        return 0;
    return reply;
}

/*!
 * \brief
 * \details
 * 通过dbus获取任务栏高度
 */
int UkmediaOsdDisplayWidget::getPanelHeight(QString str)
{
    QDBusInterface interface( "com.ukui.panel.desktop",
                              "/",
                              "com.ukui.panel.desktop",
                              QDBusConnection::sessionBus() );
    QDBusReply<int> reply = interface.call("GetPanelSize", str);
    if (reply <= 0)
        return 46;
    return reply;
}

void UkmediaOsdDisplayWidget::UkmediaOsdDisplayWidgetInit()
{
    QVBoxLayout *hLayout = new QVBoxLayout();
    hLayout->addWidget(iconButton);
    osdWidget->setLayout(hLayout);

    int margin = 12;
    osdWidget->layout()->setContentsMargins(margin,margin,margin,margin);

    this->setFixedSize(72,72);
    this->iconButton->setFixedSize(48,48);

    QSize iconSize(48,48);
    this->iconButton->setIconSize(iconSize);

    this->setAttribute(Qt::WA_TranslucentBackground);
    QPalette palete = iconButton->palette();
    palete.setColor(QPalette::Highlight,Qt::transparent);
    palete.setBrush(QPalette::Button,QBrush(QColor(245,245,245,0.75)));
    iconButton->setPalette(palete);

//    this->setStyleSheet("QWidget{background:rgba(245,245,245,0.75); border-radius:12px;}");
//    this->osdWidget->setStyleSheet("QWidget{background:rgba(245,245,245,0.75); border-radius:12px;}");
//    this->iconButton->setStyleSheet("QPushButton::hover{background:rgba(245,245,245,0.75);"
//                                    "border-radius:4px;}"
//                                    "QPushButton::pressed{background:rgba((245,245,245,0.75);"
//                                    "border-radius:4px;padding-left:0px;}");
}

void UkmediaOsdDisplayWidget::UkmediaOsdSetIcon(QPixmap dev_Icon,QString themeType)
{
    QSize iconSize(48,48);

    if ( themeType == "ukui-white" || themeType == "ukui-light") {
        this->iconButton->setStyleSheet("QPushButton{background:rgba(245,245,245,1);"
                                        "border-radius:12px;padding-left:0px;}");
        this->setStyleSheet("QWidget{background:rgba(245,245,245,1); border-radius:12px;}");
        this->osdWidget->setStyleSheet("QWidget{background:rgba(245,245,245,1); border-radius:12px;}");
        this->iconButton->setStyleSheet("QPushButton::hover{background:rgba(245,245,245,1);"
                                        "border-radius:4px;}"
                                        "QPushButton::pressed{background:rgba((245,245,245,1);"
                                        "border-radius:4px;padding-left:0px;}");
    }
    else if (themeType == "ukui-dark" || themeType == "ukui-black" || themeType == "ukui-default") {
        this->iconButton->setStyleSheet("QPushButton{background:rgba(23,24,26,1);"
                                        "border-radius:12px;padding-left:0px;}");
        this->setStyleSheet("QWidget{background:rgba(23,24,26,1); border-radius:12px;}");
        this->osdWidget->setStyleSheet("QWidget{background:rgba(23,24,26,1); border-radius:12px;}");
        this->iconButton->setStyleSheet("QPushButton::hover{background:rgba(23,24,26,1);"
                                        "border-radius:4px;}"
                                        "QPushButton::pressed{background:rgba((23,24,26,1);"
                                        "border-radius:4px;padding-left:0px;}");
    }


    this->iconButton->setIconSize(iconSize);
    this->iconButton->setProperty("useIconHighlightEffect",true);
    this->iconButton->setProperty("iconHighlightEffectMode",true);
//this->iconButton->setIcon(QIcon(drawDarkColoredPixmap((QIcon::fromTheme(muteButtonStr).pixmap(iconSize)))));
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->iconButton->setIcon(dev_Icon);
}

void UkmediaOsdDisplayWidget::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    int rounded;
    opt.init(this);
    QPainter p(this);
    double transparence = transparency * 255;
    QColor color = palette().color(QPalette::Base);
    color.setAlpha(transparence);
    QBrush brush = QBrush(color);
    p.setBrush(brush);
    p.setPen(Qt::NoPen);
    QPainterPath path;
    opt.rect.adjust(0,0,0,0);
    rounded = 12;
    path.addRoundedRect(opt.rect,rounded,rounded);
    p.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    p.drawRoundedRect(opt.rect,rounded,rounded);
    this->setAttribute(Qt::WA_TranslucentBackground);
    setProperty("blurRegion",QRegion(path.toFillPolygon().toPolygon()));
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
//    QWidget::paintEvent(event);

}


UkmediaOsdDisplayWidget::~UkmediaOsdDisplayWidget()
{

}
