#include "bluetoothsettinglabel.h"

BluetoothSettingLabel::BluetoothSettingLabel(QWidget *parent) : QLabel(parent)
{
    _MStatus = Normal;
    this->setFixedSize(420,50);
    if(QGSettings::isSchemaInstalled("org.ukui.style")){
        StyleSettings = new QGSettings("org.ukui.style");
        connect(StyleSettings,&QGSettings::changed,this,[=]{
            this->update();
        });
    }
    const QByteArray transparency_id(TRANSPARENCY_SETTINGS);
    if(QGSettings::isSchemaInstalled(transparency_id)){
        transparency_gsettings = new QGSettings(transparency_id);
    }
    if(QGSettings::isSchemaInstalled(transparency_id)){
       tran=transparency_gsettings->get(TRANSPARENCY_KEY).toDouble()*255;
       connect(transparency_gsettings, &QGSettings::changed, this, [=] {
           tran=transparency_gsettings->get(TRANSPARENCY_KEY).toDouble()*255;
           this->update();
       });
    }
}

BluetoothSettingLabel::~BluetoothSettingLabel() {

}

QColor BluetoothSettingLabel::getPainterBrushColor()
{
    QColor color;
    switch (_MStatus) {
    case Status::Normal:
        color = this->palette().color(QPalette::Active, QPalette::ButtonText);
        break;
    case Status::Hover:
        color = this->palette().color(QPalette::Active, QPalette::Highlight);
        break;
    case Status::Click:
        color = this->palette().color(QPalette::Active, QPalette::Highlight).darker(105);
        break;
    default:
        color = this->palette().color(QPalette::Active, QPalette::ButtonText);
        break;
    }
    return color;
}

void BluetoothSettingLabel::setFocusStyle(bool val) {
    focused = val;
    if(val)
        _MStatus = Hover;
    else
        _MStatus = Normal;
    this->update();
}

void BluetoothSettingLabel::enterEvent(QEvent *event) {
    Q_UNUSED(event);
    if (focused)
        return;
    _MStatus = Hover;
    setCursor(Qt::PointingHandCursor);
    this->update();
}

void BluetoothSettingLabel::leaveEvent(QEvent *event) {
    Q_UNUSED(event);
    if (focused)
        return;
    _MStatus = Normal;
    this->update();
}

void BluetoothSettingLabel::mousePressEvent(QMouseEvent *ev) {
    Q_UNUSED(ev);
    _MStatus = Click;
    this->update();
}

void BluetoothSettingLabel::mouseReleaseEvent(QMouseEvent *ev) {
    Q_UNUSED(ev);
    _MStatus = Normal;
    this->update();
//    Config::OpenBluetoothSettings();
    emit clickSignal();
    emit hideFeature();
}

void BluetoothSettingLabel::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.save();
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(QColor(Qt::transparent));
    painter.drawRoundedRect(this->rect(),12,12,Qt::AbsoluteSize);
    //painter.drawRect(this->rect());
    painter.restore();
    if(StyleSettings->get("style-name").toString() == "ukui-light" || StyleSettings->get("style-name").toString() == "ukui-default")
        painter.setPen(QColor(Qt::black));
    else
        painter.setPen(QColor(Qt::white));
    painter.setPen(getPainterBrushColor());
    painter.drawText(25,10,420,50,Qt::AlignLeft,tr("Bluetooth Settings"));

    //焦点预选框
    if (!focused)
        return;
    QPainter pt(this);
    QColor color = QColor("#818181");
    pt.setPen(color);
    QRect rect = this->rect();
    rect.setWidth(this->rect().width());
    rect.setHeight(this->rect().height() - 1);
    pt.setRenderHint(QPainter::Antialiasing);
    pt.drawRoundedRect(rect,12,12,Qt::AbsoluteSize);
}
