import { ObservablePoint } from "./observablePoint";
import { Matrix } from "../matrix";

export class Transform {
  static IDENTITY = new Transform();

  worldTransform = new Matrix();

  localTransform = new Matrix();

  position: ObservablePoint = new ObservablePoint(this.onChange, this, 0, 0);

  scale: ObservablePoint = new ObservablePoint(this.onChange, this, 1, 1);

  pivot: ObservablePoint = new ObservablePoint(this.onChange, this, 0, 0);

  skew: ObservablePoint = new ObservablePoint(this.updateSkew, this, 0, 0);

  _rotation = 0;

  // The X-coordinate value of the normalized local X axis,
  //the first column of the local transformation matrix without a scale.
  _cx = 1;

  /**
   * The Y-coordinate value of the normalized local X axis,
   * the first column of the local transformation matrix without a scale.
   */
  _sx = 0;

  /**
   * The X-coordinate value of the normalized local Y axis,
   * the second column of the local transformation matrix without a scale.
   */
  _cy = 0;

  /**
   * The Y-coordinate value of the normalized local Y axis,
   * the second column of the local transformation matrix without a scale.
   */
  _sy = 1;

  _localID = 0;

  /**
   * The locally unique ID of the local transform
   * used to calculate the current local transformation matrix.
   */
  _currentLocalID = 0;

  /**
   * The locally unique ID of the world transform.
   */
  _worldID = 0;

  /**
   * The locally unique ID of the parent's world transform
   * used to calculate the current world transformation matrix.
   */
  _parentID = 0;

  onChange() {
    this._localID++;
  }

  /**
   * Called when the skew or the rotation changes.
   */
  updateSkew() {
    this._cx = Math.cos(this._rotation + this.skew.y);
    this._sx = Math.sin(this._rotation + this.skew.y);
    this._cy = -Math.sin(this._rotation - this.skew.x); // cos, added PI/2
    this._sy = Math.cos(this._rotation - this.skew.x); // sin, added PI/2
    this._localID++;
  }

  /**
   * Updates the local transformation matrix.
   */
  updateLocalTransform() {
    const lt = this.localTransform;

    if (this._localID !== this._currentLocalID) {
      // get the matrix values of the displayobject based on its transform properties..
      lt.a = this._cx * this.scale.x;
      lt.b = this._sx * this.scale.x;
      lt.c = this._cy * this.scale.y;
      lt.d = this._sy * this.scale.y;

      lt.tx = this.position.x - (this.pivot.x * lt.a + this.pivot.y * lt.c);
      lt.ty = this.position.y - (this.pivot.x * lt.b + this.pivot.y * lt.d);
      this._currentLocalID = this._localID;

      // force an update..
      this._parentID = -1;
    }
  }

  /**
   * Updates the local and the world transformation matrices.
   */
  updateTransform(parentTransform:Transform) {
    const lt = this.localTransform;

    if (this._localID !== this._currentLocalID) {
      // get the matrix values of the displayobject based on its transform properties..
      lt.a = this._cx * this.scale.x;
      lt.b = this._sx * this.scale.x;
      lt.c = this._cy * this.scale.y;
      lt.d = this._sy * this.scale.y;

      lt.tx = this.position.x - (this.pivot.x * lt.a + this.pivot.y * lt.c);
      lt.ty = this.position.y - (this.pivot.x * lt.b + this.pivot.y * lt.d);
      this._currentLocalID = this._localID;

      // force an update..
      this._parentID = -1;
    }

    if (this._parentID !== parentTransform._worldID) {
      // concat the parent matrix with the objects transform.
      const pt = parentTransform.worldTransform;
      const wt = this.worldTransform;

      wt.a = lt.a * pt.a + lt.b * pt.c;
      wt.b = lt.a * pt.b + lt.b * pt.d;
      wt.c = lt.c * pt.a + lt.d * pt.c;
      wt.d = lt.c * pt.b + lt.d * pt.d;
      wt.tx = lt.tx * pt.a + lt.ty * pt.c + pt.tx;
      wt.ty = lt.tx * pt.b + lt.ty * pt.d + pt.ty;

      this._parentID = parentTransform._worldID;

      // update the id of the transform..
      this._worldID++;
    }
  }

  setFromMatrix(matrix:Matrix) {
    matrix.decompose(this);
    this._localID++;
  }


  get rotation() {
    return this._rotation;
  }

  set rotation( value :number ) {
    if (this._rotation !== value) {
      this._rotation = value;
      this.updateSkew();
    }
  }
}