import {ShapeControls} from "@client/canvas/containers/elements/shape-controls.mjs";

/**
 * @import AmbientLightDocument from "@client/documents/ambient-light.mjs";
 * @import AmbientLight from "@client/canvas/placeables/ambient-light.mjs";
 * @import LightingLayer from "@client/canvas/layers/lighting.mjs";
 * @import {CircleShapeData, ConeShapeData} from "@common/data/_module.mjs";
 */

/**
 * Controls for a AmbientLight shape.
 * @extends {ShapeControls<AmbientLightDocument, AmbientLight, LightingLayer, CircleShapeData|ConeShapeData>}
 */
export default class AmbientLightShapeControls extends ShapeControls {

  /** @inheritDoc */
  _onDragStart(event) {
    super._onDragStart(event);
    const {dim, bright} = this.document.config;
    const radius = Math.max(dim, bright);
    event.interactionData.dimRatio = dim / radius;
    event.interactionData.brightRatio = bright / radius;

    // Handle transformation of other controlled sources
    if ( event.interactionData.handle.name === "translate" ) {
      const others = event.interactionData.others = [];
      for ( const object of this.layer.controlled ) {
        if ( object !== this.object ) others.push(ShapeControls._createDragPreview(object));
      }
    }
  }

  /* -------------------------------------------- */

  /** @override */
  _updateDragPreview(event) {
    const {shape, preview, dimRatio, brightRatio} = event.interactionData;
    const {x, y} = shape;
    const radius = shape.radius / canvas.dimensions.distancePixels;
    const dim = dimRatio * radius;
    const bright = brightRatio * radius;
    const data = {x, y, config: {dim, bright}};
    if ( shape.type === "cone" ) {
      data.rotation = shape.rotation - 90;
      data.config.angle = shape.angle;
    }
    preview.document.updateSource(data);
    preview.renderFlags.set({refreshTransform: true, refreshField: true});
    preview.initializeLightSource();

    // Handle translation of other controlled sources
    if ( !event.interactionData.others?.length ) return;
    const dx = preview.document.x - this.document.x;
    const dy = preview.document.y - this.document.y;
    for ( const preview of event.interactionData.others ) {
      const {x, y} = preview._original.document._source;
      preview.document.updateSource({x: x + dx, y: y + dy});
      preview.renderFlags.set({refreshPosition: true, refreshField: true});
      preview.initializeLightSource();
    }
  }

  /* -------------------------------------------- */

  /** @override */
  _prepareDragDropUpdate(event) {
    const {x, y, rotation, config: {dim, bright, angle}} = event.interactionData.preview.document._source;
    return {x, y, rotation, config: {dim, bright, angle}};
  }

  /* -------------------------------------------- */

  /** @inheritDoc */
  _onDragDrop(event) {

    // Handle translation of multiple controlled sources
    if ( event.interactionData.others?.length ) {
      event.interactionData.clearPreviewContainer = false;
      const {x, y, rotation, config: {dim, bright, angle}} = event.interactionData.preview.document._source;
      const updates = [{_id: this.document.id, x, y, rotation, config: {dim, bright, angle}}];
      for ( const preview of event.interactionData.others ) {
        const object = preview._original;
        const {x, y, rotation, config: {dim, bright, angle}} = preview.document._source;
        updates.push({_id: object.document.id, x, y, rotation, config: {dim, bright, angle}});
      }
      this.document.parent.updateEmbeddedDocuments("AmbientLight", updates).finally(() => {
        this.layer.clearPreviewContainer();
      });
      return;
    }

    return super._onDragDrop(event);
  }
}
