#include "mediainfodialog.h"
#include "ui_mediainfodialog.h"

#include <QDebug>
#include <QScrollBar>
#include <QMouseEvent>

#include <KF5/KWindowSystem/kwindoweffects.h>

#include <ukui-log4qt.h>
#include "global/global.h"
#include "global/globalsignal.h"

using namespace Global;

MediaInfoDialog::MediaInfoDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MediaInfoDialog)
{
    ui->setupUi(this);
    setFixedSize(380, 355);
    this->setWindowModality(Qt::WindowModal);

    ui->btn_icon->setFixedSize(QSize(26,26));
    ui->btn_icon->setIconSize(QSize(26,26));
    ui->btn_icon->setIcon(QIcon::fromTheme("kylin-video"));
    ui->btn_icon->setStyleSheet("QPushButton{border:0px;background:transparent;}"
                                "QPushButton::hover{border:0px;background:transparent;}"
                                "QPushButton::pressed{border:0px;background:transparent;}");

    ui->btn_close->setFixedSize(30, 30);
    ui->btn_close->setProperty("isWindowButton", 0x2);
    ui->btn_close->setProperty("useIconHighlightEffect", 0x8);
    ui->btn_close->setFlat(true);
    ui->btn_close->setIcon(QIcon::fromTheme("window-close-symbolic"));
    connect(ui->btn_close, &QPushButton::clicked, [&](){accept();});

    ui->btn_ok->setProperty("isImportant", true);

    ui->m_title->setText(tr("title:"));
    ui->m_title->setFixedHeight(30);

    ui->v_title->setWordWrap(true);
    ui->v_title->setTextInteractionFlags(Qt::TextSelectableByMouse);
    ui->v_title->setCursor(Qt::IBeamCursor);

    ui->m_type->setText(tr("type:"));
    ui->m_type->setFixedHeight(30);

    ui->v_type->setTextInteractionFlags(Qt::TextSelectableByMouse);
    ui->v_type->setCursor(Qt::IBeamCursor);

    ui->m_size->setText(tr("size:"));
    ui->m_size->setFixedHeight(30);

    ui->v_size->setTextInteractionFlags(Qt::TextSelectableByMouse);
    ui->v_size->setCursor(Qt::IBeamCursor);

    ui->m_duration->setText(tr("duration:"));
    ui->m_duration->setFixedHeight(30);
    ui->v_duration->setTextInteractionFlags(Qt::TextSelectableByMouse);
    ui->v_duration->setCursor(Qt::IBeamCursor);

    ui->m_path->setText(tr("path:"));
    ui->m_path->setFixedHeight(30);

    ui->v_path->setWordWrap(true);
    ui->v_path->setTextInteractionFlags(Qt::TextSelectableByMouse);
    ui->v_path->setCursor(Qt::IBeamCursor);

    ui->scrollArea->setFixedHeight(165);
    ui->scrollArea->setFixedWidth(332);
    ui->scrollArea->setWidgetResizable(true);
    ui->scrollArea->setFrameShape(QFrame::NoFrame);
//    ui->scrollArea->setStyleSheet("QScrollArea {background-color:transparent;}");
//    ui->scrollArea->viewport()->setStyleSheet("background-color:transparent;");
    ui->scrollArea->verticalScrollBar()->setProperty("drawScrollBarGroove", false);
    ui->scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->scrollArea->verticalScrollBar()->setContextMenuPolicy(Qt::NoContextMenu);

    installEventFilter(this);
    connect(g_gsettings, &QGSettings::changed, this, &MediaInfoDialog::gSettingChanged);
    gSettingChanged("systemFont");
}

MediaInfoDialog::~MediaInfoDialog()
{
    delete ui;
}

void MediaInfoDialog::updateDate()
{
    ui->v_title->setText(m_title);
    ui->v_type->setText(m_type);
    ui->v_size->setText(m_size);
    ui->v_duration->setText((m_duration));
    ui->v_path->setText(m_filePath);

}


void MediaInfoDialog::showEvent(QShowEvent *event)
{
    // 背景模糊
    KWindowEffects::enableBlurBehind(winId(), true);
    return QDialog::showEvent(event);
}

void MediaInfoDialog::on_btn_ok_clicked()
{
    hide();
}

void MediaInfoDialog::gSettingChanged(QString key)
{
    if (key == "systemFontSize" || key == "systemFont") {
        // 字体改变
        QString fontFamily = g_gsettings->get("systemFont").toString();
        double fontSize = g_gsettings->get("systemFontSize").toDouble();
        qDebug() << "font_size: " << fontSize;
        QFont f(fontFamily);
        f.setPointSizeF(fontSize + 2);
        ui->label_2->setFont(f);
        f.setPointSizeF(fontSize);
        ui->m_title->setFont(f);
        ui->m_type->setFont(f);
        ui->m_size->setFont(f);
        ui->m_duration->setFont(f);
        ui->m_path->setFont(f);
        ui->v_title->setFont(f);
        ui->v_type->setFont(f);
        ui->v_size->setFont(f);
        ui->v_duration->setFont(f);
        ui->v_path->setFont(f);
    }
}
