import { ItemStack, system, world } from "@minecraft/server";
import {ActionFormData } from "@minecraft/server-ui"
  

system.beforeEvents.startup.subscribe(eventData => {
    eventData.itemComponentRegistry.registerCustomComponent("rustic:open_almanac", {
        onUse: e => {
            const player = e.source;
            openRusticAlmanac(player);
        }
    });
});

export function openFormWithNewInfo(firstPageText,secondPageText,firstImage,secondImage,player){
    player.playSound("item.book.page_turn")
    const equipment = player.getComponent('equippable');
    const infoUi = new ActionFormData()
        .title("book.rustic.entry.info")
        .button(firstPageText, firstImage)
        .button(secondPageText, secondImage)
    infoUi.show(player).then(response => {
        if (response.canceled) {
            equipment.setEquipment('Mainhand', new ItemStack("rustic:almanac",1));
            return;
        };
        let selection = response.selection;
        switch (selection){
            case 0:
                player.playSound("item.book.page_turn")
                openRusticAlmanac(player)
        }
    });
}

export function openRusticAlmanac(player){
    const equipment = player.getComponent('equippable');
    equipment.setEquipment('Mainhand', new ItemStack("rustic:almanac_open",1));

    const customUi = new ActionFormData()
    .title("book.rustic.entry.almanac")
    //.body(firstPageText + secondPageText + thirdPageText + fourthPageText)
    .button("book.rustic.entry.bees", "textures/items/rustic/bee")
    .button("book.rustic.entry.fertile_soil", "textures/ui/almanac_blocks/fertile_farmland")
    .button("book.rustic.entry.crop_stakes", "textures/ui/almanac_blocks/wooden_stake")
    .button("book.rustic.entry.rope", "textures/items/rustic/rope")
    .button("book.rustic.entry.tomatoes", "textures/items/rustic/tomato")
    .button("book.rustic.entry.chili_peppers", "textures/items/rustic/chili_pepper")
    .button("book.rustic.entry.wildberries", "textures/items/rustic/wildberries")
    .button("book.rustic.entry.grapes", "textures/items/rustic/grapes")
    .button("book.rustic.entry.apple_trees", "textures/items/apple")
    .button("book.rustic.entry.olive_trees", "textures/items/rustic/olives")
    .button("book.rustic.entry.ironwood_trees", "textures/items/rustic/ironberries")
    .button("book.rustic.entry.herbs", "textures/items/rustic/marsh_mallow")

    .button("book.rustic.entry.pots", "textures/ui/almanac_blocks/vase")
    .button("book.rustic.entry.barrels", "textures/ui/almanac_blocks/barrel")
    .button("book.rustic.entry.cabinets", "textures/ui/almanac_blocks/cabinet")
    .button("book.rustic.entry.chainandchandeliers", "textures/ui/almanac_blocks/iron_chandelier_chain")
    .button("book.rustic.entry.candles", "textures/ui/almanac_items/iron_candle")
    .button("book.rustic.entry.lanterns", "textures/ui/almanac_blocks/iron_lantern")
    .button("book.rustic.entry.lattice", "textures/ui/almanac_blocks/iron_lattice")
    .button("book.rustic.entry.tablesandchairs", "textures/ui/almanac_blocks/rustic_table")
    .button("book.rustic.entry.gargoyles", "textures/ui/almanac_blocks/gargoyle")
    .button("book.rustic.entry.slate", "textures/ui/almanac_blocks/slate")
    .button("book.rustic.entry.pillars", "textures/ui/almanac_blocks/pillars")
    .button("book.rustic.entry.clay_walls", "textures/ui/almanac_blocks/clay_wall")
    .button("book.rustic.entry.painted_wood", "textures/ui/almanac_blocks/painted_plank")

    .button("book.rustic.entry.alchemy", "textures/ui/almanac_blocks/condenser")
    .button("book.rustic.entry.elixirs", "textures/ui/almanac_items/elixir_bottle")
    .button("book.rustic.entry.brewing", "textures/ui/almanac_blocks/brewing_barrel")
    .button("book.rustic.entry.alcoholic_beverages", "textures/items/rustic/ale")
    .button("book.rustic.entry.crushing", "textures/ui/almanac_blocks/crushing_tub")
    .button("book.rustic.entry.drying", "textures/ui/almanac_blocks/drying_basin");

    customUi.show(player).then(response => {
        if (response.canceled) {
            equipment.setEquipment('Mainhand', new ItemStack("rustic:almanac",1));
            return;
        };

        const data = AlmanacData[response.selection] || {};
        
        let firstPageText = data.firstPageText || "None";
        let secondPageText = data.secondPageText || "book.rustic.text.noadditional";
        let firstImage = data.firstImage || "textures/ui/almanac_photos/notfound";
        let secondImage = data.secondImage || "textures/ui/almanac_photos/notfound";

        openFormWithNewInfo(firstPageText,secondPageText,firstImage,secondImage,player)
    });
}

export const AlmanacData = {
    0: {
        firstPageText: "book.rustic.text.bees",
        secondPageText: "book.rustic.text.bees2",
        firstImage: "textures/ui/almanac_photos/Agriculture/BeeImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/BeeImage2"
    },
    1: {
        firstPageText: "book.rustic.text.fertile_soil",
        firstImage: "textures/ui/almanac_photos/Agriculture/FertileSoilImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/FertileSoilImage2"
    },
    2: {
        firstPageText: "book.rustic.text.crop_stakes",
        firstImage: "textures/ui/almanac_photos/Agriculture/CropStakeImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/CropStakeImage2"
    },
    3: {
        firstPageText: "book.rustic.text.rope",
        firstImage: "textures/ui/almanac_photos/Agriculture/RopeImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/RopeImage2"
    },
    4: {
        firstPageText: "book.rustic.text.tomatoes",
        secondPageText: "book.rustic.text.tomatoes2",
        firstImage: "textures/ui/almanac_photos/Agriculture/TomatoImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/TomatoImage2"
    },
    5: {
        firstPageText: "book.rustic.text.chili_peppers",
        secondPageText: "book.rustic.text.chili_peppers2",
        firstImage: "textures/ui/almanac_photos/Agriculture/ChiliImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/ChiliImage2"
    },
    6: {
        firstPageText: "book.rustic.text.wildberries",
        secondPageText: "book.rustic.text.wildberries2",
        firstImage: "textures/ui/almanac_photos/Agriculture/WildberrieImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/WildberrieImage2"
    },
    7: {
        firstPageText: "book.rustic.text.grapes",
        secondPageText: "book.rustic.text.grapes2",
        firstImage: "textures/ui/almanac_photos/Agriculture/GrapeImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/GrapeImage2"
    },
    8: {
        firstPageText: "book.rustic.text.apple_trees",
        secondPageText: "book.rustic.text.apple_trees2",
        firstImage: "textures/ui/almanac_photos/Agriculture/AppleTreeImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/AppleTreeImage2"
    },
    9: {
        firstPageText: "book.rustic.text.olive_trees",
        secondPageText: "book.rustic.text.olive_trees2",
        firstImage: "textures/ui/almanac_photos/Agriculture/OliveImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/OliveImage2"
    },
    10: {
        firstPageText: "book.rustic.text.ironwood_trees",
        secondPageText: "book.rustic.text.ironwood_trees2",
        firstImage: "textures/ui/almanac_photos/Agriculture/IronwoodImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/IronwoodImage2"
    },
    11: {
        firstPageText: "book.rustic.text.herbs",
        secondPageText: "book.rustic.text.herbs2",
        firstImage: "textures/ui/almanac_photos/Agriculture/HerbsImage1",
        secondImage: "textures/ui/almanac_photos/Agriculture/HerbsImage2"
    },
    //Decoration
    12: {
        firstPageText: "book.rustic.text.pots",
        firstImage: "textures/ui/almanac_photos/Decoration/PotsImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/PotsImage2"
    },
    13: {
        firstPageText: "book.rustic.text.barrels",
        firstImage: "textures/ui/almanac_photos/Decoration/BarrelsImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/BarrelsImage2"
    },
    14: {
        firstPageText: "book.rustic.text.cabinets",
        firstImage: "textures/ui/almanac_photos/Decoration/CabinetImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/CabinetImage2"
    },
    15: {
        firstPageText: "book.rustic.text.chains&chandeliers",
        firstImage: "textures/ui/almanac_photos/Decoration/ChainsImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/ChandelierImage2"
    },
    16: {
        firstPageText: "book.rustic.text.candles",
        firstImage: "textures/ui/almanac_photos/Decoration/CandleImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/PaintedWoodImage2"
    },
    17: {
        firstPageText: "book.rustic.text.lanterns",
        firstImage: "textures/ui/almanac_photos/Decoration/LanternsImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/LanternsImage2"
    },
    18: {
        firstPageText: "book.rustic.text.lattice",
        firstImage: "textures/ui/almanac_photos/Decoration/LatticeImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/LatticeImage2"
    },
    19: {
        firstPageText: "book.rustic.text.tables&chairs",
        firstImage: "textures/ui/almanac_photos/Decoration/TableImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/ChairImage2"
    },
    20: {
        firstPageText: "book.rustic.text.gargoyles",
        firstImage: "textures/ui/almanac_photos/Decoration/GargoyleImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/GargoyleImage2"
    },
    21: {
        firstPageText: "book.rustic.text.slate",
        firstImage: "textures/ui/almanac_photos/Decoration/SlateImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/SlateImage2"
    },
    22: {
        firstPageText: "book.rustic.text.pillars",
        firstImage: "textures/ui/almanac_photos/Decoration/PillarsImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/PillarsImage2"
    },
    23: {
        firstPageText: "book.rustic.text.clay_walls",
        firstImage: "textures/ui/almanac_photos/Decoration/ClayWallImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/ClayWallImage2"
    },
    24: {
        firstPageText: "book.rustic.text.painted_wood",
        firstImage: "textures/ui/almanac_photos/Decoration/PaintedWoodImage1",
        secondImage: "textures/ui/almanac_photos/Decoration/PaintedWoodImage2"
    },
    //Production
    25: {
        firstPageText: "book.rustic.text.elixirs",
        firstImage: "textures/ui/almanac_photos/notfound",
        secondImage: "textures/ui/almanac_photos/notfound"
    },
    26: {
        firstPageText: "book.rustic.text.alchemy",
        secondPageText: "book.rustic.text.alchemy_advanced",
        firstImage: "textures/ui/almanac_photos/notfound",
        secondImage: "textures/ui/almanac_photos/notfound"
    },
    27: {
        firstPageText: "book.rustic.text.brewing",
        secondPageText: "book.rustic.text.brewing2",
        firstImage: "textures/ui/almanac_photos/Production/BrewingImage1",
        secondImage: "textures/ui/almanac_photos/Production/BrewingImage2"
    },
    28: {
        firstPageText: "book.rustic.text.alcoholic_beverages",
        secondPageText: "book.rustic.text.alcoholic_beverages2",
        firstImage: "textures/ui/almanac_photos/Production/BeveragesImage2",
        secondImage: "textures/ui/almanac_photos/Production/BrewingImage2"
    },
    29: {
        firstPageText: "book.rustic.text.crushing",
        firstImage: "textures/ui/almanac_photos/Production/CrushingImage1",
        secondImage: "textures/ui/almanac_photos/Production/CrushingImage2"
    },
    30: {
        firstPageText: "book.rustic.text.drying",
        secondPageText: "book.rustic.text.drying2",
        firstImage: "textures/ui/almanac_photos/Production/DryingBasinImage1",
        secondImage: "textures/ui/almanac_photos/Production/DryingBasinImage2"
    }
};