/*
 * Copyright (C) 2002  KylinSoft 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
#include "menumodule.h"
#include "xatom-helper.h"
#include "constant_class.h"
#define STYLE_NAME_VALUE_DEFAULT  "ukui-default"
#define STYLE_NAME_VALUE_DARK     "ukui-dark"
#define STYLE_NAME_VALUE_BLACK    "ukui-black"
#define STYLE_NAME_VALUE_LIGHT    "ukui-light"
#define STYLE_NAME_VALUE_WHITE    "ukui-white"
#include "ukuistylehelper/ukuistylehelper.h"
#include "windowmanager/windowmanager.h"
menuModule::menuModule(QWidget *parent = nullptr) : QWidget(parent)
{
    if(QString(qgetenv("XDG_SESSION_TYPE")) == "wayland") {
        isWayland = true;
    }
    if(isWayland){
        kdk::UkuiStyleHelper::self()->removeHeader(this);
    }
    //initGsetting();
    init();
}

void menuModule::init(){
    initAction();
}

void menuModule::initAction(){
//    titleIcon = new QPushButton();
//     bodyIcon = new QPushButton();
    titleIcon = new QLabel();
    bodyIcon = new QLabel();
    aboutWindow = new QWidget();
    sucbackWindow = new QWidget();
    body =new QWidget();
    titleText = new QLabel();
    bodyAppName = new QLabel();
    bodyAppDesc = new QLabel();
    bodyAppVersion = new QLabel();
    bodySupport = new QLabel();
    toollabel=new QLabel();
    submitbtn = new QPushButton();
    textedit=new QTextEdit();
    menuButton = new QToolButton;
    menuButton->setToolTip(tr("Menu"));
    menuButton->setProperty("isWindowButton", 0x1);
    menuButton->setProperty("useIconHighlightEffect", 0x2);
    menuButton->setPopupMode(QToolButton::InstantPopup);
    menuButton->setFixedSize(30,30);
    menuButton->setIconSize(QSize(16, 16));
    menuButton->setAutoRaise(true);
    menuButton->setIcon(QIcon::fromTheme("open-menu-symbolic"));

    m_menu = new QMenu();
    QList<QAction *> actions ;
    actionTheme = new QAction(m_menu);
    actionTheme->setText(tr("Theme"));
    actionHelp = new QAction(m_menu);
    actionHelp->setText(tr("Help"));
    actionAbout = new QAction(m_menu);
    actionAbout->setText(tr("About"));
    actionsetting = new QAction(m_menu);
    actionsetting->setText(tr("Setting"));
    actionQuit = new QAction(m_menu);
    actionQuit->setText(tr("Quit"));
   // actions<<actionTheme<<actionHelp<<actionAbout<<actionsetting<<actionQuit;
    actions<<actionHelp<<actionAbout<<actionQuit;
    m_menu->addActions(actions);
//    互斥按钮组
//    QMenu *themeMenu = new QMenu;
//    QActionGroup *themeMenuGroup = new QActionGroup(this);
//    autoTheme = new QAction(tr("Auto"),this);
//    themeMenuGroup->addAction(autoTheme);
//    themeMenu->addAction(autoTheme);
//    autoTheme->setCheckable(true);
//    lightTheme = new QAction(tr("Light"),this);
//    themeMenuGroup->addAction(lightTheme);
//    themeMenu->addAction(lightTheme);
//    lightTheme->setCheckable(true);
//    darkTheme = new QAction(tr("Dark"),this);
//    themeMenuGroup->addAction(darkTheme);
//    themeMenu->addAction(darkTheme);
//    darkTheme->setCheckable(true);
//    QList<QAction* > themeActions;
//    themeActions<<autoTheme<<lightTheme<<darkTheme;
//    actionTheme->setMenu(themeMenu);
//    QMenu *helpMenu = new QMenu;
//    QActionGroup *helpMenuGroup = new QActionGroup(this);
//    checkupdatehelp=new QAction(tr("checkupdate"),this);
//    helpMenuGroup->addAction(checkupdatehelp);
//    helpMenu->addAction(checkupdatehelp);
//    checkupdatehelp->setCheckable(true);
//    successbackhelp = new QAction(tr("successback"),this);
//    helpMenuGroup->addAction(successbackhelp);
//    helpMenu->addAction(successbackhelp);
//    successbackhelp->setCheckable(true);
//    officewebsitehelp = new QAction(tr("officewebsite"),this);
//    helpMenuGroup->addAction(officewebsitehelp);
//    helpMenu->addAction(officewebsitehelp);
//    officewebsitehelp->setCheckable(true);
//    QList<QAction* >helpActions;
//    helpActions<<checkupdatehelp<<successbackhelp<<officewebsitehelp;
//    actionHelp->setMenu(helpMenu);
    menuButton->setMenu(m_menu);
    connect(m_menu,&QMenu::triggered,this,&menuModule::triggerMenu);
    initGsetting();
    fontSizeInit();
    //setThemeFromLocalThemeSetting(themeActions);
    themeUpdate();
    connect(themeMenu,&QMenu::triggered,this,&menuModule::triggerThemeMenu);
  //  connect(helpMenu,&QMenu::triggered,this,&menuModule::triggerHelpMenu);
}

//void menuModule::setThemeFromLocalThemeSetting(QList<QAction* > themeActions)
//{
//    m_pGsettingThemeStatus = new QGSettings(APPDATA);
//    QString appConf = m_pGsettingThemeStatus->get("thememode").toString();
//    if("lightonly" == appConf){
//        m_isDarktheme=false;
//        themeStatus = themeLightOnly;
//        themeActions[1]->setChecked(true);   //程序gsetting中为浅色only的时候就给浅色按钮设置checked
//    }else if("darkonly" == appConf){
//        m_isDarktheme=true;
//        themeStatus = themeBlackOnly;
//        themeActions[2]->setChecked(true);
//    }else{
//        themeStatus = themeAuto;
//        themeActions[0]->setChecked(true);
//    }
//}
void menuModule::themeUpdate(){
    if(themeStatus == themeLightOnly)
    {
        m_isDarktheme=false;
        setThemeLight();
    }else if(themeStatus == themeBlackOnly){
        setThemeDark();
        m_isDarktheme=true;
    }else{
        setStyleByThemeGsetting();
    }
}

void menuModule::setStyleByThemeGsetting(){
    QString nowThemeStyle = m_pGsettingThemeData->get("styleName").toString();
    if("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle)
    {
        m_isDarktheme=true;
        setThemeDark();
    }else{
        m_isDarktheme=false;
        setThemeLight();
    }
}

void menuModule::triggerMenu(QAction *act){
    QString str = act->text();
    if(tr("Quit") == str){
        emit menuModuleClose();
    }else if(tr("About") == str){
        aboutAction();
    }else if(tr("Help") == str){
        helpAction();
    }
}

void menuModule::triggerThemeMenu(QAction *act){
    if(!m_pGsettingThemeStatus)
    {
        m_pGsettingThemeStatus = new QGSettings(APPDATA);  //m_pGsettingThemeStatus指针重复使用避免占用栈空间
    }
    QString str = act->text();
    if(tr("Light") == str){
        m_isDarktheme=false;
        themeStatus = themeLightOnly;
        disconnect(m_pGsettingThemeData,&QGSettings::changed,this,&menuModule::dealSystemGsettingChange);
        m_pGsettingThemeStatus->set("thememode","lightonly");
        setThemeLight();
    }else if(tr("Dark") == str){
        m_isDarktheme=true;
        themeStatus = themeBlackOnly;
        disconnect(m_pGsettingThemeData,&QGSettings::changed,this,&menuModule::dealSystemGsettingChange);
        m_pGsettingThemeStatus->set("thememode","darkonly");
        setThemeDark();
    }else{
        themeStatus = themeAuto;
        m_pGsettingThemeStatus->set("thememode","auto");
        initGsetting();
//        updateTheme();
        themeUpdate();
        fontSizeInit();
    }
}
void menuModule::triggerHelpMenu(QAction *act){
    QString str=act->text();
    if(str==tr("successback")){
        successbackAction();
    }
}
void menuModule::successbackAction(){
    initsuccessbackdilog();
}
void menuModule::aboutAction(){
//    关于点击事件处理
    initAbout();
}

void menuModule::helpAction(){
//    帮助点击事件处理
    if(!ipcDbus)
    {
        ipcDbus = new DaemonIpcDbus();
    }
    if(!ipcDbus->daemonIsNotRunning()){
       // ipcDbus->showGuide(appName);
        ipcDbus->showGuide("kylin-usb-creator");
    }
}
void menuModule::initsuccessbackdilog(){
    sucbackWindow->setWindowModality(Qt::ApplicationModal);
    sucbackWindow->setWindowFlag(Qt::Tool);
    MotifWmHints hints;
    hints.flags = MWM_HINTS_FUNCTIONS|MWM_HINTS_DECORATIONS;
    hints.functions = MWM_FUNC_ALL;
    hints.decorations = MWM_DECOR_BORDER;
    XAtomHelper::getInstance()->setWindowMotifHint(sucbackWindow->winId(), hints);
    sucbackWindow->setFixedSize(472,324);
    QVBoxLayout *mainlyt = new QVBoxLayout();
    QHBoxLayout *titleLyt = initTitlesucbackBar();
    textedit->setFixedSize(400,150);
    textedit->setPlaceholderText(tr("Please enter the contents"));
    toollabel->setText(tr("Please enter your feedback"));
    toollabel->setStyleSheet("color:red");
    submitbtn->setText(tr("submit"));
    submitbtn->setFixedSize(100,40);
    body->setFixedSize(420,260);
    QVBoxLayout *vlay=new QVBoxLayout();
    body->setLayout(vlay);
    vlay->addWidget(textedit,0,Qt::AlignHCenter);
    vlay->addWidget(toollabel,0,Qt::AlignLeft);
    vlay->addWidget(submitbtn,0,Qt::AlignRight);
    mainlyt->setMargin(0);
    mainlyt->addLayout(titleLyt);
    mainlyt->addWidget(body,0,Qt::AlignHCenter);
    mainlyt->addStretch(20);
    sucbackWindow->setLayout(mainlyt);
    //弹窗位置应用居中
    QRect availableGeometry = this->parentWidget()->geometry();
    sucbackWindow->move(availableGeometry.center()-sucbackWindow->rect().center());
    sucbackWindow->show();
    connect(textedit,&QTextEdit::textChanged,this,&menuModule::slot_limitTextEditTextCount);

}
void menuModule::slot_limitTextEditTextCount(){
    limitTextEditTextCount(textedit,150);
}
void menuModule::limitTextEditTextCount(QTextEdit* text_edit,const int& limit_count)
{
  QString text = text_edit->toPlainText();
  int count=text.count();
  if(count>limit_count){
      toollabel->setText(tr("Maximum 150 words!"));
      int position = text_edit->textCursor().position();
      QTextCursor text_cursor = text_edit->textCursor();
      text.remove(position - (count - limit_count), count - limit_count);

      text_edit->setText(text);
      text_cursor.setPosition(position - (count - limit_count));
      text_edit->setTextCursor(text_cursor);
  }
}
void menuModule::initAbout(){
    aboutWindow->setWindowModality(Qt::ApplicationModal);
    aboutWindow->setWindowFlag(Qt::Tool);
    if(isWayland){
        kdk::UkuiStyleHelper::self()->removeHeader(aboutWindow);
    }else{
    MotifWmHints hints;
    hints.flags = MWM_HINTS_FUNCTIONS|MWM_HINTS_DECORATIONS;
    hints.functions = MWM_FUNC_ALL;
    hints.decorations = MWM_DECOR_BORDER;
    XAtomHelper::getInstance()->setWindowMotifHint(aboutWindow->winId(), hints);
    }
    aboutWindow->setFixedSize(420,400);
    QVBoxLayout *mainlyt = new QVBoxLayout();
    QHBoxLayout *titleLyt = initTitleBar();
    QVBoxLayout *bodylyt = initBody();
    mainlyt->setMargin(0);
    mainlyt->addLayout(titleLyt);
    mainlyt->addLayout(bodylyt);
    mainlyt->addStretch();
    aboutWindow->setLayout(mainlyt);
//    //TODO:在屏幕中央显示
//    QRect availableGeometry = qApp->primaryScreen()->availableGeometry();
//    aboutWindow->move((availableGeometry.width()-aboutWindow->width())/2,(availableGeometry.height()- aboutWindow->height())/2);
    //弹窗位置应用居中
    QRect availableGeometry = this->parentWidget()->geometry();
    aboutWindow->move(availableGeometry.center()-aboutWindow->rect().center());
    aboutWindow->show();
}
QHBoxLayout*menuModule::initTitlesucbackBar(){
    QPushButton *titleBtnClose = new QPushButton;
    titleBtnClose->setFixedSize(30,30);
    titleBtnClose->setIcon(QIcon::fromTheme("window-close-symbolic"));
    titleBtnClose->setProperty("isWindowButton",0x2);
    titleBtnClose->setProperty("useIconHighlightEffect",0x8);
    titleBtnClose->setFlat(true);
    titleBtnClose->setToolTip(tr("Quit"));
    connect(titleBtnClose,&QPushButton::clicked,[=](){sucbackWindow->close();});
    QHBoxLayout *hlyt = new QHBoxLayout;
    titleText->setText(tr("successback"));
    hlyt->setSpacing(0);
    hlyt->setMargin(4);
    hlyt->addSpacing(4);
    hlyt->addSpacing(8);
    hlyt->addWidget(titleText,0,Qt::AlignBottom);
    hlyt->addStretch();
    hlyt->addWidget(titleBtnClose,0,Qt::AlignBottom);
    return hlyt;
}
QHBoxLayout* menuModule::initTitleBar(){
    QPushButton *titleBtnClose = new QPushButton;
    titleIcon->setFixedSize(QSize(24,24));
    titleIcon->setFocusPolicy(Qt::NoFocus);
    titleIcon->setPixmap(QIcon::fromTheme("kylin-usb-creator").pixmap(24,24));
    titleBtnClose->setFixedSize(30,30);
    titleBtnClose->setIcon(QIcon::fromTheme("window-close-symbolic"));
    titleBtnClose->setProperty("isWindowButton",0x2);
    titleBtnClose->setProperty("useIconHighlightEffect",0x8);
    titleBtnClose->setFlat(true);
    titleBtnClose->setToolTip(tr("Quit"));
    connect(titleBtnClose,&QPushButton::clicked,[=](){aboutWindow->close();});
    QHBoxLayout *hlyt = new QHBoxLayout;
    titleText->setText(tr("usb boot maker"));
    hlyt->setSpacing(0);
    hlyt->setMargin(4);
    hlyt->addSpacing(4);
    hlyt->addWidget(titleIcon,0,Qt::AlignBottom); //居下显示
    hlyt->addSpacing(8);
    hlyt->addWidget(titleText,0,Qt::AlignBottom);
    hlyt->addStretch();
    hlyt->addWidget(titleBtnClose,0,Qt::AlignBottom);
    return hlyt;
}

QVBoxLayout* menuModule::initBody(){
    bodyIcon->setFixedSize(96,96);
    bodyIcon->setPixmap(QIcon::fromTheme("kylin-usb-creator").pixmap(96,96));
    bodyIcon->setFocusPolicy(Qt::NoFocus);
   // bodyIcon->setIcon(QIcon::fromTheme("kylin-usb-creator",QIcon(iconPath)));
  //  bodyIcon->setSize(QSize(96,96));
    bodyAppDesc->setText(tr("USB Boot Maker provides system image making function."
                            "The operation process is simple and easy."
                            "You can choose ISO image and usb driver,"
                            "and make boot driver with a few clicks."));
    bodyAppDesc->setFixedWidth(390);
    bodyAppDesc->setWordWrap(true);
    bodyAppName->setFixedHeight(28);
    bodyAppName->setText(tr("usb boot maker"));
    bodyAppVersion->setFixedHeight(30);
    bodyAppVersion->setText(tr("Version: ") + appVersion);
    bodyAppVersion->setAlignment(Qt::AlignLeft);

    connect(bodySupport,&QLabel::linkActivated,this,[=](const QString url){
        QDesktopServices::openUrl(QUrl(url));
    });
    bodySupport->setContextMenuPolicy(Qt::NoContextMenu);
    bodySupport->setFixedHeight(30);
    QVBoxLayout *vlyt = new QVBoxLayout;
    vlyt->setMargin(0);
    vlyt->setSpacing(0);
    vlyt->addSpacing(12);
    vlyt->addWidget(bodyIcon,0,Qt::AlignHCenter);
    vlyt->addSpacing(12);
    vlyt->addWidget(bodyAppName,0,Qt::AlignHCenter);
    vlyt->addSpacing(12);
    vlyt->addWidget(bodyAppVersion,0,Qt::AlignHCenter);
    vlyt->addSpacing(12);
    vlyt->addWidget(bodyAppDesc,0,Qt::AlignHCenter);
    vlyt->addSpacing(12);
    vlyt->addWidget(bodySupport,0,Qt::AlignHCenter);
    vlyt->addStretch();
    return vlyt;
}
void menuModule::initGsetting(){
    m_isDarktheme=false;
    if(QGSettings::isSchemaInstalled(FITTHEMEWINDOW)){
        m_pGsettingThemeData = new QGSettings(FITTHEMEWINDOW);
        QString themeNow = m_pGsettingThemeData->get("styleName").toString();
        if("ukui-dark" == themeNow || "ukui-black" == themeNow){
            m_isDarktheme=true;
        }else{
            m_isDarktheme=false;
        }

        connect(m_pGsettingThemeData,&QGSettings::changed,this,&menuModule::dealSystemGsettingChange);
    }
}
void menuModule::fontSizeInit(){
    connect(this,&menuModule::setFontSize,this,&menuModule::dealSetFontSize);
    QGSettings* m_fontsize = nullptr;
    if(QGSettings::isSchemaInstalled(UKUI_STYLE)){
        m_fontsize = new QGSettings(UKUI_STYLE);
        connect(m_fontsize,&QGSettings::changed,this,[=](const QString &key){
            if(key == UKUI_FONT_SIZE){
                int fontsize = m_fontsize->get(UKUI_FONT_SIZE).toInt();
                if(fontsize > 0){
                    emit setFontSize(fontsize);
                }
            }
        });
    }
    if(m_fontsize){
        dealSetFontSize(m_fontsize->get(UKUI_FONT_SIZE).toInt());
    }
}
void menuModule::dealSetFontSize(int size){
    QFont font;
    font.setPointSize(size);
    this->setFont(font);
    bodyAppDesc->setFont(font);
    bodyAppName->setFont(font);
    bodySupport->setFont(font);
    titleText->setFont(font);
    bodyAppVersion->setFont(font);
    actionQuit->setFont(font);
    actionTheme->setFont(font);
    actionHelp->setFont(font);
    actionAbout->setFont(font);
    actionsetting->setFont(font);
    textedit->setFont(font);
    toollabel->setFont(font);
    submitbtn->setFont(font);
    //officewebsitehelp->setFont(font);
   // successbackhelp->setFont(font);
   // checkupdatehelp->setFont(font);
    //autoTheme->setFont(font);
  //  darkTheme->setFont(font);
  //  lightTheme->setFont(font);
}
void menuModule::keyPressEvent(QKeyEvent *event){
    if(event->key() == Qt::Key_F1){
        emit pullupHelp();
    }else{
        QWidget::keyPressEvent(event);
    }
}
void menuModule::dealSystemGsettingChange(const QString key){
    if(key == "styleName"){
        refreshThemeBySystemConf();
    }
    if(key==STYLE_ICON_NAME || key==STYLE_ICON){
        emit iconchange();
        bodyIcon->setPixmap(QIcon::fromTheme("kylin-usb-creator").pixmap(96,96));
        titleIcon->setPixmap(QIcon::fromTheme("kylin-usb-creator").pixmap(24,24));
    }
}

void menuModule::refreshThemeBySystemConf(){
    QString themeNow = m_pGsettingThemeData->get("styleName").toString();
    if("ukui-dark" == themeNow || "ukui-black" == themeNow){
        m_isDarktheme=true;
        setThemeDark();
    }else{
        m_isDarktheme=false;
        setThemeLight();
    }
}

void menuModule::setThemeDark(){
    m_isDarktheme=true;
    aboutWindow->setStyleSheet(".QWidget{background-color:rgba(61,61,65,1);}");
 //   sucbackWindow->setStyleSheet(".QWidget{background-color:rgba(61,61,65,1);}");
  //  textedit->setStyleSheet("background-color:rgba(61,61,65,1);color:rgba(255,255,255,1);");
 //   this->setStyleSheet("QLabel{color:rgba(255,255,255,1);}");
 //   titleText->setStyleSheet("color:rgba(255,255,255,1);");
    emit menuModuleSetThemeStyle("dark-theme");
    bodySupport->setText(tr("Service & Support: ") +
                         "<a href=\"mailto://support@kylinos.cn\""
                         "style=\"color:white\">"
                         "support@kylinos.cn</a>"
                         );
//    bodyIcon->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;}"
//                            "QPushButton::hover{border:0px;border-radius:4px;background:transparent;}"
//                            "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;}"
//                            );
//    titleIcon->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;}"
//                            "QPushButton::hover{border:0px;border-radius:4px;background:transparent;}"
//                            "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;}"
//                            );
//    submitbtn->setStyleSheet("QPushButton{border:0px;border-radius:4px;background-color:rgba(0,0,0,1);color:rgba(255,255,255,1)}"
//                             "QPushButton::hover{border:0px;border-radius:4px;background-color:rgba(41,135,236,1);color:rgba(255,255,255,1)}"
//                             "QPushButton::pressed{border:0px;border-radius:4px;background-color:rgba(0,0,0,1);color:rgba(255,255,255,1)}");
}
void menuModule::setThemeLight(){
    m_isDarktheme=false;
  //  aboutWindow->setStyleSheet(".QWidget{background-color:rgba(255,255,255,1);}");
 //   sucbackWindow->setStyleSheet(".QWidget{background-color:rgba(255,255,255,1);}");
 //   titleText->setStyleSheet("color:rgba(0,0,0,1);");
 //   textedit->setStyleSheet("background-color:rgba(255,255,255,1);color:rgba(61,61,65,1);");
    emit menuModuleSetThemeStyle("light-theme");
    bodySupport->setText(tr("Service & Support: ") +
                         "<a href=\"mailto://support@kylinos.cn\""
                         "style=\"color:black\">"
                         "support@kylinos.cn</a>");
//    bodyIcon->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;}"
//                            "QPushButton::hover{border:0px;border-radius:4px;background:transparent;}"
//                            "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;}"
//                            );
//    titleIcon->setStyleSheet("QPushButton{border:0px;border-radius:4px;background:transparent;}"
//                            "QPushButton::hover{border:0px;border-radius:4px;background:transparent;}"
//                            "QPushButton::pressed{border:0px;border-radius:4px;background:transparent;}"
//                            );
//    submitbtn->setStyleSheet("QPushButton{border:0px;border-radius:4px;background-color:rgba(128,128,128,1);color:rgba(0,0,0,1)}"
//                             "QPushButton::hover{border:0px;border-radius:4px;background-color:rgba(41,135,236,1);color:rgba(0,0,0,1)}"
//                             "QPushButton::pressed{border:0px;border-radius:4px;background-color:rgba(128,128,128,1);color:rgba(0,0,0,1)}");
}
