// Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "subtoolwidget.h"
#include "accessibility/acTextDefine.h"
#include "settings.h"
#include "../../utils/log.h"

#include <QActionGroup>
#include <QFileInfo>
#include <QHelpEvent>
#include <QToolTip>
#include <DFontSizeManager>

#define THEMETYPE 1 // 主题颜色为浅色

SubToolWidget::SubToolWidget(DWidget *parent): DStackedWidget(parent)
{
    qCDebug(dsrApp) << "SubToolWidget constructor called.";
    initShotLable();
    qCDebug(dsrApp) << "initShotLable finished.";
    setCurrentWidget(m_shotSubTool);
    qCDebug(dsrApp) << "Current widget set to m_shotSubTool.";
}

// 初始化贴图工具栏按钮
void SubToolWidget::initShotLable()
{
    qCDebug(dsrApp) << "Initializing pin screenshot toolbar buttons.";
    m_shotSubTool = new DLabel(this);
    qCDebug(dsrApp) << "m_shotSubTool DLabel created.";
    // ocr按钮
    m_ocrButton = new ToolButton(this);
//    m_ocrButton->setCheckable(false);
//    m_ocrButton->setFlat(true);
//    m_ocrButton->setFocusPolicy(Qt::NoFocus);
    m_ocrButton->setObjectName(AC_SUBTOOLWIDGET_PIN_OCR_BUT);
    m_ocrButton->setAccessibleName(AC_SUBTOOLWIDGET_PIN_OCR_BUT);
    m_ocrButton->setIconSize(QSize(32, 32));
    m_ocrButton->setFixedSize(32, 32);
    m_ocrButton->setIcon(QIcon::fromTheme("ocr-normal"));
    m_ocrButton->setToolTip(tr("Extract Text"));
    connect(m_ocrButton, SIGNAL(clicked()), this, SIGNAL(signalOcrButtonClicked()));
    qCDebug(dsrApp) << "OCR button initialized";

    // 选项按钮
    m_pinOptionButton = new ToolButton(this);
    qCDebug(dsrApp) << "Options button created.";
    m_pinOptionButton->setCheckable(false);
    m_pinOptionButton->setFlat(false);
    m_pinOptionButton->setHoverState(false);
    DPalette pa = m_pinOptionButton->palette();
    DGuiApplicationHelper::ColorType t_type = DGuiApplicationHelper::instance()->themeType();
    if (t_type == DGuiApplicationHelper::ColorType::LightType) {
        qCDebug(dsrApp) << "Theme type is LightType, setting palette colors.";
        pa.setColor(DPalette::ButtonText, QColor(28, 28, 28, 255));
        pa.setColor(DPalette::Dark, QColor(192, 192, 192, 255));
        pa.setColor(DPalette::Light, QColor(192, 192, 192, 255));
    } else {
        qCDebug(dsrApp) << "Theme type is DarkType, setting palette colors.";
        pa.setColor(DPalette::ButtonText, QColor(228, 228, 228, 255));
        pa.setColor(DPalette::Dark, QColor(64, 64, 64, 255));
        pa.setColor(DPalette::Light, QColor(64, 64, 64, 255));
    }
    m_pinOptionButton->setPalette(pa);
    m_pinOptionButton->setFocusPolicy(Qt::NoFocus);
    m_pinOptionButton->setObjectName(AC_SUBTOOLWIDGET_PIN_OPTION_BUT);
    m_pinOptionButton->setAccessibleName(AC_SUBTOOLWIDGET_PIN_OPTION_BUT);
    DFontSizeManager::instance()->bind(m_pinOptionButton, DFontSizeManager::T6);
    m_pinOptionButton->setText(tr("Options"));
    m_pinOptionButton->setMinimumSize(QSize(60, 36));
    m_pinOptionButton->setToolTip(tr("Options"));
    qCDebug(dsrApp) << "Options button initialized";

    m_saveGroup = new QActionGroup(this);
    qCDebug(dsrApp) << "Save action group created.";
    QActionGroup *t_formatGroup = new QActionGroup(this);
    qCDebug(dsrApp) << "Format action group created.";
    m_saveGroup->setExclusive(true);
    t_formatGroup->setExclusive(true);
    qCDebug(dsrApp) << "Action groups set to exclusive.";

    // 选项菜单
    m_optionMenu = new DMenu(this);
    qCDebug(dsrApp) << "Options menu created.";
    DFontSizeManager::instance()->bind(m_optionMenu, DFontSizeManager::T6);
    connect(m_optionMenu, &DMenu::aboutToShow, this, &SubToolWidget::updateOptionChecked);
    qCDebug(dsrApp) << "Options menu created";

    QAction *saveTitleAction = new QAction(m_optionMenu);
    QAction *saveToClipAction = new QAction(m_optionMenu);
    QAction *saveToDesktopAction = new QAction(m_optionMenu);
    QAction *saveToPictureAction = new QAction(m_optionMenu);
//    QAction *saveToSpecialPath = new QAction(m_optionMenu);

    m_saveToSpecialPathMenu = new DMenu(m_optionMenu);
    m_saveToSpecialPathMenu->setTitle(tr("Folder"));
    m_saveToSpecialPathMenu->setToolTipsVisible(true);
    m_saveToSpecialPathMenu->menuAction()->setCheckable(true);
    DFontSizeManager::instance()->bind(m_saveToSpecialPathMenu, DFontSizeManager::T8);
    QString specialPath = Settings::instance()->getSavePath();
    qCDebug(dsrApp) << "Special save path menu created";

    //设置或更新指定路径的菜单按键
    m_changeSaveToSpecialPath = new QAction(m_saveToSpecialPathMenu);
    m_changeSaveToSpecialPath->setCheckable(true);
    m_saveToSpecialPathMenu->addAction(m_changeSaveToSpecialPath);
    qCDebug(dsrApp) << "Change save to special path action created and added.";
    //历史保存路径
    m_saveToSpecialPathAction = new QAction(m_saveToSpecialPathMenu);
    if (specialPath.isEmpty() || !QFileInfo::exists(specialPath)) {
        qCDebug(dsrApp) << "Specified path does not exist or is empty.";
        m_changeSaveToSpecialPath->setText(tr("Set a path on save"));
    } else {
        qCDebug(dsrApp) << "Specified path exists.";
        initChangeSaveToSpecialAction(specialPath);
    }
    m_SavePathActions.insert(FOLDER_CHANGE, m_changeSaveToSpecialPath);
    qCDebug(dsrApp) << "Save to special path action initialized.";

    QAction *formatTitleAction = new QAction(m_optionMenu);
    QAction *pngAction = new QAction(m_optionMenu);
    QAction *jpgAction = new QAction(m_optionMenu);
    QAction *bmpAction = new QAction(m_optionMenu);
    qCDebug(dsrApp) << "Format actions created.";

    saveTitleAction->setDisabled(true);
    saveTitleAction->setText(tr("Save to"));
    qCDebug(dsrApp) << "Save title action disabled.";

    saveToDesktopAction->setText(tr("Desktop"));
    saveToDesktopAction->setCheckable(true);
    qCDebug(dsrApp) << "Save to desktop action configured.";

    saveToPictureAction->setText(tr("Pictures"));
    saveToPictureAction->setCheckable(true);
    qCDebug(dsrApp) << "Save to picture action configured.";

    //saveToSpecialPath->setText(tr("Folder"));
    //saveToSpecialPath->setCheckable(true);

    saveToClipAction->setText(tr("Clipboard"));
    saveToClipAction->setCheckable(true);
    qCDebug(dsrApp) << "Save to clipboard action configured.";

    m_saveGroup->addAction(saveToClipAction);
    m_saveGroup->addAction(saveToDesktopAction);
    m_saveGroup->addAction(saveToPictureAction);
    //m_saveGroup->addAction(saveToSpecialPath);
    m_saveGroup->addAction(m_changeSaveToSpecialPath);
    qCDebug(dsrApp) << "Save actions added to save group.";

    m_SavePathActions.insert(CLIPBOARD, saveToClipAction);
    m_SavePathActions.insert(DESKTOP, saveToDesktopAction);
    m_SavePathActions.insert(PICTURES, saveToPictureAction);
    qCDebug(dsrApp) << "Save path actions initialized";

    formatTitleAction->setDisabled(true);
    formatTitleAction->setText(tr("Format"));
    pngAction->setText(tr("PNG"));
    pngAction->setCheckable(true);
    jpgAction->setText(tr("JPG"));
    jpgAction->setCheckable(true);
    bmpAction->setText(tr("BMP"));
    bmpAction->setCheckable(true);

    t_formatGroup->addAction(pngAction);
    t_formatGroup->addAction(jpgAction);
    t_formatGroup->addAction(bmpAction);
    qCDebug(dsrApp) << "Format actions added to format group.";

    m_SaveFormatActions.insert(PNG, pngAction);
    m_SaveFormatActions.insert(JPG, jpgAction);
    m_SaveFormatActions.insert(BMP, bmpAction);
    qCDebug(dsrApp) << "Save format actions initialized";

    //保存方式
    m_optionMenu->addAction(saveTitleAction);
    m_optionMenu->addAction(saveToClipAction);
    m_optionMenu->addAction(saveToDesktopAction);
    m_optionMenu->addAction(saveToPictureAction);
    //m_optionMenu->addAction(saveToSpecialPath);
    m_optionMenu->addMenu(m_saveToSpecialPathMenu);
    m_optionMenu->addSeparator();
    // 格式
    m_optionMenu->addAction(formatTitleAction);
    m_optionMenu->addAction(pngAction);
    m_optionMenu->addAction(jpgAction);
    m_optionMenu->addAction(bmpAction);
    m_pinOptionButton->setMenu(m_optionMenu);
    qCDebug(dsrApp) << "Option menu structure completed";

    //updateOptionChecked();

    connect(m_saveGroup, QOverload<QAction *>::of(&QActionGroup::triggered), [ = ](QAction * t_act) {
        Settings::instance()->setIsChangeSavePath(false);
        m_saveToSpecialPathMenu->menuAction()->setChecked(false);
        if (t_act == saveToDesktopAction) {
            qCDebug(dsrApp) << "Selected: Save to Desktop.";
            m_SaveInfo.first = DESKTOP;
        } else if (t_act == saveToPictureAction) {
            qCDebug(dsrApp) << "Selected: Save to Pictures.";
            m_SaveInfo.first = PICTURES;
        } else if (t_act == saveToClipAction) {
            qCDebug(dsrApp) << "Selected: Copy to Clipboard.";
            m_SaveInfo.first = CLIPBOARD;
        } else if (t_act == m_changeSaveToSpecialPath) {
            qCDebug(dsrApp) << "Selected: Set or change specified save location.";
            m_SaveInfo.first = FOLDER_CHANGE;
            m_saveToSpecialPathMenu->menuAction()->setChecked(true);
            Settings::instance()->setIsChangeSavePath(true);
        } else {
            qCDebug(dsrApp) << "Selected: Save to specified location (FOLDER).";
            m_SaveInfo.first = FOLDER;
            m_saveToSpecialPathMenu->menuAction()->setChecked(true);
        }
        Settings::instance()->setSaveOption(m_SaveInfo);
        qCDebug(dsrApp) << "Save option set in settings.";
    });

    connect(t_formatGroup, QOverload<QAction *>::of(&QActionGroup::triggered),
    [ = ](QAction * t_act) {
        if (t_act == pngAction) {
            qCDebug(dsrApp) << "Selected format: PNG.";
            m_SaveInfo.second = PNG;
        } else if (t_act == jpgAction) {
            qCDebug(dsrApp) << "Selected format: JPG.";
            m_SaveInfo.second = JPG;
        } else if (t_act == bmpAction) {
            qCDebug(dsrApp) << "Selected format: BMP.";
            m_SaveInfo.second = BMP;
        }
        Settings::instance()->setSaveOption(m_SaveInfo);
    });

    //工具栏布局
    QHBoxLayout *hLayout = new QHBoxLayout();
    hLayout->setSizeConstraint(QLayout::SetFixedSize);
    hLayout->setContentsMargins(0, 0, 0, 0);
    hLayout->addWidget(m_ocrButton, 0,  Qt::AlignCenter);
    hLayout->addWidget(m_pinOptionButton, 0, Qt::AlignCenter);
    m_shotSubTool->setLayout(hLayout);
    addWidget(m_shotSubTool);
    updateOptionChecked();
    qInfo() << __LINE__ << __FUNCTION__ << "贴图工具栏初始化完成";
}

void SubToolWidget::initChangeSaveToSpecialAction(const QString specialPath)
{
    m_changeSaveToSpecialPath->setText(tr("Change the path on save"));
    //根据字体大小计算字符串宽度，确定路径省略的长度
    QFontMetrics tempFont(m_changeSaveToSpecialPath->font());
    auto changeSaveToSpecialPathFontWidth = tempFont.boundingRect(m_changeSaveToSpecialPath->text()).width();
    QFontMetrics tmpFont(m_saveToSpecialPathAction->font());
    QString sFileName = tmpFont.elidedText(specialPath, Qt::TextElideMode::ElideRight, changeSaveToSpecialPathFontWidth);
    m_saveToSpecialPathAction->setText(sFileName);
    m_saveToSpecialPathAction->setToolTip(specialPath);
    m_saveToSpecialPathAction->setCheckable(true);
    m_saveToSpecialPathMenu->insertAction(m_changeSaveToSpecialPath, m_saveToSpecialPathAction);
    m_saveGroup->addAction(m_saveToSpecialPathAction);
    m_SavePathActions.insert(FOLDER, m_saveToSpecialPathAction);
}

// 获取保存信息
QPair<int, int> SubToolWidget::getSaveInfo()
{
    return m_SaveInfo;
}

// 选项按钮被点击
void SubToolWidget::onOptionButtonClicked()
{
    if (m_optionMenu->isHidden())
        m_pinOptionButton->showMenu();
    else
        m_optionMenu->hide();
}

//根据配置文件更新当前选中的选项
void SubToolWidget::updateOptionChecked()
{
    qInfo() << __LINE__ << __FUNCTION__ << "更新菜单选项";
    QPair<int, int> saveInfo = Settings::instance()->getSaveOption();
    qDebug() << "saveInfo: " << saveInfo;
    if (saveInfo.second != PNG && saveInfo.second != JPG && saveInfo.second != BMP) { //不存在保存格式的情况
        saveInfo.second = PNG; // 默认保存格式
        qCDebug(dsrApp) << "Invalid save format, defaulting to PNG";
    }
    //没有配置文件时，给定一个默认值
    if (saveInfo.first == 0 && saveInfo.second == 0) {
        m_SaveInfo.first = CLIPBOARD; // 默认保存路径
        m_SaveInfo.second = PNG; // 默认保存格式
        qCDebug(dsrApp) << "No configuration found, using defaults: Clipboard and PNG";
    } else {
        m_SaveInfo = saveInfo;
    }
    qDebug() << "m_SaveInfo: " << m_SaveInfo;
    //当前选中的是指定目录时需要重判断
    if (m_SaveInfo.first == FOLDER || m_SaveInfo.first == FOLDER_CHANGE) {
        m_saveToSpecialPathMenu->menuAction()->setChecked(true);
        QString specialPath = Settings::instance()->getSavePath();
        //判断是否存在历史路径
        if (specialPath.isEmpty() || !QFileInfo::exists(specialPath)) {
            m_SaveInfo.first = FOLDER_CHANGE;
            qCDebug(dsrApp) << "Special path not found, switching to folder change mode";
        } else {
            m_SaveInfo.first = FOLDER;
            qCDebug(dsrApp) << "Using special path:" << specialPath;
            //配置文件中存在历史路径时(打开多个贴图，对其中某个贴图保存到指定路径)，但是当前的工具栏菜单中不存在对应的历史路径action
            if (!m_SavePathActions.contains(m_SaveInfo.first)) {
                qCDebug(dsrApp) << "Creating new special path action";
                initChangeSaveToSpecialAction(specialPath);
            } else {
                qCDebug(dsrApp) << "Updating existing special path action";
                //根据字体大小计算字符串宽度，确定路径省略的长度
                QFontMetrics tempFont(m_changeSaveToSpecialPath->font());
                auto changeSaveToSpecialPathFontWidth = tempFont.boundingRect(m_changeSaveToSpecialPath->text()).width();
                QFontMetrics tmpFont(m_saveToSpecialPathAction->font());
                QString sFileName = tmpFont.elidedText(specialPath, Qt::TextElideMode::ElideRight, changeSaveToSpecialPathFontWidth);
                m_saveToSpecialPathAction->setText(sFileName);
                m_saveToSpecialPathAction->setToolTip(specialPath);
            }
        }
        if (Settings::instance()->getIsChangeSavePath()) {
            m_SaveInfo.first = FOLDER_CHANGE;
            qCDebug(dsrApp) << "Change save path flag is set, switching to folder change mode";
        }
    } else {
        qCDebug(dsrApp) << "Save option is not FOLDER_CHANGE, unchecking special path menu action.";
        m_saveToSpecialPathMenu->menuAction()->setChecked(false);
    }

    m_SavePathActions.value(m_SaveInfo.first)->setChecked(true);
    m_SaveFormatActions.value(m_SaveInfo.second)->setChecked(true);
    qCDebug(dsrApp) << "Menu options update completed";
}

bool SubToolWidget::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == m_saveToSpecialPathMenu) {
        if(event->type() == QEvent::ToolTip){
            QHelpEvent* he = dynamic_cast<QHelpEvent*>(event);
            QAction *action = static_cast<QMenu *>(watched)->actionAt(he->pos());
            if (action != nullptr) {
                if (action == m_saveToSpecialPathAction) {
                    QToolTip::showText(he->globalPos(), action->toolTip(), this);
                }
                if (action == m_changeSaveToSpecialPath) {
                    QToolTip::hideText();
                }
            }
        }
    }
    return false;
}
