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

function loadPlayerLocations(player) {
    const savedLocations = player.getDynamicProperty('savedLocations');
    if (savedLocations) {
        return JSON.parse(savedLocations); 
    }
    return []; 
}

function savePlayerLocations(player, locations) {
    player.setDynamicProperty('savedLocations', JSON.stringify(locations)); 
}

/**
* @param {server.Entity} player
*/
function showBook(player) {
    const showBook = new ActionFormData()
        .title("crabbersbook.name")
        .body("crabbersbook.body")
        .button("crabbersbook.introduction", "textures/ui/introduction_for_ui")
        //.button("farmersbook.howToPlantAddonCrops", "textures/ui/farmland_for_ui")
        .button("crabbersbook.howToUseCrabTrap", "textures/ui/crab_trap_for_ui")
        .button("crabbersbook.newPotionandTheirTypes", "textures/ui/potions_for_ui")
        .button("crabbersbook.newTrades", "textures/ui/trades_for_ui")
        .button("crabbersbook.allAboutCrabs", "textures/ui/crabs_for_ui")
        .button("crabbersbook.howToGetSpecialDelicacies", "textures/ui/food_for_ui")
        .button("crabbersbook.exit", "textures/ui/exit")
        
    showBook.show(player).then(response => {
        if (response.canceled) return;

        switch (response.selection) {
            case 0:
                introduction(player)
                break
            // case 1:
            //     howToPlantAddonCrops(player)
            //     break;
            case 1:
                howToUseCrabTrap(player)
                break;
            case 2:
                newPotionandTheirTypes(player)
                break;
            case 3:
                newTrades(player)
                break;
            case 4:
                allAboutCrabs(player)
                break;
            case 5:
                howToGetSpecialDelicacies(player)
            case 6:
                exit(player)
                break;
        }
    })
}
function introduction(player) {
    const introduction = new ActionFormData()
        .title("§9Crabber's Delight Bedrock")
        .body({ "rawtext": [{ "translate": "crabbersdelight.welcome0" }, { "text": "\n" }, { "translate": "crabbersdelight.welcome1" }, { "text": "\n" }, { "translate": "crabbersdelight.welcome2" }, { "text": "\n" }, { "translate": "crabbersdelight.welcome3" }] })
        .button("accessibility.button.back", "textures/ui/exit");
    introduction.show(player).then(response => {
        if (!response.canceled) {
            if (response.selection == 0) {
                showBook(player)
            }
        }
    })
}

function howToUseCrabTrap(player) {
    const howToUseCrabTrap = new ActionFormData()
        .title("crabbersbook.howtousecrabtrap")
        .body("crabbersbook.howtousecrabtraptext")
        .button("accessibility.button.back", "textures/ui/exit");
        howToUseCrabTrap.show(player).then(response => {
        if (!response.canceled) {
            if (response.selection == 0) {
                showBook(player)
            }
        }
    })
}

function newPotionandTheirTypes(player) {
    const newPotionandTheirTypes = new ActionFormData()
        .title("crabbersbook.newPotionandTheirTypes")
        .body("crabbersbook.newPotionandTheirTypesBody")
        .button("accessibility.button.back", "textures/ui/exit");
        newPotionandTheirTypes.show(player).then(response => {
        if (!response.canceled) {
            if (response.selection == 0) {
                showBook(player)
            }
        }
    })
}

function newTrades(player) {
    const newTrades = new ActionFormData()
        .title("crabbersbook.newTrades")
        .body("crabbersbook.newTradesBody")
        .button("accessibility.button.back", "textures/ui/exit");
    newTrades.show(player).then(response => {
        if (!response.canceled) {
            if (response.selection == 0) {
                showBook(player)
            }
        }
    })
}

function allAboutCrabs(player) {
    const allAboutCrabs = new ActionFormData()
        .title("crabbersbook.allAboutCrabs")
        .body("crabbersbook.allAboutCrabsBody")
        .button("accessibility.button.back", "textures/ui/exit");
    allAboutCrabs.show(player).then(response => {
        if (!response.canceled) {
            if (response.selection == 0) {
                showBook(player)
            }
        }
    })
}

function howToGetSpecialDelicacies(player) {
    const howToGetSpecialDelicacies = new ActionFormData()
        .title("crabbersbook.howToGetSpecialDelicacies")
        .body("crabbersbook.howToGetSpecialDelicaciesBody")
        .button("accessibility.button.back", "textures/ui/exit");
        howToGetSpecialDelicacies.show(player).then(response => {
        if (!response.canceled) {
            if (response.selection == 0) {
                showBook(player)
            }
        }
    })
}

    world.afterEvents.itemUse.subscribe((event) => {
        const { source, itemStack } = event;
        if (itemStack.typeId === "crabbersdelight:book") {
            const locations = loadPlayerLocations(source); 
            showBook(source, locations); 
        }
    });