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

/**
 * @import AmbientSoundDocument from "@client/documents/ambient-sound.mjs";
 * @import AmbientSound from "@client/canvas/placeables/ambient-sound.mjs";
 * @import SoundsLayer from "@client/canvas/layers/sounds.mjs";
 * @import {CircleShapeData} from "@common/data/_module.mjs";
 */

/**
 * Controls for a AmbientSound shape.
 * @extends {ShapeControls<AmbientSoundDocument, AmbientSound, SoundsLayer, CircleShapeData>}
 */
export default class AmbientSoundShapeControls extends ShapeControls {

  /** @inheritDoc */
  _onDragStart(event) {
    super._onDragStart(event);

    // 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} = event.interactionData;
    const {x, y} = shape;
    const radius = shape.radius / canvas.dimensions.distancePixels;
    preview.document.updateSource({x, y, radius});
    preview.renderFlags.set({refreshTransform: true, refreshField: true});
    preview.initializeSoundSource();

    // 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 object = preview._original;
      const {x, y} = object.document._source;
      preview.document.updateSource({x: x + dx, y: y + dy});
      preview.renderFlags.set({refreshPosition: true, refreshField: true});
      preview.initializeSoundSource();
    }
  }

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

  /** @override */
  _prepareDragDropUpdate(event) {
    const {x, y, radius} = event.interactionData.preview.document._source;
    return {x, y, radius};
  }

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

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

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

    return super._onDragDrop(event);
  }
}
