import { ActionFormData } from "@minecraft/server-ui";

export class WikiEntry {
    constructor(name, description, texturePath, backFunction) {
        this.name = name;
        this.description = description;
        this.texturePath = texturePath;
        this.backFunction = backFunction;
    }

    createDetailForm(player) {
        let form = new ActionFormData();
        form.title(this.name);
        form.body(this.description);
        form.button({ translate: 'cc_aql:guide.main.btn.back' }, "textures/cc/aql/ui/back_icon.png");

        form.show(player).then(response => {
			if (response.canceled || response.selection === undefined) return;
            if (response.selection !== undefined) {
                this.backFunction(player);
            }
        });
    }
}

export class WikiMenu {
    constructor({ title, body = "", entries, backFunction = null }) {
        this.title = title;
        this.body = body;
        this.entries = entries;
        this.backFunction = backFunction;
    }

    show(player) {
        let form = new ActionFormData();
        form.title(this.title);
        form.body(this.body);
        this.entries.forEach(entry => {
            form.button(entry.name, entry.texturePath);
        });

        if (this.backFunction) {
            form.button({ translate: 'cc_aql:guide.main.btn.back' }, "textures/cc/aql/ui/back_icon.png");
        }

        form.show(player).then(response => {
            if (response.canceled || response.selection === undefined) return;

            if (response.selection < this.entries.length) {
                let selectedEntry = this.entries[response.selection];
                selectedEntry.createDetailForm(player);
            } else if (this.backFunction) {
                this.backFunction(player);
            }
        });
    }
}

// Example
/*
-
-
-
-
const section1 = new WikiEntry(
    "Diamond Sword", "A powerful weapon made of diamond.", "textures/ui/diamond_sword.png", (player) => customMenu.show(player)
);
const section2 = new WikiEntry(
    "Iron Sword", "A powerful weapon made of diamond.", "textures/ui/iron_sword.png", (player) => customMenu.show(player)
);

const customMenu = new WikiMenu("Entities", [section1, section2], (player) => showMainMenu(player));

*/
/*
function showMainMenu(player) {
  let form = new ActionFormData();
  form.title("Addon Wiki");
  form.body("Welcome to the Addon Wiki. Explore custom entities and items.");

  form.button("Entities");
  form.button("Items");

  form.show(player).then(response => {
      if (response.selection === null) return;

      if (response.selection === 0) {
          entitiesMenu.show(player);
      } else if (response.selection === 1) {
          itemsMenu.show(player);
      }
  });
}

*/