import BaseAmbientLight from "@common/documents/ambient-light.mjs";
import CanvasDocumentMixin from "./abstract/canvas-document.mjs";
import {CircleShapeData, ConeShapeData} from "@client/data/shapes.mjs";

/**
 * The client-side AmbientLight document which extends the common BaseAmbientLight document model.
 * @extends BaseAmbientLight
 * @mixes ClientDocumentMixin
 * @category Documents
 *
 * @see {@link foundry.documents.Scene}: The Scene document type which contains AmbientLight documents
 * @see {@link foundry.applications.sheets.AmbientLightConfig}: The AmbientLight configuration
 *   application
 */
export default class AmbientLightDocument extends CanvasDocumentMixin(BaseAmbientLight) {

  /* -------------------------------------------- */
  /*  Model Properties                            */
  /* -------------------------------------------- */

  /**
   * Is this ambient light source global in nature?
   * @type {boolean}
   */
  get isGlobal() {
    return !this.walls;
  }

  /**
   * The circle or cone shape of this AmbientLight document.
   * @type {CircleShapeData|ConeShapeData}
   */
  shape = this.shape; // Workaround for subclass property instantiation issue.

  /* -------------------------------------------- */
  /*  Model Methods                               */
  /* -------------------------------------------- */

  /** @inheritDoc */
  prepareDerivedData() {
    super.prepareDerivedData();
    if ( this.parent ) {
      const d = this.parent.dimensions;
      this.x = Math.clamp(this.x, 0, d.width);
      this.y = Math.clamp(this.y, 0, d.height);
    }
    const {x, y, config: {dim, bright, angle}} = this;
    const grid = this.parent?.grid ?? foundry.documents.BaseScene.defaultGrid;
    const distancePixels = grid.size / grid.distance;
    const radius = Math.max(dim, bright) * distancePixels;
    this.shape = angle < 360 ? new ConeShapeData({x, y, radius, angle, rotation: this.rotation + 90}, {parent: this})
      : new CircleShapeData({x, y, radius}, {parent: this});
  }
}
