import { EquipmentSlot, EntityEquippableComponent, GameMode } from '@minecraft/server';
import { itemManager } from '../sgs_farm_lib';
import * as mc from "@minecraft/server";

const MAX_SECTIONS = 3;

const jarRack = [
    "empty",
    "jar",
    "jam_strawberry",
    "jam_raspberry",
    "jam_blackberry",
    "jam_blueberry",
    "jam_gooseberry",
    "pesto",
    "spread_peanut",
    "spread_chocolate",
    "sauce_tomato",
    "sauce_bolognese"
]

const bottleRack = [
    "empty",
    "bottle",
    "juice_grape_red",
    "juice_grape_green",
    "juice_orange",
    "juice_lemon",
    "juice_lime",
    "juice_apple",
    "juice_pear",
    "juice_peach",
    "juice_cherry",
    "juice_passionfruit",
    "juice_pineapple",
    "juice_redcurrant",
    "juice_blackcurrant"
]

export class glassRackManager {
    
    static interact(block, player) {

        const mainhand = player.getComponent(EntityEquippableComponent.componentId).getEquipmentSlot(EquipmentSlot.Mainhand);
        const heldItem = mainhand.getItem();
        if (heldItem == undefined) return;

        let sectionTypes = jarRack;
        if (block.type.id == "sgs_farm:bottle_rack") sectionTypes = bottleRack;

        if(!sectionTypes.includes(heldItem.typeId.substring(9))) return;
        const sections = block.permutation.getState("sgs_farm:rack_sections");
        const type = block.permutation.getState("sgs_farm:type");

        //no type added, set the type with first item
        if(type == 0)
        {
            block.setPermutation(block.permutation.withState("sgs_farm:type", sectionTypes.indexOf(heldItem.typeId.substring(9))));
            block.dimension.playSound("block.decorated_pot.insert", block.center());

            if (player.getGameMode() == GameMode.creative) return;
            mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
            return;
        }

        //add more of the same type to the rack
        if(sections < MAX_SECTIONS)
        {
            if(type != sectionTypes.indexOf(heldItem.typeId.substring(9))) return;
            block.setPermutation(block.permutation.withState("sgs_farm:rack_sections", sections + 1));
            block.dimension.playSound("block.decorated_pot.insert", block.center());

            if (player.getGameMode() == GameMode.creative) return;
            mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
            return;
        }
    }

    static destroy(data) {
        this.breakblock(data.block, data.destroyedBlockPermutation);
    }

    static breakblock(block, permutation) {
        block.dimension.playSound("break.decorated_pot", block.center());

        //drop rack contents
        const sections = permutation.getState("sgs_farm:rack_sections");
        const type = permutation.getState("sgs_farm:type");
        if(type == 0) return;   //rack is empty

        let sectionTypes = jarRack;
        if (permutation.type.id == "sgs_farm:bottle_rack") sectionTypes = bottleRack;

        const { x, y, z } = block.location;
        const dropItem = new mc.ItemStack("sgs_farm:" + sectionTypes[type], sections + 1);
        block.dimension.spawnItem(dropItem, { x: x + 0.5, y: y + 0.5, z: z + 0.5 });
    }
}
