import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
import { world, ItemStack } from "@minecraft/server";
import { getOrCreateScoreboard } from "./utilities";
import { NAMESPACE } from "./anvilConstants";
export function guideBookInteractEvent(eventData) {
    const p = eventData.source;
    const item = eventData.itemStack;
    if (item.typeId === `${NAMESPACE}:dinos_guide`) {
        guideBook(p);
    }
}
function guideBook(p) {
    try {
        let form = new ActionFormData()
            .title({ rawtext: [{ translate: `${NAMESPACE}.guidebook.title` }] })
            .button({ translate: `${NAMESPACE}.guidebook.title_tutorial` }, "textures/hf_da/dinos_addon/ui/guidebook/tutorial.png")
            .button({ translate: `${NAMESPACE}.guidebook.title_settings` }, "textures/hf_da/dinos_addon/ui/guidebook/settings.png")
            .button({ translate: `${NAMESPACE}.guidebook.title_discord` }, "textures/hf_da/dinos_addon/ui/guidebook/discord.png")

        form.show(p).then((response) => {
            const inv = p.getComponent("inventory").container;
            const heldItem = inv.getItem(p.selectedSlotIndex);
            if (response.selection === 0) {
                if (heldItem?.typeId === `${NAMESPACE}:dinos_guide`) {
                    const newItem = new ItemStack(`${NAMESPACE}:dinos_guide2`, 1);
                    inv.setItem(p.selectedSlotIndex, newItem);
                }
            }
            if (response.selection === 1) {
                showSettingsForm(p);
            }
            if (response.selection === 2) {
                if (heldItem?.typeId === `${NAMESPACE}:dinos_guide`) {
                    const newItem = new ItemStack(`${NAMESPACE}:dinos_guide3`, 1);
                    inv.setItem(p.selectedSlotIndex, newItem);
                }
            }
        });
    } catch (e) { }
}
function showSettingsForm(p) {
    let settings = new ModalFormData();
    settings.title({ rawtext: [{ translate: `${NAMESPACE}.guidebook.title_settings` }] });
    for (let id in Settings) {
        let value = settingsScoreboard.getScore(id) != 0;
        settings.toggle({ rawtext: [{ translate: `${NAMESPACE}.guidebook.title_settings.${id}` }] }, value);
    }
    settings.show(p).then(response => {
        if (response.canceled || !response.formValues) return;
        let ids = Object.keys(Settings);
        for (let i = 0; i < ids.length; i++) {
            let id = ids[i];
            let newValue = response.formValues[i] ? 1 : 0;
            let prevValue = settingsScoreboard.getScore(id);
            if (newValue !== prevValue) {
                settingsScoreboard.setScore(id, newValue);
                let toggleMessage = newValue ? "enabled" : "disabled";
                world.sendMessage({ translate: `${NAMESPACE}.${id}_warning.${toggleMessage}` });
            }
        }
    });
}
const settingsScoreboard = getOrCreateScoreboard(`${NAMESPACE}.settings`);
const Settings = {
    dinosaurs_spawning: {},
    eggs_spawning: {},
    meteorite_fall: {},
};
for (let id in Settings) {
    if (!settingsScoreboard.hasParticipant(id)) {
        let initialValue = 0;
        if (id === 'dinosaurs_spawning' || id === 'eggs_spawning') {
            initialValue = 1;
        }
        settingsScoreboard.setScore(id, initialValue);
    }
}