import DocumentDirectory from "../document-directory.mjs";

/**
 * @import Scene from "@client/documents/scene.mjs";
 */

/**
 * The World Scene directory listing.
 * @extends {DocumentDirectory<Scene>}
 */
export default class SceneDirectory extends DocumentDirectory {
  /** @override */
  static DEFAULT_OPTIONS = {
    renderUpdateKeys: ["background", "thumb"],
    collection: "Scene"
  };

  /** @override */
  static tabName = "scenes";

  /** @override */
  static _entryPartial = "templates/sidebar/partials/scene-partial.hbs";

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

  /** @inheritDoc */
  _canRender(options) {
    if ( !game.user.isGM ) return false;
    return super._canRender(options);
  }

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

  /** @inheritDoc */
  _getEntryContextOptions() {
    const options = [
      {
        label: "SCENE.View",
        icon: "fa-solid fa-eye",
        visible: li => !canvas.ready || (li.dataset.entryId !== canvas.scene.id),
        onClick: (event, li) => game.scenes.get(li.dataset.entryId)?.view()
      },
      {
        label: "SCENE.Activate",
        icon: "fa-solid fa-bullseye",
        visible: li => game.user.isGM && !game.scenes.get(li.dataset.entryId)?.active,
        onClick: (event, li) => game.scenes.get(li.dataset.entryId)?.activate()
      },
      ...super._getEntryContextOptions()
    ];
    const configureOwnershipIndex = options.findIndex(o => o.label === "OWNERSHIP.Configure");
    options.splice(configureOwnershipIndex, 1, {
      label: "SCENE.Notes",
      icon: "fa-solid fa-scroll",
      visible: li => game.scenes.get(li.dataset.entryId)?.journal,
      onClick: (event, li) => {
        const scene = game.scenes.get(li.dataset.entryId);
        scene?.journal?.sheet?.render(true, { pageId: scene.journalEntryPage });
      }
    }, {
      label: "SCENE.Preload",
      icon: "fa-solid fa-download",
      visible: li => game.user.isGM,
      onClick: (event, li) => {
        const sceneId = li.dataset.entryId;
        game.scenes.preload(sceneId, {broadcast: true});
      }
    }, {
      label: "SCENE.ToggleNav",
      icon: "fa-solid fa-compass",
      visible: li => game.user.isGM && !game.scenes.get(li.dataset.entryId)?.active,
      onClick: (event, li) => {
        const scene = game.scenes.get(li.dataset.entryId);
        scene?.update({ navigation: !scene.navigation });
      }
    }, {
      label: "SCENE.GenerateThumb",
      icon: "fa-solid fa-image",
      visible: li => {
        if ( game.settings.get("core", "noCanvas") ) return false;
        const scene = game.scenes.get(li.dataset.entryId);
        if ( scene.isView ) return !!canvas.level?.background?.src;
        return !!scene.initialLevel.background?.src;
      },
      onClick: async (event, li) => {
        const scene = game.scenes.get(li.dataset.entryId);
        try {
          const { thumb } = await scene?.createThumbnail({ level: scene._view }) ?? {};
          if ( thumb ) await scene.update({ thumb }, { diff: false });
          ui.notifications.info("SCENE.GenerateThumbSuccess", { format: { name: scene.name } });
        } catch(err) {
          ui.notifications.error(err.message);
        }
      }
    });
    return options;
  }

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

  /** @inheritDoc */
  _getFolderContextOptions() {
    return super._getFolderContextOptions().filter(o => o.label !== "OWNERSHIP.Configure");
  }
}
