// SPDX-FileCopyrightText: 2020 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "cpicturetool.h"
#include "frame/cgraphicsview.h"
#include "frame/cviewmanagement.h"

#include <QDebug>
#include <QtConcurrent>
#include <QGraphicsItem>
#include <QImageReader>
#include <QScreen>
#include <QGuiApplication>

#include "application.h"

#include <DMessageBox>
#include <DDialog>
#include <DPushButton>
#include <DToolButton>

#include "frame/cundoredocommand.h"
#include "mainwindow.h"
#include "ccentralwidget.h"
#include "cattributeitemwidget.h"
#include "cattributemanagerwgt.h"
#include "cgraphicslayer.h"
#include "filehander.h"

const int BUTTON_NORMAL = 38;
const int BUTTON_COMPACT = 26;

class CPictureTool::CPictureTool_private
{
public:
    explicit CPictureTool_private(CPictureTool *father): _father(father) {}

    CPictureTool *_father;
    ProgressLayout *progressLayout = nullptr;
};

CPictureTool::CPictureTool()
    : IDrawTool(picture)
{
    qDebug() << "Creating picture tool";
    _pPrivate = new CPictureTool_private(this);
}

QAbstractButton *CPictureTool::initToolButton()
{
    DToolButton *m_picBtn = new DToolButton;
    m_picBtn->setShortcut(QKeySequence(QKeySequence(Qt::Key_I)));
    setWgtAccesibleName(m_picBtn, "Import tool button");
    m_picBtn->setToolTip(tr("Import (I)"));
    m_picBtn->setIconSize(QSize(48, 48));
    m_picBtn->setFixedSize(QSize(37, 37));
    m_picBtn->setCheckable(true);

    connect(m_picBtn, &DToolButton::toggled, m_picBtn, [ = ](bool b) {
        qDebug() << "Picture tool button toggled:" << b;
        QIcon icon       = QIcon::fromTheme("ddc_picture tools_normal");
        QIcon activeIcon = QIcon::fromTheme("ddc_picture tools_disable");
        m_picBtn->setIcon(b ? activeIcon : icon);
    });
    m_picBtn->setIcon(QIcon::fromTheme("ddc_picture tools_normal"));
    return m_picBtn;
}

SAttrisList CPictureTool::attributions()
{
    qDebug() << "Getting picture tool attributions";
    DrawAttribution::SAttrisList result;
    return result;
}

void CPictureTool::registerAttributionWidgets()
{
    auto m_leftRotateBtn = new QPushButton;
    m_leftRotateBtn->setObjectName("PicLeftRotateBtn");
    m_leftRotateBtn->setMaximumSize(QSize(38, 38));
    m_leftRotateBtn->setIcon(QIcon::fromTheme("ddc_contrarotate_normal"));
    m_leftRotateBtn->setIconSize(QSize(48, 48));
    m_leftRotateBtn->setToolTip(tr("Rotate 90° CCW"));
    m_leftRotateBtn->setFocusPolicy(Qt::NoFocus);
    connect(m_leftRotateBtn, &QPushButton::clicked, m_leftRotateBtn, [ = ]() {
        qDebug() << "Left rotate button clicked";
        drawBoard()->setDrawAttribution(EImageLeftRot, true);
    });
    connect(drawBoard()->attributionWidget(), &CAttributeManagerWgt::updateWgt, m_leftRotateBtn, [ = ](QWidget * pWgt, const QVariant & var) {
        if (pWgt == m_leftRotateBtn) {
            m_leftRotateBtn->setEnabled(var.toBool());
        }
    });
    drawBoard()->attributionWidget()->installComAttributeWgt(EImageLeftRot, m_leftRotateBtn, false);

    auto m_rightRotateBtn = new QPushButton;
    m_rightRotateBtn->setObjectName("PicRightRotateBtn");
    m_rightRotateBtn->setIcon(QIcon::fromTheme("ddc_clockwise rotation_normal"));
    m_rightRotateBtn->setIconSize(QSize(48, 48));
    m_rightRotateBtn->setToolTip(tr("Rotate 90° CW"));
    m_rightRotateBtn->setFocusPolicy(Qt::NoFocus);
    connect(m_rightRotateBtn, &QPushButton::clicked, m_rightRotateBtn, [ = ]() {
        qDebug() << "Right rotate button clicked";
        drawBoard()->setDrawAttribution(EImageRightRot, true);
    });
    connect(drawBoard()->attributionWidget(), &CAttributeManagerWgt::updateWgt, m_rightRotateBtn, [ = ](QWidget * pWgt, const QVariant & var) {
        if (pWgt == m_rightRotateBtn) {
            m_rightRotateBtn->setEnabled(var.toBool());
        }
    });
    drawBoard()->attributionWidget()->installComAttributeWgt(EImageRightRot, m_rightRotateBtn, false);

    auto m_flipHBtn = new QPushButton;
    m_flipHBtn->setObjectName("PicFlipHBtn");
    m_flipHBtn->setIcon(QIcon::fromTheme("ddc_flip horizontal_normal"));
    m_flipHBtn->setIconSize(QSize(48, 48));
    m_flipHBtn->setToolTip(tr("Flip horizontally"));
    m_flipHBtn->setFocusPolicy(Qt::NoFocus);

    connect(m_flipHBtn, &QPushButton::clicked, m_flipHBtn, [ = ]() {
        qDebug() << "Horizontal flip button clicked";
        drawBoard()->setDrawAttribution(EImageHorFilp, true);
    });

    connect(drawBoard()->attributionWidget(), &CAttributeManagerWgt::updateWgt, m_flipHBtn, [ = ](QWidget * pWgt, const QVariant & var) {
        if (pWgt == m_flipHBtn) {
            m_flipHBtn->setEnabled(var.toBool());
        }
    });

    drawBoard()->attributionWidget()->installComAttributeWgt(EImageHorFilp, m_flipHBtn, false);

    auto m_flipVBtn = new QPushButton;
    m_flipVBtn->setObjectName("PicFlipVBtn");
    m_flipVBtn->setIcon(QIcon::fromTheme("ddc_flip vertical_normal"));
    m_flipVBtn->setIconSize(QSize(48, 48));
    m_flipVBtn->setToolTip(tr("Flip vertically"));
    m_flipVBtn->setFocusPolicy(Qt::NoFocus);
    connect(m_flipVBtn, &QPushButton::clicked, m_flipVBtn, [ = ]() {
        qDebug() << "Vertical flip button clicked";
        drawBoard()->setDrawAttribution(EImageVerFilp, true);
    });

    connect(drawBoard()->attributionWidget(), &CAttributeManagerWgt::updateWgt, m_flipVBtn, [ = ](QWidget * pWgt, const QVariant & var) {
        if (pWgt == m_flipVBtn) {
            m_flipVBtn->setEnabled(var.toBool());
        }
    });

    drawBoard()->attributionWidget()->installComAttributeWgt(EImageVerFilp, m_flipVBtn, false);

    auto m_flipAdjustment = new QPushButton;
    m_flipAdjustment->setObjectName("PicFlipAdjustmentBtn");
    m_flipAdjustment->setIcon(QIcon::fromTheme("ddc_flip_adjustment_normal"));
    m_flipAdjustment->setIconSize(QSize(48, 48));
    m_flipAdjustment->setToolTip(tr("Auto fit"));
    m_flipAdjustment->setFocusPolicy(Qt::NoFocus);

    connect(m_flipAdjustment, &QPushButton::clicked, m_flipAdjustment, [ = ]() {
        qDebug() << "Auto fit button clicked";
        auto page = this->drawBoard()->currentPage();
        if (page == nullptr) {
            qDebug() << "Auto fit failed - no current page";
            return;
        }

        auto curScene = page->scene();
        if (curScene != nullptr) {
            auto rect = curScene->selectGroup()->sceneBoundingRect();
            if (rect != curScene->sceneRect()) {
                qDebug() << "Adjusting scene rect to selection bounds:" << rect;
                CCmdBlock block(curScene, CSceneUndoRedoCommand::ESizeChanged);
                curScene->setSceneRect(rect);
                curScene->updateAttribution();
                curScene->update();
            } else {
                qDebug() << "Scene rect already matches selection bounds";
            }
        } else {
            qDebug() << "Auto fit failed - no current scene";
        }
    });

    connect(drawBoard()->attributionWidget(), &CAttributeManagerWgt::updateWgt, m_flipAdjustment, [ = ](QWidget * pWgt, const QVariant & var) {
        if (pWgt == m_flipAdjustment) {
            m_flipAdjustment->setEnabled(var.toBool());
        }
    });

    int btnSize = BUTTON_NORMAL;
#ifdef DTKWIDGET_CLASS_DSizeMode
    if (DGuiApplicationHelper::instance()->sizeMode() == DGuiApplicationHelper::CompactMode) {
        qDebug() << "Setting compact button size:" << BUTTON_COMPACT;
        btnSize = BUTTON_COMPACT;
    }

    connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::sizeModeChanged, this, [=](DGuiApplicationHelper::SizeMode sizeMode) {
        int nBtnSize = 0;
        if (sizeMode == DGuiApplicationHelper::NormalMode)
            nBtnSize = BUTTON_NORMAL;
        else
            nBtnSize = BUTTON_COMPACT;
        m_leftRotateBtn->setMaximumSize(QSize(nBtnSize, nBtnSize));
        m_rightRotateBtn->setMaximumSize(QSize(nBtnSize, nBtnSize));
        m_flipHBtn->setMaximumSize(QSize(nBtnSize, nBtnSize));
        m_flipVBtn->setMaximumSize(QSize(nBtnSize, nBtnSize));
        m_flipAdjustment->setMaximumSize(QSize(nBtnSize, nBtnSize));
    });
#endif
    m_leftRotateBtn->setMaximumSize(QSize(btnSize, btnSize));
    m_rightRotateBtn->setMaximumSize(QSize(btnSize, btnSize));
    m_flipHBtn->setMaximumSize(QSize(btnSize, btnSize));
    m_flipVBtn->setMaximumSize(QSize(btnSize, btnSize));
    m_flipAdjustment->setMaximumSize(QSize(btnSize, btnSize));

    drawBoard()->attributionWidget()->installComAttributeWgt(EImageAdaptScene, m_flipAdjustment, true);
}

CPictureTool::~CPictureTool()
{
    qDebug() << "Destroying picture tool";
    delete _pPrivate;
}

void CPictureTool::onStatusChanged(EStatus oldStatus, EStatus nowStatus)
{
    qDebug() << "Picture tool status changing from" << oldStatus << "to" << nowStatus;
    if (nowStatus == EReady) {
        IDrawTool::onStatusChanged(oldStatus, nowStatus);
        DFileDialog fileDialog(drawBoard());
        //设置文件保存对话框的标题
        if (Application::isTabletSystemEnvir()) {
            qDebug() << "Setting dialog directory to Downloads (tablet environment)";
            fileDialog.setDirectory(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation));
        } else {
            qDebug() << "Setting dialog directory to Home";
            fileDialog.setDirectory(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
        }
        fileDialog.setWindowTitle(tr("Import Picture"));

        QStringList filters;
        auto formatsList = drawApp->readableFormats();
        if (!formatsList.isEmpty())
            formatsList.removeFirst();
        auto formats = QString("(*.") + formatsList.join(" *.") + QString(")");
        filters << formats;
        qDebug() << "Setting file filters:" << filters;

        fileDialog.setNameFilters(filters);
        fileDialog.setFileMode(QFileDialog::ExistingFiles);
        fileDialog.setDirectory(drawApp->defaultFileDialogPath());

        if (fileDialog.exec() == QDialog::Accepted) {
            QStringList filenames = fileDialog.selectedFiles();
            qDebug() << "Selected files:" << filenames;

            int ret = drawApp->execPicturesLimit(filenames.size());
            if (ret == 0) {
                qDebug() << "Loading" << filenames.size() << "files";
                if (drawBoard() != nullptr) {
                    drawBoard()->loadFiles(filenames);
                }
            }
        } else {
            qDebug() << "File dialog cancelled";
        }
        drawBoard()->setCurrentTool(selection);
    }
}



