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

ScrollingAreaWidgetMajor::ScrollingAreaWidgetMajor(QWidget *parent) : QWidget(parent)
{
    setScrollWidget();
    initMemberVariable();
    initGsettingValue();
    initGsettingStyleValue();
    initSlideStatus();
    initLayout();
}

void ScrollingAreaWidgetMajor::initMemberVariable()
{
    m_pVMainLayout = new QVBoxLayout();
    m_pVMainLayout->setContentsMargins(0, 16, 0, 16);
    m_pVMainLayout->setSpacing(0);
    this->setContentsMargins(0, 0, 0, 0);

    m_pPictureToWhite = new PictureToWhite();

    m_pVolumeWidget      = new QWidget();
    m_pVolumeWidget->setContentsMargins(0, 0, 0, 0);
    m_pVolumeIconLabel   = new QLabel();
    m_pVolumeIconLabel->setProperty("useIconHighlightEffect", true);
    m_pVolumeIconLabel->setProperty("iconHighlightEffectMode", true);
    m_pVolumeIconLabel->setFixedSize(24, 24);
    m_pVolumeSlide       = new SliderButtonMajor();
    m_pVolumeSlide->setFixedSize(306,20);
    connect(m_pVolumeSlide, &SliderButtonMajor::valueChanged, this, &ScrollingAreaWidgetMajor::setVolumeSlideSlots);
    connect(m_pVolumeSlide,&SliderButtonMajor::ycoordinate,this,&ScrollingAreaWidgetMajor::setVoice);

    m_pHVolumeLayout     = new QHBoxLayout();
    m_pHVolumeLayout->setContentsMargins(0, 0, 0, 0);
    m_pHVolumeLayout->setSpacing(0);

    m_pBrightNessWidget  = new QWidget();
    m_pBrightNessWidget->setContentsMargins(0, 0, 0, 0);
    m_pBrightIconLabel   = new QLabel();
    m_pBrightIconLabel->setProperty("useIconHighlightEffect", true);
    m_pBrightIconLabel->setProperty("iconHighlightEffectMode", true);
    m_pBrightIconLabel->setFixedSize(24, 24);
    m_pBrightSlide       = new SliderButtonMajor();
    m_pBrightSlide->setFixedSize(306,20);
    connect(m_pBrightSlide, &SliderButtonMajor::valueChanged, this, &ScrollingAreaWidgetMajor::setBrightSlideSlots);
    connect(m_pBrightSlide,&SliderButtonMajor::ycoordinate,this,&ScrollingAreaWidgetMajor::setBright);

    m_pBrightLayout      = new QHBoxLayout();
    m_pBrightLayout->setContentsMargins(0, 0, 0, 0);
    m_pBrightLayout->setSpacing(0);
}

void ScrollingAreaWidgetMajor::initGsettingValue()
{
    const QByteArray id(UKUI_VOLUME_BRIGHTNESS_GSETTING_ID);
    if(QGSettings::isSchemaInstalled(id)) {
        m_pVolumeLightSetting = new QGSettings(id);
        connect(m_pVolumeLightSetting, &QGSettings::changed, this, &ScrollingAreaWidgetMajor::setSliderValue);
    }

    const QByteArray id_2(KYLIN_POWER_MODE_GSETTING_VALUE);
    if(QGSettings::isSchemaInstalled(id_2)) {
        m_pBrightNessSetting = new QGSettings(id_2);
        connect(m_pBrightNessSetting, &QGSettings::changed, this, &ScrollingAreaWidgetMajor::setSliderValue);
    }
    return;
}

void ScrollingAreaWidgetMajor::initSlideStatus()
{
    int value = m_pVolumeLightSetting->get(UKUI_VOLUME_KEY).toInt();
    m_pVolumeSlide->setValue(value);
    setVolumeStatusIcon(value);

    value = m_pBrightNessSetting->get(UKUI_BRIGHTNESS_KEY).toInt();
    m_pBrightSlide->setValue(value);
    setLightStatusIcon(value);
    return;
}

void ScrollingAreaWidgetMajor::setVoice(int num)
{
    double x = num;
    double y = x / 306 *100;
    QString str = QString::number(y,'f',0);
    y = str.toInt();
    setVolumeSlideSlots(y);
}

void ScrollingAreaWidgetMajor::setBright(int num)
{
    double x = num;
    double y = x / 306 *100;
    QString str = QString::number(y,'f',0);
    y = str.toInt();
    setBrightSlideSlots(y);
}

void ScrollingAreaWidgetMajor::initLayout()
{
    m_pHVolumeLayout->addWidget(m_pVolumeIconLabel);
//    m_pHVolumeLayout->addItem(new QSpacerItem(1, 1));
    m_pHVolumeLayout->addWidget(m_pVolumeSlide);
    m_pVolumeWidget->setLayout(m_pHVolumeLayout);
    m_pVolumeWidget->setFixedHeight(24);

    m_pBrightLayout->addWidget(m_pBrightIconLabel);
    m_pBrightLayout->addItem(new QSpacerItem(3, 1));
    m_pBrightLayout->addWidget(m_pBrightSlide);

    m_pBrightNessWidget->setLayout(m_pBrightLayout);
    m_pBrightNessWidget->setFixedHeight(24);

    m_pVMainLayout->addWidget(m_pVolumeWidget);


    //加入背光判断
    if (m_bBacklitFile) {
        m_pVMainLayout->addItem(new QSpacerItem(29, 20));
        m_pVMainLayout->addWidget(m_pBrightNessWidget);
    }
    this->setLayout(m_pVMainLayout);
}

/* 获取系统内核是否包含有背光文件，是否支持调整亮度 */
void ScrollingAreaWidgetMajor::setScrollWidget()
{
    QProcess process;
    process.start("ukui-power-backlight-helper --get-max-brightness");
    process.waitForFinished();
    QByteArray output = process.readAllStandardOutput();
    QString str_output = output;
    if (str_output == "没有找到您系统上的背光\n") {
        m_bBacklitFile = false;
//         m_bBacklitFile = true; //调试
    } else {
        m_bBacklitFile = true;
    }
    qDebug() << "是否含有背光文件---->" << str_output;
    return;
}

/* 当对应的组件修改了gsetting，需修改对应滚动条的值 */
void ScrollingAreaWidgetMajor::setSliderValue(QString key)
{
    int value;
    if (key == UKUI_VOLUME_KEY) {
        value = m_pVolumeLightSetting->get(UKUI_VOLUME_KEY).toInt();
        m_pVolumeSlide->setValue(value);
        setVolumeStatusIcon(value);
    } else if (key == UKUI_BRIGHTNESS_KEY) {
        value = m_pBrightNessSetting->get(UKUI_BRIGHTNESS_KEY).toInt();
        m_pBrightSlide->setValue(value);
        setLightStatusIcon(value);
    }

    bool valueStatus = m_pVolumeLightSetting->get(UKUI_MUTE_KEY).toBool();
    if (valueStatus) {
        m_pVolumeIconLabel->setPixmap(QIcon::fromTheme("audio-volume-muted-symbolic").pixmap(m_pVolumeIconLabel->size()));
    } else {
        value = m_pVolumeLightSetting->get(UKUI_VOLUME_KEY).toInt();
        m_pVolumeSlide->setValue(value);
        setVolumeStatusIcon(value);
    }
    return;
}

void ScrollingAreaWidgetMajor::initGsettingStyleValue()
{
    const QByteArray id(UKUI_QT_STYLE);
    if (QGSettings::isSchemaInstalled(id)) {
        m_pTabletModeStyleGsetting = new QGSettings(id);
        connect(m_pTabletModeStyleGsetting, &QGSettings::changed, [=]() {
            //监听主题，更改样式
            initSlideStatus();
        });
    }
    return;
}

/* 根据声音大小值设置状态图标 */
void ScrollingAreaWidgetMajor::setVolumeStatusIcon(int value)
{
    if (value == 0)
        m_pVolumeIconLabel->setPixmap(QIcon::fromTheme("audio-volume-muted-symbolic").pixmap(m_pVolumeIconLabel->size()));
    else if (value > 0 && value <= 35)
        m_pVolumeIconLabel->setPixmap(QIcon::fromTheme("audio-volume-low-symbolic").pixmap(m_pVolumeIconLabel->size()));
    else if (value >= 36 && value <= 65)
        m_pVolumeIconLabel->setPixmap(QIcon::fromTheme("audio-volume-medium-symbolic").pixmap(m_pVolumeIconLabel->size()));
    else
       m_pVolumeIconLabel->setPixmap(QIcon::fromTheme("audio-volume-high-symbolic").pixmap(m_pVolumeIconLabel->size()));
    m_pVolumeIconLabel->update();
}

/* 根据亮度值设置状态图标 */
void ScrollingAreaWidgetMajor::setLightStatusIcon(int value)
{
    //应用主题，貌似不好使
//    if (value == 0)
//        m_pBrightIconLabel->setPixmap(QIcon::fromTheme("ukui-light-0-symbolic").pixmap(m_pBrightIconLabel->size()));
//    else if (value > 0 && value <= 25)
//        m_pBrightIconLabel->setPixmap(QIcon::fromTheme("ukui-light-25-symbolic").pixmap(m_pBrightIconLabel->size()));
//    else if (value >25 && value <= 50)
//        m_pBrightIconLabel->setPixmap(QIcon::fromTheme("ukui-light-50-symbolic").pixmap(m_pBrightIconLabel->size()));
//    else if (value >50 && value <= 75)
//       m_pBrightIconLabel->setPixmap(QIcon::fromTheme("ukui-light-75-symbolic").pixmap(m_pBrightIconLabel->size()));
//    else
//       m_pBrightIconLabel->setPixmap(QIcon::fromTheme("ukui-light-100-symbolic").pixmap(m_pBrightIconLabel->size()));
//       m_pBrightIconLabel->update();

    m_bModelStatus = m_pTabletModeStyleGsetting->get("styleName").toString();
    if (m_bModelStatus == "ukui-dark" || m_bModelStatus == "ukui-default" ) {
        if (value == 0)
              m_pBrightIconLabel->setPixmap(QIcon(":/image_major/icon-light-0-white.svg").pixmap(m_pBrightIconLabel->size()));
        else if (value > 0 && value <= 25)
              m_pBrightIconLabel->setPixmap(QIcon(":/image_major/icon-light-25-white.svg").pixmap(m_pBrightIconLabel->size()));
        else if (value >25 && value <= 50)
              m_pBrightIconLabel->setPixmap(QIcon(":/image_major/icon-light-50-white.svg").pixmap(m_pBrightIconLabel->size()));
        else if (value >50 && value <= 75)
              m_pBrightIconLabel->setPixmap(QIcon(":/image_major/icon-light-75-white.svg").pixmap(m_pBrightIconLabel->size()));
        else
              m_pBrightIconLabel->setPixmap(QIcon(":/image_major/icon-light-100-white.svg").pixmap(m_pBrightIconLabel->size()));
    } else {
        if (value == 0)
              m_pBrightIconLabel->setPixmap(QIcon(":/image_major/icon-light-0-black.svg").pixmap(m_pBrightIconLabel->size()));
        else if (value > 0 && value <= 25)
              m_pBrightIconLabel->setPixmap(QIcon(":/image_major/icon-light-25-black.svg").pixmap(m_pBrightIconLabel->size()));
        else if (value >25 && value <= 50)
              m_pBrightIconLabel->setPixmap(QIcon(":/image_major/icon-light-50-black.svg").pixmap(m_pBrightIconLabel->size()));
        else if (value >50 && value <= 75)
              m_pBrightIconLabel->setPixmap(QIcon(":/image_major/icon-light-75-black.svg").pixmap(m_pBrightIconLabel->size()));
        else
              m_pBrightIconLabel->setPixmap(QIcon(":/image_major/icon-light-100-black.svg").pixmap(m_pBrightIconLabel->size()));
    }

    m_pBrightIconLabel->update();




}

/* 改变音量滚动条时，修改gsetting值，通知音量组件修改，同步 */
void ScrollingAreaWidgetMajor::setVolumeSlideSlots(int value)
{
    if (nullptr != m_pVolumeLightSetting) {
        m_pVolumeLightSetting->set(UKUI_VOLUME_KEY, value);
    }
    setVolumeStatusIcon(value);
    return;
}

/* 改变亮度滚动条时，修改gsetting值，通知修改亮度，同步 */
void ScrollingAreaWidgetMajor::setBrightSlideSlots(int value)
{
    if (value >3) {
        if (nullptr != m_pBrightNessSetting) {
            m_pBrightNessSetting->set(UKUI_BRIGHTNESS_KEY, value);
        }
        setLightStatusIcon(value);
    }

    return;
}

SliderButtonMajor::SliderButtonMajor(QWidget *parent) : QSlider(parent)
{

    const QByteArray id(UKUI_VOLUME_BRIGHTNESS_GSETTING_ID);
    if(QGSettings::isSchemaInstalled(id)) {
          m_pVolumeLightSetting = new QGSettings(id);
    }

    setOrientation(Qt::Orientation::Horizontal);
    setMaximum(100);

    if (m_pVolumeLightSetting->get(UKUI_VOLUME_KEY).toInt() <20) {
        setStyleSheet(
               "QSlider::groove:Horizontal {"
                "border: 0px none;"
                "background: #2FB3E8;"
                "height: 20px;"
                "width: 306px;"
                "border-radius: 8px;"
                "}"

                "QSlider::handle:Horizontal {"
                "background: #2FB3E8;"
                "border: 0px none;"
                "width: 20px;"
                "height: 50px;"
                "border-radius: 8px;"
                "margin: 0px 0;"
                "border-image:url(:/image_major/slider.png);"
                "}"

                "QSlider::add-page:Horizontal {"
                "background: #F5F5F5;"
                "border-radius: 8px;"
                "}"

                "QSlider::sub-page:horizontal {"
                "background: transparent;"
                "border-radius: 8px;"
                "}"
       );

    } else {
        setStyleSheet(
               "QSlider::groove:Horizontal {"
                "border: 0px none;"
                "background: #F5F5F5;"
                "height: 20px;"
                "width: 306px;"
                "border-radius: 8px;"
                "}"

                "QSlider::handle:Horizontal {"
                "background: #2FB3E8;"
                "border: 0px none;"
                "width: 20px;"
                "height: 50px;"
                "border-radius: 8px;"
                "margin: 0px 0;"
                "border-image:url(:/image_major/slider.png);"
                "}"

                "QSlider::add-page:Horizontal {"
                "background: transparent;"
                "border-radius: 8px;"
                "}"

                "QSlider::sub-page:horizontal {"
                "background: #2FB3E8;"
                "border-radius: 8px;"
                "}"
            );
    }


}
//白色 #F5F5F5
//蓝色 #2FB3E8

void SliderButtonMajor::mousePressEvent(QMouseEvent *e)
{
    if (e->x() <20) {
        setStyleSheet(
               "QSlider::groove:Horizontal {"
                "border: 0px none;"
                "background: #2FB3E8;"
                "height: 20px;"
                "width: 306px;"
                "border-radius: 8px;"
                "}"

                "QSlider::handle:Horizontal {"
                "background: #2FB3E8;"
                "border: 0px none;"
                "width: 20px;"
                "height: 50px;"
                "border-radius: 8px;"
                "margin: 0px 0;"
                "border-image:url(:/image_major/slider.png);"
                "}"

                "QSlider::add-page:Horizontal {"
                "background: #F5F5F5;"
                "border-radius: 8px;"
                "}"

                "QSlider::sub-page:horizontal {"
                "background: transparent;"
                "border-radius: 8px;"
                "}"
       );
    } else {
        setStyleSheet(
               "QSlider::groove:Horizontal {"
                "border: 0px none;"
                "background: #F5F5F5;"
                "height: 20px;"
                "width: 306px;"
                "border-radius: 8px;"
                "}"

                "QSlider::handle:Horizontal {"
                "background: #2FB3E8;"
                "border: 0px none;"
                "width: 20px;"
                "height: 50px;"
                "border-radius: 8px;"
                "margin: 0px 0;"
                "border-image:url(:/image_major/slider.png);"
                "}"

                "QSlider::add-page:Horizontal {"
                "background: transparent;"
                "border-radius: 8px;"
                "}"

                "QSlider::sub-page:horizontal {"
                "background: #2FB3E8;"
                "border-radius: 8px;"
                "}"
            );

    }

    if(e->button()==Qt::LeftButton) {
          clickState = true;
          emit  ycoordinate(e->x());
    }
}

void SliderButtonMajor::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) {
        clickState = false;
    }
}

void SliderButtonMajor::mouseMoveEvent(QMouseEvent *event)
{
    if (event->x() <20) {
        setStyleSheet(
               "QSlider::groove:Horizontal {"
                "border: 0px none;"
                "background: #2FB3E8;"
                "height: 20px;"
                "width: 306px;"
                "border-radius: 8px;"
                "}"

                "QSlider::handle:Horizontal {"
                "background: #2FB3E8;"
                "border: 0px none;"
                "width: 20px;"
                "height: 50px;"
                "border-radius: 8px;"
                "margin: 0px 0;"
                "border-image:url(:/image_major/slider.png);"
                "}"

                "QSlider::add-page:Horizontal {"
                "background: #F5F5F5;"
                "border-radius: 8px;"
                "}"

                "QSlider::sub-page:horizontal {"
                "background: transparent;"
                "border-radius: 8px;"
                "}"
       );
    } else {
        setStyleSheet(
               "QSlider::groove:Horizontal {"
                "border: 0px none;"
                "background: #F5F5F5;"
                "height: 20px;"
                "width: 306px;"
                "border-radius: 8px;"
                "}"

                "QSlider::handle:Horizontal {"
                "background: #2FB3E8;"
                "border: 0px none;"
                "width: 20px;"
                "height: 50px;"
                "border-radius: 8px;"
                "margin: 0px 0;"
                "border-image:url(:/image_major/slider.png);"
                "}"

                "QSlider::add-page:Horizontal {"
                "background: transparent;"
                "border-radius: 8px;"
                "}"

                "QSlider::sub-page:horizontal {"
                "background: #2FB3E8;"
                "border-radius: 8px;"
                "}"
            );

    }

    if(clickState) {
        i++ ;
        if(i==5){
            i=0;
            if(event->x()<=0) {
                emit  ycoordinate(0);
            }
            if(event->x()>=306) {
                emit  ycoordinate(306);
            }
            if(event->x()>0 && event->x()<306) {
                emit  ycoordinate(event->x());
            }
        }
    }
}

