import { world, ItemStack, system, GameMode } from '@minecraft/server';
import { updateBlock } from '../utils/block';
import { consumeEquipped } from '../utils/item';

const Utils = {
    getPlayerEquipment(player) {
        return player.getComponent('equippable');
    },
    getSelectedItem(player) {
        const equip = this.getPlayerEquipment(player);
        return equip ? equip.getEquipment('Mainhand') : null;
    },
    consumeItem(player, item) {
        if (player.getGameMode() === GameMode.creative) return;
        const equip = this.getPlayerEquipment(player);
        consumeEquipped(equip, item);
    },
    setBlockState(block, stateName, value) {
        updateBlock(block, { [stateName]: value });
    },
    giveItem(player, id) {
        player.getComponent("inventory").container.addItem(new ItemStack(id));
    },
    playSound(block, sound) {
        block.dimension.playSound(sound, block.location);
    },
    spawnSmoke(block) {
        const loc = block.location;
        block.dimension.spawnParticle("minecraft:basic_smoke_particle", { x: loc.x + 0.5, y: loc.y + 1, z: loc.z + 0.5 });
    }
};

const TIN_MAP = {
  "tin_blueberry": 1,
  "tin_cheese_and_guava": 2,
  "tin_chocolate": 3,
  "tin_chocomint": 4,
  "tin_cookies_and_cream": 5,
  "tin_grape": 6,
  "tin_guava": 7,
  "tin_mango": 8,
  "tin_mint": 9,
  "tin_neapolitan": 10,
  "tin_passion_fruit": 11,
  "tin_peach": 12,
  "tin_strawberry": 13,
  "tin_vanilla": 14,
  "tin_watermelon": 15
};

const POPSICLES = [
  "peach:popsicle_blueberry",
  "peach:popsicle_cheese_and_guava",
  "peach:popsicle_chocolate",
  "peach:popsicle_chocomint",
  "peach:popsicle_cookies_and_cream",
  "peach:popsicle_grape",
  "peach:popsicle_guava",
  "peach:popsicle_mango",
  "peach:popsicle_mint",
  "peach:popsicle_neapolitan",
  "peach:popsicle_passion_fruit",
  "peach:popsicle_peach",
  "peach:popsicle_strawberry",
  "peach:popsicle_vanilla",
  "peach:popsicle_watermelon"
];

const SORBETS = [
  "peach:sorbet_blueberry",
  "peach:sorbet_cheese_and_guava",
  "peach:sorbet_chocolate",
  "peach:sorbet_chocomint",
  "peach:sorbet_cookies_and_cream",
  "peach:sorbet_grape",
  "peach:sorbet_guava",
  "peach:sorbet_mango",
  "peach:sorbet_mint",
  "peach:sorbet_neapolitan",
  "peach:sorbet_passion_fruit",
  "peach:sorbet_peach",
  "peach:sorbet_strawberry",
  "peach:sorbet_vanilla",
  "peach:sorbet_watermelon"
];

world.beforeEvents.worldInitialize.subscribe(pch => {
    pch.blockComponentRegistry.registerCustomComponent('peach:freezer_logic', {
        onPlayerInteract: e => {
            const { player, block } = e;
            const selected = Utils.getSelectedItem(player);
            const selectedShort = selected && selected.typeId ? (selected.typeId.includes(':') ? selected.typeId.split(':')[1] : selected.typeId) : null;

            let flavors = parseInt(block.permutation.getState("peach:freezer_flavors") || "0", 10);
            let stack = parseInt(block.permutation.getState("peach:freezer_stack") || "0", 10);

            if (flavors === 0 && selectedShort && TIN_MAP[selectedShort]) {
                const flavorNumber = TIN_MAP[selectedShort];
                Utils.consumeItem(player, selected);
                Utils.setBlockState(block, "peach:freezer_flavors", String(flavorNumber));
                Utils.setBlockState(block, "peach:freezer_stack", "7");
                Utils.giveItem(player, "peach:tin");
                Utils.playSound(block, "random.pop");
                return;
            }

            if (flavors > 0) {
                if (selectedShort === "stick") {
                    const giveId = POPSICLES[flavors - 1];
                    if (giveId) {
                        Utils.giveItem(player, giveId);
                        stack = Math.max(0, stack - 1);
                        Utils.setBlockState(block, "peach:freezer_stack", String(stack));
                        Utils.playSound(block, "random.pop");
                        if (stack === 0) Utils.setBlockState(block, "peach:freezer_flavors", "0");
                        return;
                    }
                }

                if (selectedShort === "sorbet_cone") {
                    const giveId = SORBETS[flavors - 1];
                    if (giveId) {
                        Utils.giveItem(player, giveId);
                        stack = Math.max(0, stack - 1);
                        Utils.setBlockState(block, "peach:freezer_stack", String(stack));
                        Utils.playSound(block, "random.pop");
                        if (stack === 0) Utils.setBlockState(block, "peach:freezer_flavors", "0");
                        return;
                    }
                }
            }
        }
    });
});
