import PlaceablePaletteMixin from "./placeable-palette-mixin.mjs";
import AmbientLightConfig from "../ambient-light-config.mjs";
import AdaptiveLightingShader from "../../../canvas/rendering/shaders/lighting/base-lighting.mjs";
import {LightData} from "@common/data/data.mjs";

/**
 * A dialog that provides bulk operation or default values for newly-created ambient lights.
 * @extends {AmbientLightConfig}
 * @mixes PlaceablePaletteMixin
 */
export default class AmbientLightPalette extends PlaceablePaletteMixin(AmbientLightConfig) {
  /** @override */
  static DEFAULT_OPTIONS = {
    id: "ambient-light-palette"
  };

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

  /**
   * The setting key where default data is saved.
   * @type {string}
   */
  static SETTING_KEY = "ambientLightPalette";

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

  /**
   * The placeable document.
   * @type {string}
   */
  static documentName = "AmbientLight";

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

  /** @override */
  static TABS = {};

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

  /** @override */
  static PARTS = {
    body: { template: "templates/scene/palette/ambient-light/body.hbs" },
    footer: { template: "templates/generic/form-footer.hbs" }
  };

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

  /** @override */
  static get schema() {
    const schema = super.schema;
    schema.removeFields(["x", "y", "name", "locked", "flags", "rotation", "config"]);

    // Replace config with a SchemaField that excludes dim and bright
    const { dim: _d, bright: _b, ...config} = LightData.defineSchema();
    schema.extendFields({ config: new foundry.data.fields.SchemaField(config) });

    return schema;
  }

  /* -------------------------------------------- */
  /*  Rendering                                   */
  /* -------------------------------------------- */

  /** @inheritDoc */
  async _prepareContext(options) {
    // Restore fields excluded from the palette schema for select mode display
    if ( this.isSelect ) {
      const source = this.controlled.at(0)?._source;
      this.document.updateSource({
        rotation: source?.rotation ?? 0,
        config: { dim: source?.config.dim ?? 0, bright: source?.config.bright ?? 0 }
      });
    }
    const context = await super._prepareContext(options);
    context.isDarkness = context.document.config.negative;
    context.colorationTechniques = AdaptiveLightingShader.SHADER_TECHNIQUES;
    context.lightAnimations = context.isDarkness ? CONFIG.Canvas.darknessAnimations : CONFIG.Canvas.lightAnimations;
    return context;
  }

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

  /** @override */
  async _preparePartContext(partId, context) {
    context.partId = `${this.id}-${partId}`;
    return context;
  }

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

  /** @inheritDoc */
  _onChangeForm(formConfig, event) {
    if ( (event.target.name === "config.negative") && this.isSelect ) {
      this.element.querySelector('[data-sync="details-advanced"]').hidden = event.target.checked;
    }
    return super._onChangeForm(formConfig, event);
  }
}
