#include "selection.h"
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDialog>
#include <QLabel>
#include <QMouseEvent>
#include <QStyle>
#include <QStyleOption>
#include <QPainter>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusConnection>
#include <QDebug>
#include <QMenu>
#include <QTimer>

#include "mypushbutton.h"


SelectWidget::SelectWidget(QWidget *parent)
{
    //  按照设计稿要求设置划词界面固定大小
    this->setFixedSize(302, 36);
    this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::Popup);
    this->setObjectName("mySelectWidget");
    this->setStyleSheet("#mySelectWidget {"
                        "border: 1px solid rgba(38, 38, 38, 0.1);"
                        "border-radius:8px;"
                        "background-color: white;"
                        "}");
    QHBoxLayout *scribeWordsLayout = new QHBoxLayout;

    m_textExpansionButton = new QPushButton(tr("Text Expansion"));
    m_textPolishingButton = new QPushButton(tr("Text Polishing"));
    m_translateButton = new QPushButton(tr("Translation"));
    m_textExpansionButton->setFixedSize(72, 28);
    m_textPolishingButton->setFixedSize(72, 28);
    m_translateButton->setFixedSize(44, 28);

    m_textExpansionButton->setStyleSheet("QPushButton {"
                                         "border-top-left-radius: 6px;"
                                         "border-bottom-left-radius: 6px;"
                                         "border-top-right-radius: 0;"
                                         "border-bottom-right-radius: 0;"
                                         "background-color:rgba(38, 38, 38, 0.1);"
                                         "}"
                                         "QPushButton:hover {"
                                         "background-color: rgba(38, 38, 38, 0.3);"
                                         "}");
    m_textPolishingButton->setStyleSheet("QPushButton {"
                                         "border-top-left-radius: 0;"
                                         "border-bottom-left-radius: 0;"
                                         "border-top-right-radius: 0;"
                                         "border-bottom-right-radius: 0;"
                                         "background-color:rgba(38, 38, 38, 0.1);"
                                         "}"
                                         "QPushButton:hover {"
                                         "background-color: rgba(38, 38, 38, 0.3);"
                                         "}");
    m_translateButton->setStyleSheet("QPushButton {"
                                     "border-top-left-radius: 0;"
                                     "border-bottom-left-radius: 0;"
                                     "border-top-right-radius: 6px;"
                                     "border-bottom-right-radius: 6px;"
                                     "margin-right: 4px;"
                                     "background-color:rgba(38, 38, 38, 0.1);"
                                     "}"
                                     "QPushButton:hover {"
                                     "background-color: rgba(38, 38, 38, 0.3);"
                                     "}");

     connect(m_textExpansionButton, &QPushButton::clicked, this, &SelectWidget::sendButtonText);
     connect(m_textPolishingButton, &QPushButton::clicked, this, &SelectWidget::sendButtonText);
     connect(m_translateButton, &QPushButton::clicked, this, &SelectWidget::sendButtonText);

    QIcon upicon(QString(":/icon/icon/subscript.svg"));
    m_totalButton = new CustomButton(tr("total"), upicon);
    m_totalButton->setFixedSize(68, 28);
    m_totalButton->setStyleSheet("QPushButton {"
                                 "background-color: rgba(38, 38, 38, 0.1);"
                                 "border-radius:6px;"
                                 "}"
                                 "QPushButton:hover {"
                                 "background-color: rgba(38, 38, 38, 0.3);"
                                 "}"
                                 "QPushButton::label {"
                                 "font-size: 14px;"
                                 "}");

    settingButton = new MyPushButton();
    settingButton->setFixedSize(28, 28);
    QIcon buttonIcon(QString(":/html/html/img/meeting_assistant.svg"));
    settingButton->setIcon(buttonIcon);
    settingButton->setIconSize(QSize(16, 16));
    settingButton->setContentsMargins(0, 0, 0, 0);
    settingButton->setStyleSheet("QPushButton {"
                                 "text-align: center;"
                                 "image: center;"
                                 "background-color: white;"
                                 "border-radius:4px;"
                                 "}"
                                 "QPushButton:hover {"
                                 "background-color: rgba(55, 144, 250, 0.1);"
                                 "}");
    connect(settingButton, &MyPushButton::leftButtonClicked, this, [=]() {
        this->buttonText = "";
        if(instructIsVisible)
        {
            instructPage->hide();
            instructIsVisible = false;
            m_totalButton->setIconLabelPixmap(downScaledPixmap);
        }
        emit signalSendClicpText();
    });
    connect(settingButton, &MyPushButton::rightButtonClicked, this, [=]() {
        if(instructIsVisible)
        {
            instructPage->hide();
            instructIsVisible = false;
            m_totalButton->setIconLabelPixmap(downScaledPixmap);
        }
        connect(settingButton, &MyPushButton::sigSelectWidgetClose, this,[=](){
            if(instructIsVisible)
            {
                instructPage->hide();
                instructIsVisible = false;
                m_totalButton->setIconLabelPixmap(downScaledPixmap);
            }
            this->close();
        });
        connect(settingButton, &MyPushButton::sigHomePageShow, this,[=](){
            if(instructIsVisible)
            {
                instructPage->hide();
                instructIsVisible = false;
                m_totalButton->setIconLabelPixmap(downScaledPixmap);
            }
           emit sigHomePageShow();
        });
        connect(settingButton, &MyPushButton::sigSettingShow, this,[=](){
            if(instructIsVisible)
            {
                instructPage->hide();
                instructIsVisible = false;
                m_totalButton->setIconLabelPixmap(downScaledPixmap);
            }
           emit sigSettingShow();
        });
        connect(settingButton, &MyPushButton::sigDisableScribe, this,[=](){
            if(instructIsVisible)
            {
                instructPage->hide();
                instructIsVisible = false;
                m_totalButton->setIconLabelPixmap(downScaledPixmap);
            }
           emit sigDisableScribe();
        });
    });



     scribeWordsLayout->addWidget(m_textExpansionButton);
     scribeWordsLayout->addWidget(m_textPolishingButton);
     scribeWordsLayout->addWidget(m_translateButton);
     scribeWordsLayout->setSpacing(2);
     scribeWordsLayout->addWidget(m_totalButton);
     scribeWordsLayout->addWidget(settingButton);
     scribeWordsLayout->setAlignment(Qt::AlignCenter);
     scribeWordsLayout->setContentsMargins(2, 4, 4, 4);
     this->setLayout(scribeWordsLayout);

     instructPage = new QDialog;
     instructPage->setWindowFlags(Qt::FramelessWindowHint | Qt::Window | Qt::Tool); // 添加这一行来移除窗口标题栏
     instructPage->setObjectName("instructWidget");
     instructPage->setStyleSheet("#instructWidget {"
                                 "border: 1px solid rgba(38, 38, 38, 0.1);"
                                 "border-radius:8px;"
                                 "background-color:white;"
                                 "}");

     QVBoxLayout *totalLayout = new QVBoxLayout;
     QLabel *label = new QLabel("全部指令(6)");
     totalLayout->addWidget(label);

     /*m_totalButton点击设置*/
     QPixmap downIcon(":/icon/icon/subscript.svg");
     QPixmap upIcon(":/icon/icon/superscript.svg");
     upScaledPixmap = upIcon.scaled(16, 16, Qt::KeepAspectRatio, Qt::SmoothTransformation);
     downScaledPixmap = downIcon.scaled(16, 16,Qt::KeepAspectRatio, Qt::SmoothTransformation);
     QObject::connect(m_totalButton, &QPushButton::clicked, [=] {
         if (instructPage->isVisible()) {
             instructPage->hide();
             instructIsVisible = false;
             m_totalButton->setIconLabelPixmap(downScaledPixmap);
         } else {
             QRect parentGeometry = this->frameGeometry();
             QPoint bottomLeft = parentGeometry.bottomLeft();
             QPoint bottomCenter(bottomLeft.x(), bottomLeft.y() + 4);
             QPoint newPosition = bottomCenter - QPoint(12, 0);
             instructPage->move(newPosition);
             instructPage->show();
             instructIsVisible = true;
             m_totalButton->setIconLabelPixmap(upScaledPixmap);
         }
     });
     label->setStyleSheet("QLabel {"
                          "width:100px;"
                          "}");
     /*指令界面功能实现*/
     QHBoxLayout *firstLineButtonLayout = new QHBoxLayout;
     QHBoxLayout *secondLineButtonLayout = new QHBoxLayout;

     QPushButton *contentButton = new QPushButton(tr("Content Inspect"));
     QPushButton *summaryButton = new QPushButton(tr("Summary"));
     QPushButton *emailButton = new QPushButton(tr("Email Reply"));
     QPushButton *workButton = new QPushButton(tr("Work Summary"));
     QPushButton *codeButton = new QPushButton(tr("Code Generat"));
     QPushButton *scheduleButton = new QPushButton(tr("New Schedule")); //新建日程

     contentButton->setFixedSize(72, 28);
     summaryButton->setFixedSize(72, 28);
     emailButton->setFixedSize(72, 28);
     workButton->setFixedSize(72, 28);
     codeButton->setFixedSize(72, 28);
     scheduleButton->setFixedSize(72, 28);

     contentButton->setStyleSheet("QPushButton {"
                                  "border-radius: 4px;"
                                  "color: rgba(55, 144, 250, 0.9);"
                                  "background-color: rgba(55, 144, 250, 0.1);"
                                  "}"
                                  "QPushButton:hover {"
                                  "color: white;"
                                  "background-color: rgba(55, 144, 250, 0.8);}"
                                  "QPushButton:pressed {"
                                  "color: white;"
                                  "background-color: rgb(55, 144, 250);}"
                                  "QPushButton:disabled {"
                                  "color: rgba(55, 144, 250, 0.5);"
                                  "background-color: rgba(55, 100, 140, 0.1);"
                                  "}");
     summaryButton->setStyleSheet("QPushButton {"
                                  "border-radius: 4px;"
                                  "color: rgba(55, 144, 250, 0.9);"
                                  "background-color: rgba(55, 144, 250, 0.1);"
                                  "}"
                                  "QPushButton:hover {"
                                  "color: white;"
                                  "background-color: rgba(55, 144, 250, 0.8);}"
                                  "QPushButton:pressed {"
                                  "color: white;"
                                  "background-color: rgb(55, 144, 250);}"
                                  "QPushButton:disabled {"
                                  "color: rgba(55, 144, 250, 0.5);"
                                  "background-color: rgba(55, 100, 140, 0.1);"
                                  "}");
     emailButton->setStyleSheet("QPushButton {"
                                "border-radius: 4px;"
                                "color: rgba(55, 144, 250, 0.9);"
                                "background-color: rgba(55, 144, 250, 0.1);"
                                "}"
                                "QPushButton:hover {"
                                "color: white;"
                                "background-color: rgba(55, 144, 250, 0.8);}"
                                "QPushButton:pressed {"
                                "color: white;"
                                "background-color: rgb(55, 144, 250);}"
                                "QPushButton:disabled {"
                                "color: rgba(55, 144, 250, 0.5);"
                                "background-color: rgba(55, 100, 140, 0.1);"
                                "}");
     workButton->setStyleSheet("QPushButton {"
                               "border-radius: 4px;"
                               "color: rgba(55, 144, 250, 0.9);"
                               "background-color: rgba(55, 144, 250, 0.1);"
                               "}"
                               "QPushButton:hover {"
                               "color: white;"
                               "background-color: rgba(55, 144, 250, 0.8);}"
                               "QPushButton:pressed {"
                               "color: white;"
                               "background-color: rgb(55, 144, 250);}"
                               "QPushButton:disabled {"
                               "color: rgba(55, 144, 250, 0.5);"
                               "background-color: rgba(55, 100, 140, 0.1);"
                               "}");
     codeButton->setStyleSheet("QPushButton {"
                               "border-radius: 4px;"
                               "color: rgba(55, 144, 250, 0.9);"
                               "background-color: rgba(55, 144, 250, 0.1);"
                               "}"
                               "QPushButton:hover {"
                               "color: white;"
                               "background-color: rgba(55, 144, 250, 0.8);}"
                               "QPushButton:pressed {"
                               "color: white;"
                               "background-color: rgb(55, 144, 250);}"
                               "QPushButton:disabled {"
                               "color: rgba(55, 144, 250, 0.5);"
                               "background-color: rgba(55, 100, 140, 0.1);"
                               "}");

     scheduleButton->setStyleSheet("QPushButton {"
                               "border-radius: 4px;"
                               "color: rgba(55, 144, 250, 0.9);"
                               "background-color: rgba(55, 144, 250, 0.1);"
                               "}"
                               "QPushButton:hover {"
                               "color: white;"
                               "background-color: rgba(55, 144, 250, 0.8);}"
                               "QPushButton:pressed {"
                               "color: white;"
                               "background-color: rgb(55, 144, 250);}"
                               "QPushButton:disabled {"
                               "color: rgba(55, 144, 250, 0.5);"
                               "background-color: rgba(55, 100, 140, 0.1);"
                               "}");


     // 实现指令模板命令标签发送
     connect(contentButton, &QPushButton::clicked, this, &SelectWidget::sendButtonText);
     connect(summaryButton, &QPushButton::clicked, this, &SelectWidget::sendButtonText);
     connect(emailButton, &QPushButton::clicked, this, &SelectWidget::sendButtonText);
     connect(workButton, &QPushButton::clicked, this, &SelectWidget::sendButtonText);
     connect(codeButton, &QPushButton::clicked, this, &SelectWidget::sendButtonText);
     connect(scheduleButton, &QPushButton::clicked, this, &SelectWidget::sendButtonText);

     firstLineButtonLayout->addWidget(contentButton);
     firstLineButtonLayout->addWidget(summaryButton);
     firstLineButtonLayout->addWidget(emailButton);
     firstLineButtonLayout->addWidget(workButton);
     secondLineButtonLayout->addWidget(codeButton);
     secondLineButtonLayout->addWidget(scheduleButton);

     firstLineButtonLayout->setAlignment(Qt::AlignLeft);
      secondLineButtonLayout->setAlignment(Qt::AlignLeft);
     totalLayout->addLayout(firstLineButtonLayout);
     totalLayout->addLayout(secondLineButtonLayout);
     instructPage->setLayout(totalLayout);

}

void SelectWidget::sendButtonText()
{
    // 关闭指令模板
    if(instructIsVisible)
    {
        instructPage->hide();
        instructIsVisible = false;
        m_totalButton->setIconLabelPixmap(downScaledPixmap);
    }
    QPushButton *button = qobject_cast<QPushButton *>(sender());
    if (button)
    {
        buttonText = button->text();
    }
    emit sigbuttontext();
}

QString SelectWidget::getButtonText()
{
    return buttonText;
}

void SelectWidget::mousePressEvent(QMouseEvent *event)
{
    m_nMouseClick_X_Coordinate = event->x();
    m_nMouseClick_Y_Coordinate = event->y();
}

void SelectWidget::mouseMoveEvent(QMouseEvent *event)
{
    move(event->globalX()-m_nMouseClick_X_Coordinate,event->globalY()-m_nMouseClick_Y_Coordinate);
}

void SelectWidget::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void SelectWidget::insertText(const QString &text)
{
    QDBusInterface interface("org.ukui.inputmethod",
                             "/commitstring",
                             "org.ukui.inputmethod.commitstring",
                             QDBusConnection::sessionBus());

    interface.call("CommitString", text);
}

void SelectWidget::closeWidgetAndinstructPage(int x, int y)
{
    QRect geoInstructRect = instructPage->geometry();
    QRect geoSettingRightClickMenu = settingButton->settingRightClickMenu->geometry();
    if(!geoInstructRect.contains(x, y) && !geoSettingRightClickMenu.contains(x, y) && instructIsVisible)
    {
        instructPage->hide();
        instructIsVisible = false;
        m_totalButton->setIconLabelPixmap(downScaledPixmap);
        settingButton->settingRightClickMenu->hide();
        this->close();
    }else if(!geoInstructRect.contains(x, y) && !geoSettingRightClickMenu.contains(x, y) && this->isVisible()){
        settingButton->settingRightClickMenu->hide();
        this->close();
    }
}

void SelectWidget::showScribeWordsAtCursorPosition()
{
    // 获取鼠标光标位置
    QPoint cursorPos = QCursor::pos();
    // 调整弹出窗口位置，确保文本可见
    move(cursorPos);
    // 关闭指令模板
    if(instructIsVisible)
    {
        instructPage->hide();
        instructIsVisible = false;
        m_totalButton->setIconLabelPixmap(downScaledPixmap);
    }
    close();
    show();
}
