// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef QLOTTIESPATIALPROPERTY_P_H
#define QLOTTIESPATIALPROPERTY_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QPointF>
#include <QPainterPath>

#include <QtLottie/private/qlottieproperty_p.h>

QT_BEGIN_NAMESPACE

class QLottieSpatialProperty : public QLottieProperty2D<QPointF>
{
public:
    virtual void construct(const QJsonObject &definition) override
    {
        qCDebug(lcLottieQtLottieParser) << "QLottieSpatialProperty::parse()";
        m_bezierPath.setCachingEnabled(true);
        QLottieProperty2D<QPointF>::construct(definition);
    }

    virtual EasingSegment<QPointF> parseKeyframe(const QJsonObject keyframe,
                                                 bool fromExpression) override
    {
        m_subPaths.append(QPainterPath{});

        EasingSegment<QPointF> easing =
                QLottieProperty2D<QPointF>::parseKeyframe(keyframe, fromExpression);

        // No need to parse further incomplete keyframes (i.e. last keyframes)
        if (!easing.complete) {
            return easing;
        }

        qreal tix = 0, tiy = 0, tox = 0, toy = 0;
        if (fromExpression) {
            // If spatial property definition originates from
            // an expression (specifically Slider), it contains scalar
            // property. It must be expanded to both x and y coordinates
            QJsonArray iArr = keyframe.value(u"i"_s).toArray();
            QJsonArray oArr = keyframe.value(u"o"_s).toArray();

            if (iArr.count() && oArr.count()) {
                tix = iArr.at(0).toDouble();
                tiy = tix;
                tox = oArr.at(0).toDouble();
                toy = tox;
            }
        } else {
            QJsonArray tiArr = keyframe.value(u"ti"_s).toArray();
            QJsonArray toArr = keyframe.value(u"to"_s).toArray();

            if (tiArr.count() && toArr.count()) {
                tix = tiArr.at(0).toDouble();
                tiy = tiArr.at(1).toDouble();
                tox = toArr.at(0).toDouble();
                toy = toArr.at(1).toDouble();
            }
        }
        QPointF s(easing.startValue);
        QPointF e(easing.endValue);
        QPointF c1(tox, toy);
        QPointF c2(tix, tiy);

        moveTo(s);
        if (c1.isNull() && c2.isNull()) {
            lineTo(e);
        } else {
            c1 += s;
            c2 += e;
            cubicTo(c1, c2, e);
        }

        return easing;
    }

    virtual EasingSegment<QPointF> parseKeyframe(const QJsonObject keyframe,
                                                 const QJsonObject nextKeyframe,
                                                 bool fromExpression) override
    {
        m_subPaths.append(QPainterPath{});

        EasingSegment<QPointF> easing =
                QLottieProperty2D<QPointF>::parseKeyframe(keyframe, nextKeyframe, fromExpression);

        // No need to parse further incomplete keyframes (i.e. last keyframes)
        if (!easing.complete) {
            return easing;
        }

        qreal tix = 0, tiy = 0, tox = 0, toy = 0;
        if (fromExpression) {
            // If spatial property definition originates from
            // an expression (specifically Slider), it contains scalar
            // property. It must be expanded to both x and y coordinates
            QJsonArray iArr = keyframe.value(u"i"_s).toArray();
            QJsonArray oArr = keyframe.value(u"o"_s).toArray();

            if (iArr.count() && oArr.count()) {
                tix = iArr.at(0).toDouble();
                tiy = tix;
                tox = oArr.at(0).toDouble();
                toy = tox;
            }
        } else {
            QJsonArray tiArr = keyframe.value(u"ti"_s).toArray();
            QJsonArray toArr = keyframe.value(u"to"_s).toArray();

            if (tiArr.count() && toArr.count()) {
                tix = tiArr.at(0).toDouble();
                tiy = tiArr.at(1).toDouble();
                tox = toArr.at(0).toDouble();
                toy = toArr.at(1).toDouble();
            }
        }
        QPointF s(easing.startValue);
        QPointF e(easing.endValue);
        QPointF c1(tox, toy);
        QPointF c2(tix, tiy);

        moveTo(s);
        if (c1.isNull() && c2.isNull()) {
            lineTo(e);
        } else {
            c1 += s;
            c2 += e;
            cubicTo(c1, c2, e);
        }

        return easing;
    }

    virtual bool update(int frame) override
    {
        if (!m_animated)
            return false;

        int adjustedFrame = qBound(m_startFrame, frame, m_endFrame);
        if (const EasingSegment<QPointF> *easing = getEasingSegment(adjustedFrame)) {
            qreal progress = ((adjustedFrame - m_startFrame) * 1.0) / (m_endFrame - m_startFrame);
            qreal easedValue = easing->valueForProgress(progress);
            m_value = m_bezierPath.pointAtPercent(easedValue);
        }

        return true;
    }

    bool hasCurves() const
    {
        for (qsizetype i = 0; i < m_subPaths.size(); ++i) {
            const QPainterPath path = m_subPaths.at(i);
            for (int j = 0; j < path.elementCount(); ++j)
                if (path.elementAt(j).isCurveTo())
                    return true;
        }

        return false;
    }

    const QList<QPainterPath> &subPaths() const
    {
        return m_subPaths;
    }

private:
    void moveTo(const QPointF &e)
    {
        m_bezierPath.moveTo(e);
        m_subPaths.last().moveTo(e);
    }

    void lineTo(const QPointF &e)
    {
        m_bezierPath.lineTo(e);
        m_subPaths.last().lineTo(e);
    }

    void cubicTo(const QPointF &c1, const QPointF &c2, const QPointF &e)
    {
        m_bezierPath.cubicTo(c1, c2, e);
        m_subPaths.last().cubicTo(c1, c2, e);
    }

    QPainterPath m_bezierPath;
    QList<QPainterPath> m_subPaths;
};

QT_END_NAMESPACE

#endif // QLOTTIESPATIALPROPERTY_P_H
