#include "activeconnectionwidget.h"
#include "config/xatom-helper.h"
#include "config/config.h"

ActiveConnectionWidget::ActiveConnectionWidget(QString address, QString name, QString type, int rssi) :
    devAddr(address),
    devName(name),
    devType(type),
    devRSSI(rssi)
{
    if(QGSettings::isSchemaInstalled("org.ukui.style")){
        settings = new QGSettings("org.ukui.style");
        connect(settings,&QGSettings::changed,this,&ActiveConnectionWidget::GSettingsChanges);
    }

    //窗管协议
    MotifWmHints hints;
    hints.flags = MWM_HINTS_FUNCTIONS|MWM_HINTS_DECORATIONS;
    hints.functions = MWM_FUNC_ALL;
    hints.decorations = MWM_DECOR_BORDER;
    XAtomHelper::getInstance()->setWindowMotifHint(this->winId(), hints);

    QPalette palette;
    if(settings->get("style-name").toString() == "ukui-default" || settings->get("style-name").toString() == "ukui-light"){
        palette.setColor(QPalette::Background,QColor(255,255,255));
    }else{
        palette.setColor(QPalette::Background,QColor(0,0,0));
    }
    this->setWindowIcon(QIcon::fromTheme("bluetooth"));
    this->setWindowTitle(tr("Bluetooth Connection"));

    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setPalette(palette);
    this->setAutoFillBackground(true);
    this->setFixedSize(420, 154);

    window_icon = new QLabel(this);
    window_icon->setPixmap(QIcon::fromTheme("preferences-system-bluetooth").pixmap(22,22));

    window_title = new QLabel(tr("Bluetooth Connections"),this);
    window_title->setFixedSize(300,20);

    window_close = new QPushButton(this);
    window_close->setIcon(QIcon::fromTheme("window-close-symbolic"));
    window_close->setProperty("isWindowButton", 0x2);
    window_close->setProperty("useIconHighlightEffect", 0x8);
    window_close->setFlat(true);
    connect(window_close, &QPushButton::clicked, this, &ActiveConnectionWidget::onClick_close_btn);

    tip_txt = new QLabel(this);
    tip_txt->setFixedWidth(380);
    QFontMetrics fontMetrics(tip_txt->font());
    QString tip = QString(tr("Found audio device \"") + devName + tr("\", connect it or not?"));
    QString _tip = fontMetrics.elidedText(tip,
                                         Qt::ElideMiddle,
                                         tip_txt->width());
    if (tip != _tip)
        tip_txt->setToolTip(tip);
    tip_txt->setText(_tip);

    connect_btn = new QPushButton(tr("Connect"),this);
    connect(connect_btn, &QPushButton::clicked, this, &ActiveConnectionWidget::onClick_connect_btn);
    cancel_btn = new QPushButton(tr("Cancel"),this);
    connect(cancel_btn, &QPushButton::clicked, this, &ActiveConnectionWidget::onClick_cancel_btn);

    window_icon->setGeometry(8,8,22,22);
    window_title->setGeometry(36, 9, 300, 20);
    window_close->setGeometry(385,4,30,30);
    tip_txt->setGeometry(24,48,380,28);
    cancel_btn->setGeometry(188,94,96,36);
    connect_btn->setGeometry(300,94,96,36);

    QRect   desktop;
    QDesktopWidget *desktop_widget = QApplication::desktop();
    desktop = desktop_widget->availableGeometry();
    this->move(QPoint(desktop.right() - this->width() - 4,desktop.bottom() - this->height() - 4));
}

ActiveConnectionWidget::~ActiveConnectionWidget()
{
    qDebug() << Q_FUNC_INFO;
}

void ActiveConnectionWidget::onClick_close_btn()
{
    qDebug() << Q_FUNC_INFO << "close";
    onClick_cancel_btn();
}

void ActiveConnectionWidget::onClick_connect_btn()
{
    qDebug() << Q_FUNC_INFO << "accpet";
    emit replyActiveConnection(devAddr, true);
    this->hide();
    this->deleteLater();
}

void ActiveConnectionWidget::onClick_cancel_btn()
{
    qDebug() << Q_FUNC_INFO << "cancel";
    emit replyActiveConnection(devAddr, false);
    this->hide();
    this->deleteLater();
}

void ActiveConnectionWidget::GSettingsChanges(const QString &key)
{
    QPalette palette;
    qDebug() << Q_FUNC_INFO << key;
    if(key == "styleName"){
        if(settings->get("style-name").toString() == "ukui-default" || settings->get("style-name").toString() == "ukui-light"){
            palette.setColor(QPalette::Background,QColor(Qt::white));
        }else{
            palette.setColor(QPalette::Background,QColor(Qt::black));
        }
        this->setPalette(palette);
        this->update();
    }
    if (key == "systemFontSize") {
        QFontMetrics fontMetrics(tip_txt->font());
        QString tip = fontMetrics.elidedText(QString(tr("Found audio device \"") + devName + tr("\", connect it or not?")),
                                             Qt::ElideMiddle,
                                             tip_txt->width());
        tip_txt->setText(tip);
    }
}

QPushButton* ActiveConnectionWidget::getButton() {
    switch (pressCnt) {
    case 1:
        return connect_btn;
    case 2:
        return cancel_btn;
    case 3:
        return window_close;
    default:
        return nullptr;
    }
}

void ActiveConnectionWidget::paintEvent(QPaintEvent *event) {
    QPainter pt(this);
    QColor color = QColor("#818181");
    pt.setPen(color);
    selectedBtn = getButton();
    if (selectedBtn == nullptr)
        return;
    QRect rect = selectedBtn->geometry();
    pt.setRenderHint(QPainter::Antialiasing);
    pt.drawRect(rect);
}

void ActiveConnectionWidget::keyPressEvent(QKeyEvent *event) {
    switch (event->key()) {
    case Qt::Key_Tab:
        qDebug() << "press Tab";
        ++pressCnt;
        if (pressCnt == 4)
            pressCnt = 1;
        this->update();
        break;
    case Qt::Key_Enter:
    case Qt::Key_Return:
        qDebug() << "press Enter";
        if (selectedBtn == nullptr)
            return;
        selectedBtn->clicked();
        break;
    case Qt::Key_Escape:
        qDebug() << "press Esc";
        if (pressCnt == 0)
            this->close();
        pressCnt = 0;
        this->update();
        break;
    default:
        break;
    }
}
