/*
 * Copyright (C) 2023, 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 3 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, see <https://www.gnu.org/licenses/>.
 */
#include "menumodule.h"
#include <usermanual.h>
#include <QWidget>
#include "controller/core/viewcontrol.h"
#include "windowmanage.hpp"
menuModule::menuModule(QWidget *parent) : QWidget(parent)
{
    init();
    //    m_locationCore = new Core();
    //    m_coreInteraction = new CoreInteraction();
}

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

void menuModule::initAction()
{
    m_bodyAppName = new QLabel();
    m_ft.setPixelSize(14); //先不用，之后要用
    m_bodyAppVersion = new QLabel();
    m_bodySupport = new QLabel();
    m_bodyDescription = new QLabel();
    m_bodyDescription->setFixedSize(QSize(380, 90));
    m_titleText = new QLabel();

    m_bodySupport->setText(tr("Service & Support Team: ")
                           + "<a href=\"mailto://support@kylinos.cn\""
                             "style=\"color:rgba(0,0,0,1)\">"
                             "support@kylinos.cn</a>");
    m_bodyAppVersion->setText(tr("Version: ") + "3.1.2");

    menuButton = new QToolButton(this);
    //    menuButton = new QPushButton(this);
    menuButton->setToolTip(tr("Options"));
    menuButton->setIcon(QIcon::fromTheme("open-menu-symbolic"));
    menuButton->setProperty("isWindowButton", 0x1);
    menuButton->setProperty("useIconHighlightEffect", 0x2);
    menuButton->setProperty("isOptionButton", true);
    menuButton->setIconSize(QSize(16, 16));
    menuButton->setPopupMode(QToolButton::InstantPopup);
    menuButton->setFixedSize(QSize(32, 32));
    menuButton->setAutoRaise(true);
    menuButton->setFocusPolicy(Qt::NoFocus);
    menuButton->setProperty("setIconHighlightEffectDefaultColor", QColor(Qt::white));
    m_menu = new QMenu();

    //    QList<QAction *> actions ;
    QAction *actionHelp = new QAction();
    actionHelp->setText(tr("Help"));
    m_menu->addAction(actionHelp);

    // Location
    QMenu *actionLocation = new QMenu();
    actionLocation->setTitle(tr("Location"));
    m_locationYes = new QAction();
    m_locationYes->setText(tr("yes"));
    //    m_locationYes->setIcon(QIcon::fromTheme("emblem-insync-synced"));
    m_locationNo = new QAction();
    m_locationNo->setText(tr("no"));

    if ("1" == CoreVar::getSettings("allow-locate").toString()) {
        //        m_locationYes->setIcon(QIcon::fromTheme("emblem-insync-synced"));
        m_locationYes->setIcon(QIcon());
    } else {
        m_locationNo->setIcon(QIcon());
    }

    actionLocation->addAction(m_locationYes);
    actionLocation->addAction(m_locationNo);

    m_menu->addMenu(actionLocation);

    QAction *actionAbout = new QAction();
    actionAbout->setText(tr("About"));
    m_menu->addAction(actionAbout);

    QAction *actionQuit = new QAction();
    actionQuit->setText(tr("Quit"));
    m_menu->addAction(actionQuit);

    //    actions<<actionHelp<<actionLocation<<actionAbout<<actionQuit;//暂时禁掉主题切换按钮
    //    actions<<actionHelp<<actionAbout<<actionQuit;//暂时禁掉主题切换按钮
    //    m_menu->addActions(actions);

    menuButton->setMenu(m_menu);
    initConnect();

    //    connect(this,&menuModule::menuModuleClose,Interaction::getInstance(),&Interaction::close);
}

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();
    } else if (tr("yes") == str) {
        m_locationYes->setIcon(QIcon(":/res/contorl_icons/selected_normal.png"));
        m_locationNo->setIcon(QIcon());
        emit allowLocationYes();
        //        m_locationNo->setIcon(QIcon());
    } else if (tr("no") == str) {
        m_locationNo->setIcon(QIcon(":/res/contorl_icons/selected_normal.png"));
        m_locationYes->setIcon(QIcon());
        emit allowLocationNo();
        //        m_locationYes->setIcon(QIcon());
    }
}

void menuModule::initConnect()
{
    connect(m_menu, &QMenu::triggered, this, &menuModule::triggerMenu);
    connect(this, &menuModule::allowLocationYes, Interaction::getInstance(), &Interaction::allowLocationYes); // 允许定位信号
    connect(this, &menuModule::allowLocationNo, Interaction::getInstance(), &Interaction::allowLocationNo); // 不允许定位信号
    connect(this, &menuModule::menuModuleClose, Interaction::getInstance(), &Interaction::closePrograme); //关闭程序
    //是否允许定位选项及关于服务与支持链接样式跟随主题
    connect(ViewVar::getInstance(), &ViewVar::monitorThemeChanged, this, &menuModule::monitorThemeChanged);
}

void menuModule::monitorThemeChanged(QString themeChange)
{
    if ("1" == CoreVar::getSettings("allow-locate").toString()) {
        if ("ukui-dark" == themeChange || "ukui-black" == themeChange) {
            m_locationYes->setIcon(QIcon(QIcon(":/res/contorl_icons/selected_dark.png")));
        } else {
            m_locationYes->setIcon(QIcon(QIcon(":/res/contorl_icons/selected_normal.png")));
        }
    } else {
        if ("ukui-dark" == themeChange || "ukui-black" == themeChange) {
            m_locationNo->setIcon(QIcon(QIcon(":/res/contorl_icons/selected_dark.png")));
        } else {
            m_locationNo->setIcon(QIcon(QIcon(":/res/contorl_icons/selected_normal.png")));
        }
    }
    if ("ukui-dark" == themeChange || "ukui-black" == themeChange) {
        m_bodySupport->setText(tr("Service & Support Team: ")
                               + "<a href=\"mailto://support@kylinos.cn\""
                                 "style=\"color:rgba(255,255,255,1)\">"
                                 "support@kylinos.cn</a>");

        //        m_bodyDescription->setText(tr("Weather is a city weather information viewing tool, you can browse the
        //        future weather conditions and living index, simple and convenient operation, accurately understand the
        //        weather changes.") +
        //                                   "<a href=\"mailto://support@kylinos.cn\""
        //                                   "style=\"color:rgba(255,255,255,1)\">"
        //                                   "support@kylinos.cn</a>");
        m_bodyDescription->setText(
            tr("Weather is a weather information tool, it can browse the weather conditions and living index."));
        m_bodyDescription->setWordWrap(true);

    } else {
        m_bodySupport->setText(tr("Service & Support Team: ")
                               + "<a href=\"mailto://support@kylinos.cn\""
                                 "style=\"color:rgba(0,0,0,1)\">"
                                 "support@kylinos.cn</a>");
        //        m_bodyDescription->setText(tr("Weather is a city weather information viewing tool, you can browse the
        //        future weather conditions and living index, simple and convenient operation, accurately understand the
        //        weather changes.") +
        //                               "<a href=\"mailto://support@kylinos.cn\""
        //                               "style=\"color:rgba(0,0,0,1)\">"
        //                               "support@kylinos.cn</a>");
        m_bodyDescription->setText(
            tr("Weather is a weather information tool, it can browse the weather conditions and living index."));
        m_bodyDescription->setWordWrap(true);
    }
}

void menuModule::setMenuModuleAllowLocationYes()
{
    m_locationYes->setIcon(QIcon(QIcon(":/res/contorl_icons/selected_normal.png")));
    m_locationNo->setIcon(QIcon());
    emit allowLocationYes();
}

void menuModule::helpAction()
{
    if(viewControl::getInstance()->activeWindow()->windowState() == Qt::WindowFullScreen)
        viewControl::getInstance()->activeWindow()->showMaximized();
    kdk::kabase::UserManualManagement userManualTest;
    if (!userManualTest.callUserManual("kylin-weather")) {
        qCritical() << "user manual call fail!";
    }

    return;
}

void menuModule::aboutAction()
{
    //    关于点击事件处理
    initAbout();
}

void menuModule::initAbout()
{
    kdk::KAboutDialog aboutWindow(this, QIcon::fromTheme("kylin-weather"), tr("Weather"), tr("Version: ") + APP_VERSION);
    aboutWindow.setBodyText(tr("Weather is a weather information tool, it can browse the weather conditions and living index."));
    aboutWindow.setBodyTextVisiable(false);
    ::kabase::WindowManage::removeHeader(&aboutWindow);
    aboutWindow.exec();
}

QHBoxLayout *menuModule::initTitleBar()
{
    QPushButton *titleIcon = new QPushButton();
    titleIcon->setFixedSize(QSize(24, 24));
    titleIcon->setIcon(QIcon::fromTheme("kylin-weather", QIcon(":/res/contorl_icons/icon.png")));
    //    titleIcon->setIcon(QIcon::fromTheme("kylin-weather-viewer"));
    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;}");
    titleIcon->setIconSize(QSize(24, 24));
    m_appShowingName = tr("Weather");
    m_iconPath = ":/res/contorl_icons/icon.png";
    //    titleIcon->setPixmap(QPixmap::fromImage(QImage(m_iconPath)));
    //    titleIcon->setScaledContents(true);

    QPushButton *titleBtnClose = new QPushButton;
    titleBtnClose->setIcon(QIcon::fromTheme("window-close-symbolic"));
    titleBtnClose->setToolTip(tr("close"));
    titleBtnClose->setFixedSize(QSize(30, 30));
    titleBtnClose->setFocusPolicy(Qt::NoFocus); //设置焦点类型
    titleBtnClose->setProperty("isWindowButton", 0x2);
    titleBtnClose->setProperty("useIconHighlightEffect", 0x8);
    titleBtnClose->setFlat(true);
    connect(titleBtnClose, &QPushButton::clicked, this, [=]() {
        m_aboutWindow->close();
    });

    QHBoxLayout *hlyt = new QHBoxLayout;
    m_titleText->setText(tr("Weather"));
    hlyt->setSpacing(0);
    hlyt->setMargin(4);
    hlyt->addSpacing(4);
    hlyt->addWidget(titleIcon, 0, Qt::AlignBottom); //居下显示
    hlyt->addSpacing(8);
    hlyt->addWidget(m_titleText, 0, Qt::AlignBottom);
    hlyt->addStretch();
    hlyt->addWidget(titleBtnClose, 0, Qt::AlignBottom);
    return hlyt;
}

QVBoxLayout *menuModule::initBody()
{
    m_appVersion = "3.1.2";
    QPushButton *bodyIcon = new QPushButton();
    bodyIcon->setFixedSize(QSize(80, 80));
    //    bodyIcon->setIcon(QIcon::fromTheme("kylin-weather", QIcon(":/res/contorl_icons/icon.png")));
    bodyIcon->setIcon(QIcon::fromTheme("kylin-weather", QIcon(":/res/contorl_icons/icon.png")));
    //    bodyIcon->setIcon(QIcon::fromTheme("kylin-weather-viewer"));

    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;}");
    bodyIcon->setIconSize(QSize(80, 80));
    m_bodyAppName->setFixedHeight(28);
    m_bodyAppName->setText(tr(m_appShowingName.toLocal8Bit()));
    m_bodyAppVersion->setFixedHeight(24);
    m_bodyAppVersion->setText(tr("Version: ") + m_appVersion);
    m_bodyAppVersion->setAlignment(Qt::AlignLeft);
    connect(m_bodySupport, &QLabel::linkActivated, this, [=](const QString url) {
        QDesktopServices::openUrl(QUrl(url));
    });
    m_bodySupport->setFixedHeight(28);
    m_bodySupport->setOpenExternalLinks(true);
    m_bodySupport->setContextMenuPolicy(Qt::NoContextMenu);

    QVBoxLayout *vlyt = new QVBoxLayout;
    vlyt->setMargin(0);
    vlyt->setSpacing(0);
    vlyt->addSpacing(20);
    vlyt->addWidget(bodyIcon, 0, Qt::AlignHCenter);
    //    vlyt->addSpacing(16);
    vlyt->addSpacing(12);
    vlyt->addWidget(m_bodyAppName, 0, Qt::AlignHCenter);
    vlyt->addSpacing(12);
    vlyt->addWidget(m_bodyAppVersion, 0, Qt::AlignHCenter);
    vlyt->addSpacing(8);
    //    vlyt->addWidget(m_bodyDescription,0,Qt::AlignHCenter);
    vlyt->setContentsMargins(20, 0, 0, 0);
    vlyt->addWidget(m_bodyDescription, 0, Qt::AlignLeft);
    vlyt->addSpacing(8);
    vlyt->addWidget(m_bodySupport, 1, Qt::AlignLeft);
    vlyt->addStretch();
    return vlyt;
}
