/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef DOM_SVG_SVGTRANSFORM_H_
#define DOM_SVG_SVGTRANSFORM_H_

#include "gfxMatrix.h"
#include "mozilla/dom/SVGTransformBinding.h"
#include "mozilla/gfx/Matrix.h"
#include "nsDebug.h"

namespace mozilla {

/*
 * The DOM wrapper class for this class is DOMSVGTransform.
 */
class SVGTransform {
 public:
  // Default ctor initialises to matrix type with identity matrix
  SVGTransform() = default;

  explicit SVGTransform(const gfxMatrix& aMatrix) : mMatrix(aMatrix) {}

  bool operator==(const SVGTransform& rhs) const {
    return mType == rhs.mType && mMatrix.ExactlyEquals(rhs.mMatrix) &&
           mAngle == rhs.mAngle && mOrigin == rhs.mOrigin;
  }

  void GetValueAsString(nsAString& aValue) const;

  float Angle() const { return mAngle; }
  void GetRotationOrigin(float& aOriginX, float& aOriginY) const {
    aOriginX = mOrigin.x;
    aOriginY = mOrigin.y;
  }
  uint16_t Type() const { return mType; }

  const gfxMatrix& GetMatrix() const { return mMatrix; }
  void SetMatrix(const gfxMatrix& aMatrix);
  void SetTranslate(float aTx, float aTy);
  void SetScale(float aSx, float aSy);
  void SetRotate(float aAngle, float aCx, float aCy);
  nsresult SetSkewX(float aAngle);
  nsresult SetSkewY(float aAngle);

  static uint16_t GetTransformTypeForString(const nsAString& aTransformType);

 protected:
  gfxMatrix mMatrix;
  gfx::Point mOrigin;
  float mAngle = 0.f;
  uint16_t mType = dom::SVGTransform_Binding::SVG_TRANSFORM_MATRIX;
};

/*
 * A slightly more light-weight version of SVGTransform for SMIL animation.
 *
 * Storing the parameters in an array (rather than a matrix) also allows simpler
 * (transform type-agnostic) interpolation and addition.
 *
 * The meaning of the mParams array depends on the transform type as follows:
 *
 * Type                | mParams[0], mParams[1], mParams[2], ...
 * --------------------+-----------------------------------------
 * translate           | tx, ty
 * scale               | sx, sy
 * rotate              | rotation-angle (in degrees), cx, cy
 * skewX               | skew-angle (in degrees)
 * skewY               | skew-angle (in degrees)
 * matrix              | a, b, c, d, e, f
 *
 * The matrix type is never generated by animation code (it is only produced
 * when the user inserts one via the DOM) and often requires special handling
 * when we do encounter it. Therefore many users of this class are only
 * interested in the first three parameters and so we provide a special
 * constructor for setting those parameters only.
 */
class SVGTransformSMILData {
 public:
  // Number of float-params required in constructor, if constructing one of the
  // 'simple' transform types (all but matrix type)
  static constexpr size_t kNumSimpleParams = 3;

  // Number of float-params required in constructor for matrix type.
  // This is also the number of params we actually store, regardless of type.
  static constexpr size_t kNumStoredParams = 6;

  using SimpleParams = std::array<float, kNumSimpleParams>;
  using StoredParams = std::array<float, kNumStoredParams>;

  explicit SVGTransformSMILData(uint16_t aType) : mTransformType(aType) {
    MOZ_ASSERT(aType >= dom::SVGTransform_Binding::SVG_TRANSFORM_MATRIX &&
                   aType <= dom::SVGTransform_Binding::SVG_TRANSFORM_SKEWY,
               "Unexpected transform type");
    mParams.fill(0.f);
  }

  SVGTransformSMILData(uint16_t aType, const SimpleParams& aParams)
      : mTransformType(aType) {
    MOZ_ASSERT(aType >= dom::SVGTransform_Binding::SVG_TRANSFORM_TRANSLATE &&
                   aType <= dom::SVGTransform_Binding::SVG_TRANSFORM_SKEWY,
               "Expected 'simple' transform type");
    std::copy(aParams.begin(), aParams.end(), mParams.begin());
    std::fill(mParams.begin() + kNumSimpleParams, mParams.end(), 0.f);
  }

  // Conversion to/from a fully-fledged SVGTransform
  explicit SVGTransformSMILData(const SVGTransform& aTransform);
  SVGTransform ToSVGTransform() const;

  float operator[](uint32_t aIndex) const { return mParams[aIndex]; }
  float& operator[](uint32_t aIndex) { return mParams[aIndex]; }

  bool operator==(const SVGTransformSMILData& aOther) const {
    if (mTransformType != aOther.mTransformType) return false;

    return mParams == aOther.mParams;
  }

  bool operator!=(const SVGTransformSMILData& aOther) const {
    return !(*this == aOther);
  }

  nsresult Distance(const SVGTransformSMILData& aOther,
                    double& aDistance) const;

  uint16_t TransformType() const { return mTransformType; }

 private:
  StoredParams mParams;
  uint16_t mTransformType;
};

}  // namespace mozilla

#endif  // DOM_SVG_SVGTRANSFORM_H_
