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

// Функция для проверки наличия Farmer's Delight
function checkFarmersDelight() {
    try {
        // Используем ItemTypes.get() как в примере Farmer's Delight
        return ItemTypes.get("farmersdelight:farmers_book") ? true : false;
    } catch (e) {
        return false;
    }
}

/**
* @param {server.Entity} player
*/
function showBook(player) {
    // Проверяем наличие модов при каждом открытии книги
    const hasFarmersDelight = checkFarmersDelight();
    
    // Формируем body с информацией о совместимости
    const bodyRawtext = [
        { "translate": "largebook.body" },
        { "text": "\n\n" },
        { "translate": "largebook.compatibility" },
        { "text": ":\n" },
        { "translate": "Farmer's Delight" },
        { "text": ": " }
    ];
    
    // Добавляем цвет и статус для Farmer's Delight
    if (hasFarmersDelight) {
        bodyRawtext.push({ "text": "§a" }, { "translate": "largebook.available" });
    } else {
        bodyRawtext.push({ "text": "§c" }, { "translate": "largebook.notFound" });
    }
    
    bodyRawtext.push({ "text": "\n\n" });
    
    const showBook = new ActionFormData()
        .title("largebook.name")
        .body({ "rawtext": bodyRawtext })
        .button("largebook.introduction", "textures/ui/introduction_for_ui")
        //.button("farmersbook.howToPlantAddonCrops", "textures/ui/farmland_for_ui")
        .button("largebook.yummyYummy", "textures/ui/yummy_for_ui")
        .button("largebook.howToGetMuttonRack", "textures/ui/rack_for_ui")
        .button("largebook.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:
                yummyYummy(player)
                break;
            case 2:
                howToGetMuttonRack(player)
                    break;
            case 3:
                exit(player)
                break;
        }
    })
}
function introduction(player) {
    const introduction = new ActionFormData()
        .title("§eLarge Meals Bedrock")
        .body({ "rawtext": [{ "translate": "largemeals.welcome0" }, { "text": "\n" }, { "translate": "largemeals.welcome1" }, { "text": "\n" }, { "translate": "largemeals.welcome2" }, { "text": "\n" }, { "translate": "largemeals.welcome3" }] })
        .button("accessibility.button.back", "textures/ui/exit");
    introduction.show(player).then(response => {
        if (!response.canceled) {
            if (response.selection == 0) {
                showBook(player)
            }
        }
    })
}

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

function howToGetMuttonRack(player) {
    const howToGetMuttonRack = new ActionFormData()
        .title("largebook.howToGetMuttonRack")
        .body("largebook.howToGetMuttonRackBody")
        .button("accessibility.button.back", "textures/ui/exit");
        howToGetMuttonRack.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 === "largemeals:book") {
            showBook(source); 
        }
    });