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("sunflowerbook.name")
        .body("sunflowerbook.body")
        .button("sunflowerbook.introduction", "textures/ui/introduction_for_ui")
        //.button("farmersbook.howToPlantAddonCrops", "textures/ui/farmland_for_ui")
        .button("sunflowerbook.whatYouCanToPutInTheComposter", "textures/ui/composter_for_ui")
        .button("sunflowerbook.image", "textures/ui/wearable_for_ui")
        .button("sunflowerbook.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:
                whatYouCanToPutInTheComposter(player)
                break;
            case 2:
                image(player)
                    break;
            case 3:
                exit(player)
                break;
        }
    })
}
function introduction(player) {
    const introduction = new ActionFormData()
        .title("§dSunlower Delight Bedrock")
        .body({ "rawtext": [{ "translate": "sunflowersdelight.welcome0" }, { "text": "\n" }, { "translate": "sunflowersdelight.welcome1" }, { "text": "\n" }, { "translate": "sunflowersdelight.welcome2" }, { "text": "\n" }, { "translate": "sunflowersdelight.welcome3" }] })
        .button("accessibility.button.back", "textures/ui/exit");
    introduction.show(player).then(response => {
        if (!response.canceled) {
            if (response.selection == 0) {
                showBook(player)
            }
        }
    })
}

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

function image(player) {
    const image = new ActionFormData()
        .title("sunflowerbook.image")
        .body("sunflowerbook.imageBody")
        .button("accessibility.button.back", "textures/ui/exit");
        image.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 === "sunflowerdelight:book") {
            const locations = loadPlayerLocations(source); 
            showBook(source, locations); 
        }
    });