import { world, } from "@minecraft/server";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
import { namespace } from "../../config/_config";
import { getPlayerUnlockedForms, getWorldUnlockedForms } from "../formModule";
export var Behavior;
(function (Behavior) {
    Behavior["FRIENDLY"] = "friendly";
    Behavior["NEUTRAL"] = "neutral";
    Behavior["HOSTILE"] = "hostile";
})(Behavior || (Behavior = {}));
export class ActionForm {
    constructor(id, options, isMainForm = false) {
        this.id = id;
        this.options = options;
        this._isMainForm = isMainForm;
    }
    translateString(str) {
        return { translate: str };
    }
    translateStrings(strings) {
        return strings.map((str) => ({ translate: str }));
    }
    getTitle() {
        return this.translateString(this.options.title || `item.${namespace}:${this.id}`);
    }
    get isMainForm() {
        return this._isMainForm;
    }
    compile(player, referencedForms, referencesMap, unlockedForms) {
        const form = new ActionFormData();
        form.title(this.getTitle());
        const body = [];
        const newlines = { text: "\n\n" };
        const singleNewline = { text: "\n" };
        if (this.options.description) {
            this.translateStrings(this.options.description).forEach((desc) => {
                body.push(singleNewline, desc);
            });
        }
        const wikiInfo = this.options.wikiInfo;
        if (wikiInfo) {
            if (wikiInfo) {
                if (wikiInfo.biomes) {
                    if (wikiInfo.biomes.length === 0) {
                        body.push(newlines, { translate: `${namespace}.form.no_biomes` });
                    }
                    else {
                        body.push(newlines, { translate: `${namespace}.form.biomes` }, { text: " " });
                        for (const biome of wikiInfo.biomes) {
                            body.push({ translate: `${namespace}.form.biome.${biome}` }, { text: ", " });
                        }
                        body.pop(); // Remove trailing comma
                    }
                }
                if (wikiInfo.isRideable !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.isRideable
                            ? `${namespace}.form.rideable`
                            : `${namespace}.form.not_rideable`,
                    });
                }
                if (wikiInfo.isBreedable !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.isBreedable
                            ? `${namespace}.form.breedable`
                            : `${namespace}.form.not_breedable`,
                    });
                }
                if (wikiInfo.isTameable !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.isTameable
                            ? `${namespace}.form.tameable`
                            : `${namespace}.form.not_tameable`,
                    });
                }
                if (wikiInfo.behavior) {
                    body.push(newlines, {
                        translate: `${namespace}.form.behavior.${wikiInfo.behavior}`,
                    });
                }
                if (wikiInfo.food) {
                    if (wikiInfo.food.length === 0) {
                        body.push(newlines, { translate: `${namespace}.form.no_food` });
                    }
                    else {
                        body.push(newlines, { translate: `${namespace}.form.food` }, { text: " " });
                        for (const item of wikiInfo.food) {
                            body.push({ translate: item }, { text: ", " });
                        }
                        body.pop(); // Remove trailing comma
                    }
                }
                if (wikiInfo.drops) {
                    if (wikiInfo.drops.length === 0) {
                        body.push(newlines, { translate: `${namespace}.form.no_drops` });
                    }
                    else {
                        body.push(newlines, { translate: `${namespace}.form.drops` }, { text: " " });
                        for (const drop of wikiInfo.drops) {
                            body.push({ translate: drop }, { text: ", " });
                        }
                        body.pop(); // Remove trailing comma
                    }
                }
                if (wikiInfo.hasCraftingTableRecipe !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.hasCraftingTableRecipe
                            ? `${namespace}.form.recipe`
                            : `${namespace}.form.no_recipe`,
                    });
                }
                if (wikiInfo.hasCustomTableRecipe !== undefined) {
                    if (wikiInfo.hasCustomTableRecipe.recipe !== undefined) {
                        body.push(newlines, {
                            translate: wikiInfo.hasCustomTableRecipe.recipe
                                ? `${namespace}.form.custom.recipe`
                                : `${namespace}.form.custom.no_recipe`,
                        }, { translate: `tile.${wikiInfo.hasCustomTableRecipe.blockID}.name` }, { text: "§r." });
                    }
                }
                if (wikiInfo.isShearable !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.isShearable
                            ? `${namespace}.form.shearable`
                            : `${namespace}.form.no_shearable`,
                    });
                }
                if (wikiInfo.isCompostable !== undefined) {
                    body.push(newlines, {
                        translate: wikiInfo.isCompostable
                            ? `${namespace}.form.compostable`
                            : `${namespace}.form.not_compostable`,
                    });
                }
            }
        }
        let buttonCount = 0;
        let backButtonIndex;
        // Add buttons for references
        const references = referencesMap.get(this.id);
        if (references) {
            references.forEach((refId) => {
                const refForm = referencedForms.get(refId);
                if (!refForm)
                    return;
                // For each referenced form, determine its property and get correct unlocked set
                const refScope = refForm.options.unlock?.property === "world" ? "world" : "player";
                const refUnlockedForms = refScope === "world" ? getWorldUnlockedForms() : getPlayerUnlockedForms(player);
                if (refForm instanceof AchievementForm) {
                    const achievementUnlocked = refUnlockedForms.has(refId);
                    const title = { translate: `${refForm.getTitle()}` };
                    const iconPath = achievementUnlocked
                        ? refForm.achievementOptions.iconPath.unlocked
                        : refForm.achievementOptions.iconPath.locked;
                    form.button(title, iconPath);
                    buttonCount++;
                }
                else {
                    if (refUnlockedForms.has(refId) || !refForm.options.unlock) {
                        const title = refForm instanceof ActionForm ? refForm.getTitle() : { translate: refForm.getTitle() };
                        const iconPath = refForm.options.iconPath || "";
                        form.button(title, iconPath);
                        buttonCount++;
                    }
                }
            });
        }
        // Add back button if not main form
        if (!this.isMainForm) {
            form.button({ translate: `${namespace}.form.back` });
            backButtonIndex = buttonCount;
            buttonCount++;
        }
        body.push(newlines);
        form.body({ rawtext: body });
        return { form, buttonCount, backButtonIndex };
    }
}
export class ModalForm {
    constructor(id, options, _isMainForm = false) {
        this.id = id;
        this.options = options;
        this._isMainForm = _isMainForm;
    }
    getTitle() {
        return this.options.title;
    }
    get isMainForm() {
        return this._isMainForm;
    }
    compile() {
        const form = new ModalFormData().title(this.options.title);
        this.options.controls.forEach((control) => {
            let value = control.defaultValue;
            if (control.dynamicProperty) {
                const dynamicValue = world.getDynamicProperty(control.dynamicProperty);
                if (dynamicValue !== undefined) {
                    value = dynamicValue;
                }
            }
            switch (control.type) {
                case "toggle":
                    form.toggle(control.label, value);
                    break;
                case "slider":
                    form.slider(control.label, control.options.min, control.options.max, control.options.step, value);
                    break;
                case "dropdown":
                    form.dropdown(control.label, control.options, value);
                    break;
                case "textField":
                    form.textField(control.label, control.options.placeholder, value);
                    break;
            }
        });
        return form;
    }
}
export class AchievementForm extends ActionForm {
    constructor(id, options, isMainForm = false) {
        super(id, {
            title: options.title,
            description: options.description,
            references: options.references,
            iconPath: "",
            unlock: options.unlock,
        }, isMainForm);
        this.achievementOptions = options;
    }
    compile(player, referencedForms, referencesMap, unlockedForms) {
        const form = new ActionFormData();
        const isUnlocked = unlockedForms.has(this.id);
        form.title({ translate: this.achievementOptions.title });
        const body = [];
        const doubleNewline = { text: "\n\n" };
        const singleNewline = { text: "\n" };
        // Show description
        this.achievementOptions.description.forEach((desc) => {
            body.push(singleNewline, { translate: desc });
        });
        body.push(doubleNewline);
        // Show unlocked/locked status
        if (isUnlocked) {
            body.push(singleNewline, { translate: `${namespace}.achievement.unlocked` });
        }
        else {
            body.push(singleNewline, { translate: `${namespace}.achievement.locked` });
        }
        // Show rewards if defined
        const unlock = this.achievementOptions.unlock;
        if (unlock && unlock.reward) {
            const { xp, items } = unlock.reward;
            body.push({ text: "\n\n" }, { translate: `${namespace}.achievement.reward` }, { text: " " });
            let firstElement = true;
            if (typeof xp === "number") {
                body.push({ text: `${xp}xp` });
                firstElement = false;
            }
            if (items && items.length > 0) {
                items.forEach((itemData) => {
                    if (!firstElement) {
                        body.push({ text: " & " });
                    }
                    body.push({ translate: `item.${itemData.id}` }, { text: ` (${itemData.count}x)` });
                    firstElement = false;
                });
            }
        }
        let buttonCount = 0;
        let backButtonIndex;
        // Show referenced forms for AchievementForm
        // const references = referencesMap.get(this.id);
        // if (references) {
        //   references.forEach((refId) => {
        //     const refForm = referencedForms.get(refId);
        //     if (refForm) {
        //       const title =
        //         refForm instanceof ActionForm ? refForm.getTitle() : { translate: refForm.getTitle() };
        //       const statusText = isUnlocked ? "Unlocked" : "Locked";
        //       // Determine this referenced form's scope and get its own unlocked set
        //       const refScope = refForm.options.unlock?.property === "world" ? "world" : "player";
        //       const refUnlockedForms =
        //         refScope === "world" ? getWorldUnlockedForms() : getPlayerUnlockedForms(player);
        //       let refIcon = refForm.options.iconPath || "";
        //       if (refForm instanceof AchievementForm) {
        //         const refIsUnlocked = refUnlockedForms.has(refForm.id);
        //         refIcon = refIsUnlocked
        //           ? refForm.achievementOptions.iconPath.unlocked
        //           : refForm.achievementOptions.iconPath.locked;
        //       }
        //       const combinedTitle = { translate: refForm.getTitle() };
        //       form.button(
        //         {
        //           translate: `hey`,
        //         },
        //         refIcon
        //       );
        //       buttonCount++;
        //     }
        //   });
        // }
        // Add back button if not main form
        if (!this.isMainForm) {
            body.push(doubleNewline);
            form.button({ translate: `${namespace}.form.back` });
            backButtonIndex = buttonCount;
            buttonCount++;
        }
        else {
            body.push(doubleNewline);
        }
        form.body({ rawtext: body });
        return { form, buttonCount, backButtonIndex };
    }
}
