import PlaceableConfig from "./placeable-config.mjs";
import FormDataExtended from "../ux/form-data-extended.mjs";

/**
 * @import TileDocument from "@client/documents/tile.mjs";
 */

/**
 * The Application responsible for configuring a single Tile document within a parent Scene.
 */
export default class TileConfig extends PlaceableConfig {
  /** @inheritDoc */
  static DEFAULT_OPTIONS = {
    classes: ["tile-config"],
    canCreate: true,
    window: {
      contentClasses: ["standard-form"],
      icon: "fa-solid fa-cubes"
    },
    position: {width: 480},
    form: {
      closeOnSubmit: true
    }
  };

  /** @override */
  static PARTS = {
    tabs: {template: "templates/generic/tab-navigation.hbs"},
    position: {template: "templates/scene/tile/position.hbs"},
    appearance: {template: "templates/scene/tile/appearance.hbs"},
    overhead: {template: "templates/scene/tile/overhead.hbs"},
    footer: {template: "templates/generic/form-footer.hbs"}
  };

  /** @override */
  static TABS = {
    sheet: {
      tabs: [
        {id: "position", icon: "fa-solid fa-location-dot"},
        {id: "appearance", icon: "fa-solid fa-image"},
        {id: "overhead", icon: "fa-solid fa-house"}
      ],
      initial: "position",
      labelPrefix: "TILE.TABS"
    }
  };

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

  /** @inheritDoc */
  get title() {
    if ( !this.document.id ) return _loc("TILE.ACTIONS.CREATE");
    return super.title;
  }

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

  /** @inheritDoc */
  _configureRenderOptions(options) {
    if (options.isFirstRender && !this.document._source.texture.src) {
      this.tabGroups.sheet = "appearance";
    }
    return super._configureRenderOptions(options);
  }

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

  /** @inheritDoc */
  async _prepareContext(options) {
    const context = await super._prepareContext(options);
    const submitText = `TILE.ACTIONS.${this.document.collection?.has(this.document.id) ? "UPDATE" : "CREATE"}`;
    return Object.assign(context, {
      tabClasses: "top-tabs",
      buttons: [{type: "submit", icon: "fa-solid fa-floppy-disk", label: submitText}]
    });
  }

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

  /** @inheritDoc */
  async _preparePartContext(partId, context, options) {
    const partContext = await super._preparePartContext(partId, context, options);
    if ( partId === "appearance" ) {
      partContext.hasVideo = foundry.helpers.media.VideoHelper.hasVideoExtension(context.source.texture.src);
    }
    const tab = partContext.tabs[partId];
    if ( tab ) partContext.tab = tab;
    return partContext;
  }

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

  /** @inheritDoc */
  _onChangeForm(formConfig, event) {
    super._onChangeForm(formConfig, event);

    // Show/hide video options
    const videoOptions = this.element.querySelector("fieldset[data-video-options]");
    if ( !videoOptions ) return;
    const formData = new FormDataExtended(this.form);
    const hasVideo = foundry.helpers.media.VideoHelper.hasVideoExtension(formData.object["texture.src"]);
    videoOptions.hidden = !hasVideo;
  }

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

  /** @inheritDoc */
  _previewChanges(changes) {
    const priorTextureSrc = this._preview.texture.src;
    super._previewChanges(changes);
    if ( !this._preview.rendered ) return;
    const redraw = this._preview.texture.src !== priorTextureSrc;
    this._preview.object.renderFlags.set({redraw});
  }
}
