/* This file is part of the KDE project
 * SPDX-FileCopyrightText: 2007 C. Boemann <cbo@boemann.dk>
 * SPDX-FileCopyrightText: 2007 Thomas Zander <zander@kde.org>
 * SPDX-FileCopyrightText: 2007 Jan Hambrecht <jaham@gmx.net>
 * SPDX-FileCopyrightText: 2010 Boudewijn Rempt <boud@kogmbh.com>
 * SPDX-FileCopyrightText: 2011 Arjen Hiemstra <ahiemstra@heimr.nl>
 *
 * SPDX-License-Identifier: LGPL-2.0-or-later
 */
#include <KoZoomController.h>
#include <KoZoomController_p.h>

#include <KActionCollection>
#include <KLocalizedString>
#include <WidgetsDebug.h>

#include <KoCanvasBase.h>
#include <KoCanvasController.h>
#include <KoZoomHandler.h>

void KoZoomController::Private::init(KoCanvasController *co, KoZoomHandler *zh, KActionCollection *actionCollection, bool createZoomShortcuts)
{
    canvasController = co;
    fitMargin = co->margin();
    zoomHandler = zh;
    connect(action, &KoZoomAction::zoomChanged, parent, QOverload<KoZoomMode::Mode, qreal>::of(&KoZoomController::setZoom));
    connect(action, &KoZoomAction::aspectModeChanged, parent, &KoZoomController::aspectModeChanged);
    connect(action, &KoZoomAction::zoomedToSelection, parent, &KoZoomController::zoomedToSelection);
    connect(action, &KoZoomAction::zoomedToAll, parent, &KoZoomController::zoomedToAll);

    actionCollection->addAction(QStringLiteral("view_zoom"), action);

    if (createZoomShortcuts) {
        actionCollection->addAction(KStandardAction::ZoomIn, QStringLiteral("zoom_in"), action, SLOT(zoomIn()));
        actionCollection->addAction(KStandardAction::ZoomOut, QStringLiteral("zoom_out"), action, SLOT(zoomOut()));
    }

    connect(canvasController->proxyObject, &KoCanvasControllerProxyObject::sizeChanged, parent, [this](const QSize sz) {
        Q_UNUSED(sz);
        setAvailableSize();
    });

    connect(canvasController->proxyObject, &KoCanvasControllerProxyObject::zoomRelative, parent, [this](qreal r, const QPointF pt) {
        requestZoomRelative(r, pt);
    });
}

KoZoomController::KoZoomController(KoCanvasController *co,
                                   KoZoomHandler *zh,
                                   KActionCollection *actionCollection,
                                   KoZoomAction::SpecialButtons specialButtons,
                                   QObject *parent)
    : QObject(parent)
    , d(new Private(this, specialButtons))
{
    d->init(co, zh, actionCollection, true);
}

KoZoomController::KoZoomController(KoCanvasController *co,
                                   KoZoomHandler *zh,
                                   KActionCollection *actionCollection,
                                   bool createZoomShortcuts,
                                   KoZoomAction::SpecialButtons specialButtons,
                                   QObject *parent)
    : QObject(parent)
    , d(new Private(this, specialButtons))
{
    d->init(co, zh, actionCollection, createZoomShortcuts);
}

KoZoomController::~KoZoomController()
{
    delete d;
}

KoZoomAction *KoZoomController::zoomAction() const
{
    return d->action;
}

void KoZoomController::setZoomMode(KoZoomMode::Mode mode)
{
    setZoom(mode, 1.0);
}

void KoZoomController::setPageSize(const QSizeF &pageSize)
{
    if (d->pageSize == pageSize)
        return;
    d->pageSize = pageSize;

    if (d->zoomHandler->zoomMode() == KoZoomMode::ZOOM_WIDTH)
        setZoom(KoZoomMode::ZOOM_WIDTH, 0);
    if (d->zoomHandler->zoomMode() == KoZoomMode::ZOOM_PAGE)
        setZoom(KoZoomMode::ZOOM_PAGE, 0);
}

QSizeF KoZoomController::pageSize() const
{
    return d->pageSize;
}

void KoZoomController::setTextMinMax(qreal min, qreal max)
{
    if (d->textMinX == min && d->textMaxX == max) {
        return;
    }
    d->textMinX = min;
    d->textMaxX = max;

    if (d->zoomHandler->zoomMode() == KoZoomMode::ZOOM_TEXT)
        setZoom(KoZoomMode::ZOOM_TEXT, 0);
}

void KoZoomController::setDocumentSize(const QSizeF &documentSize, bool recalculateCenter)
{
    d->documentSize = documentSize;
    d->canvasController->updateDocumentSize(documentToViewport(d->documentSize), recalculateCenter);

    // Finally ask the canvasController to recenter
    d->canvasController->recenterPreferred();
}

QSizeF KoZoomController::documentSize() const
{
    return d->documentSize;
}

void KoZoomController::setZoom(KoZoomMode::Mode mode, qreal zoom)
{
    setZoom(mode, zoom, d->canvasController->preferredCenter());
}

void KoZoomController::setZoom(KoZoomMode::Mode mode, qreal zoom, const QPointF &stillPoint)
{
    setZoom(mode, zoom, d->zoomHandler->resolutionX(), d->zoomHandler->resolutionY(), stillPoint);
}

void KoZoomController::setZoom(KoZoomMode::Mode mode, qreal zoom, qreal resolutionX, qreal resolutionY)
{
    setZoom(mode, zoom, resolutionX, resolutionY, d->canvasController->preferredCenter());
}

void KoZoomController::setZoom(KoZoomMode::Mode mode, qreal zoom, qreal resolutionX, qreal resolutionY, const QPointF &stillPoint)
{
    if (d->zoomHandler->zoomMode() == mode && qFuzzyCompare(d->zoomHandler->zoom(), zoom) && qFuzzyCompare(d->zoomHandler->resolutionX(), resolutionX)
        && qFuzzyCompare(d->zoomHandler->resolutionY(), resolutionY)) {
        return; // no change
    }

    qreal oldEffectiveZoom = d->action->effectiveZoom();
    QSize oldPageViewportSize = documentToViewport(d->pageSize);
    QSize oldTextViewportSize = documentToViewport(QSizeF(d->textMaxX - d->textMinX, 1));
    qreal yfixAlignTop = d->canvasController->viewportSize().height();

    if (!qFuzzyCompare(d->zoomHandler->resolutionX(), resolutionX) || !qFuzzyCompare(d->zoomHandler->resolutionY(), resolutionY)) {
        d->zoomHandler->setResolution(resolutionX, resolutionY);
    }

    if (mode == KoZoomMode::ZOOM_CONSTANT) {
        if (zoom == 0.0)
            return;
        d->action->setZoom(zoom);
    } else if (mode == KoZoomMode::ZOOM_WIDTH) {
        zoom = (d->canvasController->viewportSize().width() - 2 * d->fitMargin) / (oldPageViewportSize.width() / d->zoomHandler->zoom());
        d->action->setSelectedZoomMode(mode);
        d->action->setEffectiveZoom(zoom);
    } else if (mode == KoZoomMode::ZOOM_PAGE) {
        zoom = (d->canvasController->viewportSize().width() - 2 * d->fitMargin) / (oldPageViewportSize.width() / d->zoomHandler->zoom());
        zoom = qMin(zoom, (d->canvasController->viewportSize().height() - 2 * d->fitMargin) / (oldPageViewportSize.height() / d->zoomHandler->zoom()));

        d->action->setSelectedZoomMode(mode);
        d->action->setEffectiveZoom(zoom);
    } else if (mode == KoZoomMode::ZOOM_TEXT) {
        zoom = (d->canvasController->viewportSize().width() - 2 * d->fitMargin) / (oldTextViewportSize.width() / d->zoomHandler->zoom());
        d->action->setSelectedZoomMode(mode);
        d->action->setEffectiveZoom(zoom);
    }

    d->zoomHandler->setZoomMode(mode);
    d->zoomHandler->setZoom(d->action->effectiveZoom());

#ifdef DEBUG
    if (!d->documentSize.isValid())
        warnWidgets << "Setting zoom while there is no document size set, this will fail";
    else if (d->pageSize.width() > d->documentSize.width() || d->pageSize.height() > d->documentSize.height())
        warnWidgets << "ZoomController; Your page size is larger than your document size (" << d->pageSize << " > " << d->documentSize << ")\n";
#endif

    QSize documentViewportSize = documentToViewport(d->documentSize);

    // Tell the canvasController that the zoom has changed
    // Actually canvasController doesn't know about zoom, but the document in pixels
    // has changed as a result of the zoom change
    // To allow listeners of offset changes to react on the real new offset and not on the
    // intermediate offsets, we block the signals here, and emit by ourselves later.
    d->canvasController->proxyObject->blockSignals(true);
    d->canvasController->updateDocumentSize(documentViewportSize, true);
    d->canvasController->proxyObject->blockSignals(false);

    // Finally ask the canvasController to recenter
    if (d->canvasController->canvasMode() == KoCanvasController::Infinite) {
        QPointF documentCenter;
        if (mode == KoZoomMode::ZOOM_WIDTH || mode == KoZoomMode::ZOOM_PAGE) {
            documentCenter = QRectF(QPointF(), documentViewportSize).center();
        } else {
            qreal zoomCoeff = d->action->effectiveZoom() / oldEffectiveZoom;
            QPointF oldCenter = d->canvasController->preferredCenter();
            documentCenter = stillPoint * zoomCoeff - (stillPoint - 1.0 / zoomCoeff * oldCenter);
        }
        d->canvasController->setPreferredCenter(documentCenter);
    } else if (mode == KoZoomMode::ZOOM_TEXT) {
        QPointF documentCenter = d->canvasController->preferredCenter();
        yfixAlignTop -= d->canvasController->viewportSize().height();

        documentCenter.setX(d->zoomHandler->documentToViewX(d->textMinX + d->textMaxX) * 0.5);
        documentCenter.setY(documentCenter.y() - yfixAlignTop);
        d->canvasController->setPreferredCenter(documentCenter);
    } else {
        if (d->canvasController->canvasMode() == KoCanvasController::AlignTop) {
            QPointF documentCenter = d->canvasController->preferredCenter();
            documentCenter.setX(0.0);
            d->canvasController->setPreferredCenter(documentCenter);
        } else {
            d->canvasController->recenterPreferred();
        }
    }

    // now that we have the final offset, let's emit some signals
    if (mode != KoZoomMode::ZOOM_TEXT) {
        d->canvasController->proxyObject->emitCanvasOffsetXChanged(d->canvasController->canvasOffsetX());
        d->canvasController->proxyObject->emitCanvasOffsetYChanged(d->canvasController->canvasOffsetY());
    }
    Q_EMIT zoomChanged(mode, d->action->effectiveZoom());
}

QSize KoZoomController::documentToViewport(const QSizeF &size)
{
    return d->zoomHandler->documentToView(size).toSize();
}

void KoZoomController::setAspectMode(bool status)
{
    if (d->action) {
        d->action->setAspectMode(status);
    }
}
// have to include this because of Q_PRIVATE_SLOT
#include <moc_KoZoomController.cpp>
