/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include "switchbutton.h"

#include <QDebug>
#include <QPainterPath>
#include "global_variable.h"
#include "gsettings_monitor.h"

#define THEME_GTK_SCHEMA "org.mate.interface"

SwitchButton::SwitchButton(QWidget *parent) :
    QWidget(parent)
{
//    this->resize(QSize(52, 24));
    this->setFixedSize(QSize(48, 24));

    m_checked = false;

    m_borderColorOff = QColor("#cccccc");

    m_bgColorOff = QColor("#cccccc");
    m_bgColorOn = QColor("#3D6BE5");

    m_sliderColorOff = QColor("#ffffff");
    m_sliderColorOn = QColor("#ffffff");

    m_space = 4;
//    rectRadius = 5;

    m_step = width() / 40;
    m_startX = 0;
    m_endX= 0;

    m_timer = new QTimer(this);
    m_timer->setInterval(5);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(updatevalue()));
    if(QGSettings::isSchemaInstalled(THEME_GTK_SCHEMA) && GsettingsMonitor::getInstance()->gsettingsCheck()) {
        QByteArray gtkThemeID(THEME_GTK_SCHEMA);

        m_gtkThemeSetting = new QGSettings(gtkThemeID,QByteArray(),this);

        QString style = GsettingsMonitor::getInstance()->sysStyleNameGet();
        if(style == "ukui-dark" || style == "ukui-black") {
            m_bgColorOff = QColor("#3d3d3f");
        } else {
            m_bgColorOff = QColor("#cccccc");
        }

        connect(GsettingsMonitor::getInstance(),&GsettingsMonitor::sysStyleNameUpdate, [this] (const QString &style) {
            if(style == "ukui-dark" || style == "ukui-black") {
                m_bgColorOff = QColor("#3d3d3f");
            } else {
                m_bgColorOff = QColor("#cccccc");
            }
        });
    }
}

SwitchButton::~SwitchButton()
{
}

void SwitchButton::paintEvent(QPaintEvent *){
    //启用反锯齿
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    drawBg(&painter);
    drawSlider(&painter);
}

void SwitchButton::drawBg(QPainter *painter){
    painter->save();
//    painter->setPen(Qt::NoPen);

    if (!m_checked){
        painter->setPen(Qt::NoPen);
        painter->setBrush(m_bgColorOff);
    }
    else{
        painter->setPen(Qt::NoPen);
        painter->setBrush(m_bgColorOn);
    }
    //circle out
//    QRect rect(space, space, width() - space * 2, height() - space * 2);
//    painter->drawRoundedRect(rect, rectRadius, rectRadius);

    //circle in
    QRect rect(0, 0, width(), height());
    //半径为高度的一半
    int radius = rect.height() / 2;
    //圆的宽度为高度
    int circleWidth = rect.height();

    QPainterPath path;
    path.moveTo(radius, rect.left());
    path.arcTo(QRectF(rect.left(), rect.top(), circleWidth, circleWidth), 90, 180);
    path.lineTo(rect.width() - radius, rect.height());
    path.arcTo(QRectF(rect.width() - rect.height(), rect.top(), circleWidth, circleWidth), 270, 180);
    path.lineTo(radius, rect.top());

    painter->drawPath(path);

    painter->restore();
}

void SwitchButton::drawSlider(QPainter *painter){
    painter->save();
    painter->setPen(Qt::NoPen);

    if (!m_checked){
        painter->setBrush(m_sliderColorOff);
    }
    else
        painter->setBrush(m_sliderColorOn);
    //circle out
//    QRect rect(0, 0, width() - space, height() - space);
//    int sliderwidth = rect.height();
//    QRect sliderRect(startX, space / 2, sliderwidth, sliderwidth);
//    painter->drawEllipse(sliderRect);

    //circle in
    QRect rect(0, 0, width(), height());
    int sliderWidth = rect.height() - m_space * 2;
    QRect sliderRect(m_startX + m_space, m_space, sliderWidth, sliderWidth);
    painter->drawEllipse(sliderRect);

    painter->restore();
}

void SwitchButton::mousePressEvent(QMouseEvent *){
    m_checked = !m_checked;
    emit checkedChanged(m_checked);

    m_step = width() / 40;

    if (m_checked){
        //circle out
//        endX = width() - height() + space;
        //circle in
        m_endX = width() - height();
    }
    else{
        m_endX = 0;
    }
    m_timer->start();
}

void SwitchButton::resizeEvent(QResizeEvent *){
    //
    m_step = width() / 40;

    if (m_checked){
        //circle out
//        startX = width() - height() + space;
        //circle in
        m_startX = width() - height();
    }
    else
        m_startX = 0;

    update();
}

void SwitchButton::updatevalue(){
    if (m_checked)
        if (m_startX < m_endX){
            m_startX = m_startX + m_step;
        }
        else{
            m_startX = m_endX;
            m_timer->stop();
        }
    else{
        if (m_startX > m_endX){
            m_startX = m_startX - m_step;
        }
        else{
            m_startX = m_endX;
            m_timer->stop();
        }
    }
    update();
}

void SwitchButton::setChecked(bool checked){
    if (this->m_checked != checked){
        this->m_checked = checked;
        emit checkedChanged(checked);
        update();
    }

    m_step = width() / 40;

    if (checked){
        //circle out
//        endX = width() - height() + space;
        //circle in
        m_endX = width() - height();
    }
    else{
        m_endX = 0;
    }
    m_timer->start();
}

bool SwitchButton::isChecked(){
    return this->m_checked;
}


