#include "kafloatingbarpage.h"
#include "global/kaglobal.h"
#include "libukcc/widgets/SettingWidget/settinggroup.h"

#include <QLabel>
#include <QSharedPointer>

#include "kswitchbutton.h"

using namespace kdk;

class KAFloatingBarPage::Private
{
public:
    QLabel *mWordTitleLabel = nullptr;
    UkccFrame *mEnableFrame = nullptr;
    KSwitchButton *mEnableBtn =nullptr;  // 允许启用悬浮条
    QLabel *mEnableLabel = nullptr;
    bool isMEnableBtn = false;
};

KAFloatingBarPage::KAFloatingBarPage(QWidget *parent)
    : QWidget{parent},
      d_ptr(make_shared<Private>())
{
    initLayout();
}

KAFloatingBarPage::~KAFloatingBarPage()
{

}

void KAFloatingBarPage::initLayout()
{
    QVBoxLayout *base_vlayout = new QVBoxLayout(this);

    d_ptr->mWordTitleLabel = new QLabel(this);
    d_ptr->mWordTitleLabel->setText(tr("Word Selection Management"));
    d_ptr->mWordTitleLabel->setContentsMargins(20, 0, 0, 0);
    QFont font("Noto Sans CJK SC", 11, QFont::DemiBold, false);
    d_ptr->mWordTitleLabel->setFont(font);
    base_vlayout->addWidget(d_ptr->mWordTitleLabel);

    d_ptr->mEnableFrame = new UkccFrame(this);
    d_ptr->mEnableFrame->setMinimumSize(QSize(490, 60));
    d_ptr->mEnableFrame->setMaximumSize(QSize(16777215, 60));
    d_ptr->mEnableFrame->setFrameShape(QFrame::Box);
    d_ptr->mEnableFrame->setObjectName(QString("enableFrame"));

    QHBoxLayout *enableHLayout = new QHBoxLayout(d_ptr->mEnableFrame);
    enableHLayout->setContentsMargins(20, 10, 20, 10);

    d_ptr->mEnableLabel = new QLabel(tr("Enable floating bar for word selection"), this);
    d_ptr->mEnableLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    enableHLayout->addWidget(d_ptr->mEnableLabel);
    enableHLayout->addStretch();
    d_ptr->mEnableBtn = new KSwitchButton(this);
    d_ptr->mEnableBtn->setObjectName("enable");
    d_ptr->mEnableBtn->setChecked(true);
    d_ptr->isMEnableBtn = true;
    connect(d_ptr->mEnableBtn, &KSwitchButton::clicked, this, &KAFloatingBarPage::mEnableBtnSetting);
    enableHLayout->addWidget(d_ptr->mEnableBtn);
    d_ptr->mEnableLabel->setStyleSheet("QLabel{"
                                          "font-family: Noto Sans CJK SC;"
                                          "font-size: 14px;"
                                          "font-weight: 400;"
                                          "line-height: 20px;"
                                          "text-align: left;"
                                          "}");
    base_vlayout->addWidget(d_ptr->mWordTitleLabel);
    base_vlayout->setSpacing(8);
    base_vlayout->addWidget(d_ptr->mEnableFrame);
    base_vlayout->addStretch();
}

void KAFloatingBarPage::initConnection()
{

}

void KAFloatingBarPage::mEnableBtnDisabled()
{
    if(d_ptr->mEnableBtn && d_ptr->isMEnableBtn)
    {
        d_ptr->mEnableBtn->setChecked(false);
        d_ptr->isMEnableBtn = false;
    }
}

void KAFloatingBarPage::mEnableBtnSetting()
{
    d_ptr->isMEnableBtn = !d_ptr->isMEnableBtn;
    d_ptr->mEnableBtn->setChecked(d_ptr->isMEnableBtn);
    if(d_ptr->isMEnableBtn)
    {
        emit sigShowScribeWords();
    }else{
        emit sigCloseScribeWords();
    }
}
