import * as ui from "@minecraft/server-ui";

export function openTrickMenu(player, dog) {
    if (!dog) return;
    // const buttonIcons = dogMappings[dog.typeId].buttonMappings ?? mappings['default'].buttonMappings;
    // const sounds = dogMappings[dog.typeId].soundMappings ?? mappings['default'].soundMappings;

    const buttonIcons = {
        free: `textures/oct/dogs/forms/${dog.typeId.replace("oct_dogs:", "")}_free`,
        sit: `textures/oct/dogs/forms/${dog.typeId.replace("oct_dogs:", "")}_sit`,
        lie_down: `textures/oct/dogs/forms/${dog.typeId.replace("oct_dogs:", "")}_lie_down`,
        roll: `textures/oct/dogs/forms/${dog.typeId.replace("oct_dogs:", "")}_roll`,
        speak: `textures/oct/dogs/forms/${dog.typeId.replace("oct_dogs:", "")}_speak`,
        stand: `textures/oct/dogs/forms/${dog.typeId.replace("oct_dogs:", "")}_stand`,
        backflip: `textures/oct/dogs/forms/${dog.typeId.replace("oct_dogs:", "")}_backflip`
    };
    const sounds = {
        roll: `oct_dogs.${dog.typeId.replace("oct_dogs:", "")}.chase_tail`,
        stand: `oct_dogs.${dog.typeId.replace("oct_dogs:", "")}.pant`,
        backflip: `oct_dogs.${dog.typeId.replace("oct_dogs:", "")}.pant`
    };

    const dogName = dog.nameTag;

    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "forms.trick_menu.title" }] })
        .label(dogName ? { rawtext: [{ translate: "forms.trick_menu.label.name", with: [`${dogName}`] }] } : { rawtext: [{ translate: "forms.trick_menu.label.no_name" }] })
        .divider()
        .button({ rawtext: [{ translate: "forms.trick_menu.button.free" }] }, buttonIcons.free)
        .button({ rawtext: [{ translate: "forms.trick_menu.button.sit" }] }, buttonIcons.sit)
        .button({ rawtext: [{ translate: "forms.trick_menu.button.lie_down" }] }, buttonIcons.lie_down)
        .button({ rawtext: [{ translate: "forms.trick_menu.button.roll" }] }, buttonIcons.roll)
        .button({ rawtext: [{ translate: "forms.trick_menu.button.speak" }] }, buttonIcons.speak)
        .button({ rawtext: [{ translate: "forms.trick_menu.button.stand" }] }, buttonIcons.stand)
        .button({ rawtext: [{ translate: "forms.trick_menu.button.backflip" }] }, buttonIcons.backflip);

    dog.triggerEvent(
        "oct_dogs:interaction.wait_for_command"
    );

    form.show(player).then((response) => {
        if (response.canceled) {
            dog.triggerEvent("oct_dogs:interaction.free");
            return;
        }

        switch (response.selection) {
            case 0:
                dog.triggerEvent("oct_dogs:interaction.free");
                break;
            case 1:
                dog.triggerEvent("oct_dogs:interaction.sit");
                break;
            case 2:
                dog.triggerEvent("oct_dogs:interaction.lie_down");
                break;
            case 3:
                dog.triggerEvent("oct_dogs:interaction.roll");
                dog.dimension.playSound(sounds.roll, dog.location);
                break;
            case 4:
                dog.triggerEvent("oct_dogs:interaction.speak");
                break;
            case 5:
                dog.triggerEvent("oct_dogs:interaction.stand");
                dog.dimension.playSound(sounds.stand, dog.location);
                break;
            case 6:
                dog.triggerEvent("oct_dogs:interaction.backflip");
                dog.dimension.playSound(sounds.backflip, dog.location);
                break;
        }
    });
}

export function openGuidebookMenu(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "forms.guidebook.main.title" }] })
        .body({ rawtext: [{ translate: "forms.guidebook.main.body", with: ["\n"] }] })
        .divider()
        .button({ rawtext: [{ translate: "forms.guidebook.main.button.recipes" }] }, "textures/oct/dogs/forms/button_recipes")
        .button({ rawtext: [{ translate: "forms.guidebook.main.button.dogs" }] }, "textures/oct/dogs/forms/button_tutorial")
        .button({ rawtext: [{ translate: "forms.guidebook.main.button.help" }] }, "textures/oct/dogs/forms/button_help")
        .divider()
        .button({ rawtext: [{ translate: "forms.guidebook.button.exit" }] }, "textures/oct/dogs/forms/button_exit");

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

        switch (response.selection) {
            case 0:
                openRecipesMenu(player);
                break;
            case 1:
                openDogsMenu(player);
                break;
            case 2:
                openHelpMenu(player);
                break;
        }
    });
}

function openDogsMenu(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "forms.guidebook.dogs.title" }] })
        .label({ rawtext: [{ translate: "forms.guidebook.dogs.header_1", with: ["\n"] }] })
        .label({ rawtext: [{ translate: "forms.guidebook.dogs.body_1", with: ["\n"] }] })
        .divider()
        .label({ rawtext: [{ translate: "forms.guidebook.dogs.header_2", with: ["\n"] }] })
        .label({ rawtext: [{ translate: "forms.guidebook.dogs.body_2", with: ["\n"] }] })
        .divider()
        .label({ rawtext: [{ translate: "forms.guidebook.dogs.header_3", with: ["\n"] }] })
        .label({ rawtext: [{ translate: "forms.guidebook.dogs.body_3", with: ["\n"] }] })
        .divider()
        .label({ rawtext: [{ translate: "forms.guidebook.dogs.header_4", with: ["\n"] }] })
        .label({ rawtext: [{ translate: "forms.guidebook.dogs.body_4", with: ["\n"] }] })
        .divider()
        .label({ rawtext: [{ translate: "forms.guidebook.dogs.header_5", with: ["\n"] }] })
        .label({ rawtext: [{ translate: "forms.guidebook.dogs.body_5", with: ["\n"] }] })
        .divider()
        .button({ rawtext: [{ translate: "forms.guidebook.button.back" }] }, "textures/oct/dogs/forms/button_back")
        .button({ rawtext: [{ translate: "forms.guidebook.button.exit" }] }, "textures/oct/dogs/forms/button_exit");

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

        switch (response.selection) {
            case 0:
                openGuidebookMenu(player);
                break;
        }
    });
}
function openHelpMenu(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "forms.guidebook.help.title" }] })
        .label({ rawtext: [{ translate: "forms.guidebook.help.header_1", with: ["\n"] }] })
        .label({ rawtext: [{ translate: "forms.guidebook.help.body_1", with: ["\n"] }] })
        .divider()
        .label({ rawtext: [{ translate: "forms.guidebook.help.header_2", with: ["\n"] }] })
        .label({ rawtext: [{ translate: "forms.guidebook.help.body_2", with: ["\n"] }] })
        .divider()
        .button({ rawtext: [{ translate: "forms.guidebook.button.back" }] }, "textures/oct/dogs/forms/button_back")
        .button({ rawtext: [{ translate: "forms.guidebook.button.exit" }] }, "textures/oct/dogs/forms/button_exit");

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

        switch (response.selection) {
            case 0:
                openGuidebookMenu(player);
                break;
        }
    });
}

function openRecipesMenu(player) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: "forms.guidebook.recipes.title" }] })
        .button({ rawtext: [{ translate: "forms.guidebook.recipes.button.guidebook" }] }, "textures/oct/dogs/forms/recipe_guidebook")
        .divider()
        .button({ rawtext: [{ translate: "forms.guidebook.recipes.button.treat" }] }, "textures/oct/dogs/forms/recipe_treat")
        .button({ rawtext: [{ translate: "forms.guidebook.recipes.button.dog_food" }] }, "textures/oct/dogs/forms/recipe_dog_food")
        .divider()
        .button({ rawtext: [{ translate: "forms.guidebook.recipes.button.ball" }] }, "textures/oct/dogs/forms/recipe_ball")
        .button({ rawtext: [{ translate: "forms.guidebook.recipes.button.tennis_ball" }] }, "textures/oct/dogs/forms/recipe_tennis_ball")
        .button({ rawtext: [{ translate: "forms.guidebook.recipes.button.frisbee" }] }, "textures/oct/dogs/forms/recipe_frisbee")
        .button({ rawtext: [{ translate: "forms.guidebook.recipes.button.chewy_bone" }] }, "textures/oct/dogs/forms/recipe_chewy_bone")
        .divider()
        .button({ rawtext: [{ translate: "forms.guidebook.recipes.button.bowl" }] }, "textures/oct/dogs/forms/recipe_bowl")
        .button({ rawtext: [{ translate: "forms.guidebook.recipes.button.dyed_bowl" }] }, "textures/oct/dogs/forms/recipe_dyed_bowl")
        .divider()
        .button({ rawtext: [{ translate: "forms.guidebook.button.back" }] }, "textures/oct/dogs/forms/button_back")
        .button({ rawtext: [{ translate: "forms.guidebook.button.exit" }] }, "textures/oct/dogs/forms/button_exit");

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

        switch (response.selection) {
            case 0:
                openItemInfo(player, 'guidebook');
                break;
            case 1:
                openItemInfo(player, 'treat');
                break;
            case 2:
                openItemInfo(player, 'dog_food');
                break;
            case 3:
                openItemInfo(player, 'ball');
                break;
            case 4:
                openItemInfo(player, 'tennis_ball');
                break;
            case 5:
                openItemInfo(player, 'frisbee');
                break;
            case 6:
                openItemInfo(player, 'chewy_bone');
                break;
            case 7:
                openItemInfo(player, 'bowl');
                break;
            case 8:
                openItemInfo(player, 'dyed_bowl');
                break;
            case 9:
                openGuidebookMenu(player);
                break;
        }
    });
}

function openItemInfo(player, itemKey) {
    const form = new ui.ActionFormData()
        .title({ rawtext: [{ translate: `forms.guidebook.recipes.button.${itemKey}` }] })
        .body({ // item Name and amount
            rawtext: [{
                translate: `forms.guidebook.recipes.itemNameHeader`,
                with:
                {
                    rawtext: [
                        { text: "\n" },
                        { translate: `forms.guidebook.recipes.button.${itemKey}` },
                        { text: `${itemDict[itemKey].craftedAmount}` },
                    ]
                }
            }]
        })
        .divider()
        .label({ // item Info
            rawtext: [{
                translate: `forms.guidebook.recipes.itemInfoHeader`,
                with:
                {
                    rawtext: [
                        { text: "\n" },
                        { translate: `forms.guidebook.recipes.body.${itemKey}`, with: ["\n"] }
                    ]
                }
            }]
        })
        .divider()
        .label({ // item Recipe Header
            rawtext: [{
                translate: `${itemDict[itemKey].type === "shapeless" ?
                    "forms.guidebook.recipes.itemRecipeShapelessHeader" :
                    "forms.guidebook.recipes.itemRecipeHeader"}`,
                with: itemDict[itemKey].recipe
            }]
        })
        .divider()
        .button({ rawtext: [{ translate: "forms.guidebook.button.back" }] }, "textures/oct/dogs/forms/button_back")
        .button({ rawtext: [{ translate: "forms.guidebook.button.exit" }] }, "textures/oct/dogs/forms/button_exit");

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

        switch (response.selection) {
            case 0:
                openRecipesMenu(player);
                break;
        }
    });
}

const itemDict = {
    empty: {
        type: "shaped",
        craftedAmount: 1,
        recipe: {
            rawtext: [
                {
                    translate: `§8
[▒] [▒] [▒]
[▒] [§n█§8] [▒]
[▒] [▒] [▒]

§8[§n█§8] - §n%%1§r`,
                    with: {
                        rawtext: [
                            { translate: `item.oct_dogs:snack` },
                        ],
                    },
                },
            ]
        }
    },
    guidebook: {
        type: "shapeless",
        craftedAmount: 1,
        recipe: {
            rawtext: [
                {
                    translate: `§8
[▒] [§d█§8] [▒]
[▒] [§n█§8] [▒]
[▒] [▒] [▒]

§8[§n█§8] - §n%%1
§8[§d█§8] - §d%%2§r`,
                    with: {
                        rawtext: [
                            { translate: `item.book.name` },
                            { translate: `item.oct_dogs:snack` },
                        ],
                    },
                },
            ]
        }
    },
    treat: {
        type: "shapeless",
        craftedAmount: 16,
        recipe: {
            rawtext: [
                {
                    translate: `§8
[§n█§8] [§m█§8] [▒]
[§h█§8] [§g█§8] [▒]
[▒] [▒] [▒]

§8[§n█§8] - §n%%1
§8[§m█§8] - §m%%2
§8[§h█§8] - §h%%3
§8[§g█§8] - §g%%4§r`,
                    with: {
                        rawtext: [
                            { translate: `item.fish.name` },
                            { translate: `item.beef.name` },
                            { translate: `item.chicken.name` },
                            { translate: `item.wheat.name` },
                        ],
                    },
                },
            ]
        }
    },
    dog_food: {
        type: "shaped",
        craftedAmount: 8,
        recipe: {
            rawtext: [
                {
                    translate: `§8
[§n█§8] [§n█§8] [§n█§8]
[§m█§8] [§m█§8] [§m█§8]
[§g█§8] [§g█§8] [§g█§8]

§8[§n█§8] - §n%%1
§8[§m█§8] - §m%%2
§8[§g█§8] - §g%%3§r`,
                    with: {
                        rawtext: [
                            { translate: `item.oct_dogs:snack` },
                            { translate: `item.beef.name` },
                            { translate: `item.wheat.name` },
                        ],
                    },
                },
            ]
        }
    },
    ball: {
        type: "shaped",
        craftedAmount: 1,
        recipe: {
            rawtext: [
                {
                    translate: `§8
[▒] [§q█§8] [▒]
[§q█§8] [§n█§8] [§q█§8]
[▒] [§q█§8] [▒]

§8[§n█§8] - §n%%1
§8[§q█§8] - §q%%2§r`,
                    with: {
                        rawtext: [
                            { translate: `item.oct_dogs:snack` },
                            { translate: `item.slime_ball.name` },
                        ],
                    },
                },
            ]
        }
    },
    tennis_ball: {
        type: "shaped",
        craftedAmount: 1,
        recipe: {
            rawtext: [
                {
                    translate: `§8
[▒] [§i█§8] [▒]
[§i█§8] [§n█§8] [§i█§8]
[▒] [§i█§8] [▒]

§8[§n█§8] - §n%%1
§8[§i█§8] - §i%%2§r`,
                    with: {
                        rawtext: [
                            { translate: `item.oct_dogs:snack` },
                            { translate: `item.string.name` },
                        ],
                    },
                },
            ]
        }
    },
    frisbee: {
        type: "shaped",
        craftedAmount: 1,
        recipe: {
            rawtext: [
                {
                    translate: `§8
[▒] [§9█§8] [▒]
[§9█§8] [§n█§8] [§9█§8]
[▒] [§9█§8] [▒]

§8[§n█§8] - §n%%1
§8[§9█§8] - §9%%2§r`,
                    with: {
                        rawtext: [
                            { translate: `item.oct_dogs:snack` },
                            { translate: `item.clay_ball.name` },
                        ],
                    },
                },
            ]
        }
    },
    chewy_bone: {
        type: "shaped",
        craftedAmount: 1,
        recipe: {
            rawtext: [
                {
                    translate: `§8
[▒] [§i█§8] [▒]
[§i█§8] [§n█§8] [§i█§8]
[▒] [§i█§8] [▒]

§8[§n█§8] - §n%%1
§8[§i█§8] - §i%%2§r`,
                    with: {
                        rawtext: [
                            { translate: `item.oct_dogs:snack` },
                            { translate: `item.bone.name` },
                        ],
                    },
                },
            ]
        }
    },
    bowl: {
        type: "shaped",
        craftedAmount: 1,
        recipe: {
            rawtext: [
                {
                    translate: `§8
[▒] [§d█§8] [▒]
[§i█§8] [§n█§8] [§i█§8]
[▒] [§i█§8] [▒]

§8[§d█§8] - §d%%1
§8[§i█§8] - §i%%2
§8[§n█§8] - §n%%3§r`,
                    with: {
                        rawtext: [
                            { translate: `item.oct_dogs:snack` },
                            { translate: `item.iron_nugget.name` },
                            { translate: `item.bowl.name` },
                        ],
                    },
                },
            ]
        }
    },
    dyed_bowl: {
        type: "shapeless",
        craftedAmount: 1,
        recipe: {
            rawtext: [
                {
                    translate: `§8
[▒] [§d█§8] [▒]
[▒] [§n█§8] [▒]
[▒] [▒] [▒]

§8[§d█§8] - §d%%1
§8[§n█§8] - §n%%2§r`,
                    with: {
                        rawtext: [
                            { translate: `forms.guidebook.recipes.dyeAny` },
                            { translate: `item.oct_dogs:bowl` },
                        ],
                    },
                },
            ]
        }
    }
}