#include "diskhistorywidget.h"
#include <QPainter>
#include <QPainterPath>
#include <QIcon>
#include <QLocale>
#include "commoninfo.h"

#include "../macro.h"

DiskHistoryWidget::DiskHistoryWidget(QWidget *parent)
    : QWidget(parent)
{
    m_curFontSize = DEFAULT_FONT_SIZE;
    setAttribute(Qt::WA_TranslucentBackground);
    initUI();
    initStyleTheme();
}

DiskHistoryWidget::~DiskHistoryWidget()
{
    if (m_pStyleSettings) {
        delete m_pStyleSettings;
        m_pStyleSettings = nullptr;
    }
}

void DiskHistoryWidget::initUI()
{
    m_colorBg = QColor("#F5F5F5");

    // 初始化布局
    m_pMainVLayout = new QVBoxLayout();
    m_pMainVLayout->setContentsMargins(12, 0, 12, 0);
    m_pMainVLayout->setSpacing(0);
    m_pReadHLayout = new QHBoxLayout();
    m_pReadHLayout->setContentsMargins(0, 0, 0, 0);
    m_pReadHLayout->setSpacing(3);
    m_pWriteHLayout = new QHBoxLayout();
    m_pWriteHLayout->setContentsMargins(0, 0, 0, 0);
    m_pWriteHLayout->setSpacing(3);

    //初始化控件
    m_pDiskTitleLabel = new KLabel();
    m_pDiskTitleLabel->setText(tr("Disk reading and writing"));
    m_pDiskTitleLabel->setFixedWidth(160);
    m_pDiskTitleLabel->setAlignment(Qt::AlignLeft);

    m_pIconReadLabel = new QLabel();
    m_pIconReadLabel->setPixmap(QIcon::fromTheme(":/img/read.svg").pixmap(14,14));
    m_pReadHLayout->addWidget(m_pIconReadLabel, 0, Qt::AlignLeft|Qt::AlignVCenter);
    m_pReadTitleLabel = new KLabel();
    m_pReadTitleLabel->setText(tr("Disk Read"));
    m_pReadTitleLabel->setFixedWidth(60);
    m_pReadTitleLabel->setAlignment(Qt::AlignLeft);
    m_pReadHLayout->addWidget(m_pReadTitleLabel, 1, Qt::AlignLeft|Qt::AlignVCenter);
    m_pReadValueLabel = new KLabel();
    m_pReadValueLabel->setText("0MB/s");
    m_pReadValueLabel->setFixedWidth(80);

    m_pIconWriteLabel = new QLabel();
    m_pIconWriteLabel->setPixmap(QIcon::fromTheme(":/img/write.svg").pixmap(14,14));
    m_pWriteHLayout->addWidget(m_pIconWriteLabel, 0, Qt::AlignLeft|Qt::AlignVCenter);
    m_pWriteTitleLabel = new KLabel();
    m_pWriteTitleLabel->setText(tr("Disk Write"));
    m_pWriteTitleLabel->setFixedWidth(60);
    m_pWriteTitleLabel->setAlignment(Qt::AlignLeft);
    m_pWriteHLayout->addWidget(m_pWriteTitleLabel, 1, Qt::AlignLeft|Qt::AlignVCenter);
    m_pWriteValueLabel = new KLabel();
    m_pWriteValueLabel->setText("0MB/s");
    m_pWriteValueLabel->setFixedWidth(80);

    // 维吾尔 哈萨克斯 柯尔克孜 语言控件翻转
    QString language = QLocale::system().name();
    if (language == "ug_CN" || language == "kk_KZ" || language == "ky_KG") {
        m_pReadValueLabel->setAlignment(Qt::AlignLeft);
        m_pReadHLayout->addWidget(m_pReadValueLabel, 1, Qt::AlignLeft|Qt::AlignVCenter);
        m_pWriteValueLabel->setAlignment(Qt::AlignLeft);
        m_pWriteHLayout->addWidget(m_pWriteValueLabel, 1, Qt::AlignLeft|Qt::AlignVCenter);
    } else {
        m_pReadValueLabel->setAlignment(Qt::AlignRight);
        m_pReadHLayout->addWidget(m_pReadValueLabel, 1, Qt::AlignRight|Qt::AlignVCenter);
        m_pWriteValueLabel->setAlignment(Qt::AlignRight);
        m_pWriteHLayout->addWidget(m_pWriteValueLabel, 1, Qt::AlignRight|Qt::AlignVCenter);
    }

    m_pMainVLayout->addSpacing(10);
    m_pMainVLayout->addWidget(m_pDiskTitleLabel);
    m_pMainVLayout->addLayout(m_pReadHLayout);
    m_pMainVLayout->addLayout(m_pWriteHLayout);
    this->setLayout(m_pMainVLayout);

}

void DiskHistoryWidget::initStyleTheme()
{
    const QByteArray idd(THEME_QT_SCHEMA);
    if(QGSettings::isSchemaInstalled(idd)) {
        m_pStyleSettings = new QGSettings(idd);
    }
    if (m_pStyleSettings) {
        //监听主题改变
        connect(m_pStyleSettings, &QGSettings::changed, this, [=](const QString &key){
            themeChangedSlot(key);
        });
        auto variant = m_pStyleSettings->get(FONT_SIZE);
        if (variant.isValid()) {
            m_curFontSize = m_pStyleSettings->get(FONT_SIZE).toString().toFloat();
            onThemeFontChange(m_curFontSize);
        }
        variant = m_pStyleSettings->get("styleName");
        if (variant.isValid()) {
            onThemeStyleChange(variant.toString());
        }
    }
}

void DiskHistoryWidget::themeChangedSlot(const QString &key)
{
    if (key == "styleName") {
        auto variant = m_pStyleSettings->get("styleName");
        if (variant.isValid()) {
            onThemeStyleChange(variant.toString());
        }
    } else if("systemFont" == key || "systemFontSize" == key) {
        auto variant = m_pStyleSettings->get(FONT_SIZE);
        if (variant.isValid()) {
            m_curFontSize = m_pStyleSettings->get(FONT_SIZE).toString().toFloat();
            onThemeFontChange(m_curFontSize);
        }
    }
}

void DiskHistoryWidget::onThemeFontChange(float fFontSize)
{
    QFont fontContext;
    fontContext.setPointSize(fFontSize*0.8);
    QFont fontTitle = this->font();
    if (m_pReadValueLabel) {
        m_pReadValueLabel->setFont(fontContext);
    }
    if (m_pWriteValueLabel) {
        m_pWriteValueLabel->setFont(fontContext);
    }
    if (m_pDiskTitleLabel) {
        m_pDiskTitleLabel->setFont(fontTitle);
    }
    if (m_pReadTitleLabel) {
        m_pReadTitleLabel->setFont(fontTitle);
    }
    if (m_pWriteTitleLabel) {
        m_pWriteTitleLabel->setFont(fontTitle);
    }
}

void DiskHistoryWidget::onUpdateDiskData()
{
    double allReadSpeed = 0;
    double allWriteSpeed = 0;
    QStringList diskList = diskInfo::getDiskName();
    foreach (QString diskName, diskList) {
        DiskSpeedInfo::instance()->updateDiskSpeed(diskName);
        double readSpeed = DiskSpeedInfo::instance()->readSpeed(diskName);
        double writeSpeed = DiskSpeedInfo::instance()->writeSpeed(diskName);
        allReadSpeed += readSpeed;
        allWriteSpeed += writeSpeed;
    }

    m_pReadValueLabel->setText(commom::format::Format_Memory(allReadSpeed, KDK_KILOBYTE) + "/s");
    m_pWriteValueLabel->setText(commom::format::Format_Memory(allWriteSpeed, KDK_KILOBYTE) + "/s");
}

void DiskHistoryWidget::onThemeStyleChange(QString strStyleName)
{
    QPalette controlPE;
    if(strStyleName == "ukui-dark" || strStyleName == "ukui-black") {
        controlPE.setColor(QPalette::WindowText,QColor("#999999"));
        m_colorBg = QColor("#262626");
        m_pReadTitleLabel->setStyleSheet("QToolTip{border-radius:4px;background-color:rgb(56,56,56,100%);color:white;}");
        m_pReadValueLabel->setStyleSheet("QToolTip{border-radius:4px;background-color:rgb(56,56,56,100%);color:white;}");
        m_pWriteTitleLabel->setStyleSheet("QToolTip{border-radius:4px;background-color:rgb(56,56,56,100%);color:white;}");
        m_pWriteValueLabel->setStyleSheet("QToolTip{border-radius:4px;background-color:rgb(56,56,56,100%);color:white;}");
    } else {
        controlPE.setColor(QPalette::WindowText,QColor("#757575"));
        m_colorBg = QColor("#F5F5F5");
        m_pReadTitleLabel->setStyleSheet("QToolTip{border-radius:4px;background-color:rgb(245,245,245,100%);color:black;}");
        m_pReadValueLabel->setStyleSheet("QToolTip{border-radius:4px;background-color:rgb(245,245,245,100%);color:black;}");
        m_pWriteTitleLabel->setStyleSheet("QToolTip{border-radius:4px;background-color:rgb(245,245,245,100%);color:black;}");
        m_pWriteValueLabel->setStyleSheet("QToolTip{border-radius:4px;background-color:rgb(245,245,245,100%);color:black;}");
    }
    if (m_pReadTitleLabel) {
        m_pReadTitleLabel->setPalette(controlPE);
    }
    if (m_pReadValueLabel) {
        m_pReadValueLabel->setPalette(controlPE);
    }
    if (m_pWriteTitleLabel) {
        m_pWriteTitleLabel->setPalette(controlPE);
    }
    if (m_pWriteValueLabel) {
        m_pWriteValueLabel->setPalette(controlPE);
    }
    repaint();
}


void DiskHistoryWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        Q_EMIT clicked();
    }
    QWidget::mousePressEvent(event);
}

void DiskHistoryWidget::paintEvent(QPaintEvent *event)
{
    QPainterPath path;

    QPainter painter(this);
    painter.setOpacity(0.4);
    painter.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    painter.setClipping(true);
    painter.setPen(Qt::transparent);

    path.addRoundedRect(this->rect(), 4, 4);
    path.setFillRule(Qt::WindingFill);
    painter.setBrush(QBrush(m_colorBg));
    painter.setPen(Qt::transparent);

    painter.drawPath(path);
    QWidget::paintEvent(event);
}
