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

const MAX_SECTIONS = 3;

export const herbBlocks = [
    {
        cropID: "sgs_farm:thyme_crop",
        seedID: "sgs_farm:thyme_seed",
        state: "THYME"
    },
    {
        cropID: "sgs_farm:rosemary_crop",
        seedID: "sgs_farm:rosemary_seed",
        state: "ROSEMARY"
    },
    {
        cropID: "sgs_farm:lavender_crop",
        seedID: "sgs_farm:lavender_seed",
        state: "LAVENDER"
    },
    {
        cropID: "sgs_farm:basil_crop",
        seedID: "sgs_farm:basil_seed",
        state: "BASIL"
    },
    {
        cropID: "sgs_farm:mint_crop",
        seedID: "sgs_farm:mint_seed",
        state: "MINT"
    },
    {
        cropID: "sgs_farm:cilantro_crop",
        seedID: "sgs_farm:cilantro_seed",
        state: "CILANTRO"
    }
]

export class herbPotManager {
    static tick(block) {
        const herb = block.permutation.getState("sgs_farm:herb");
        if (herb == "NONE") return;

        const stage = block.permutation.getState("sgs_farm:growth");
        let setStage = stage + 1;
        if (setStage > 7) setStage = 7;
        block.setPermutation(block.permutation.withState("sgs_farm:growth", setStage));
    }

    static interact(block, player) {

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

        const herbData = herbBlocks.find((f) => f.seedID == heldItem.typeId);
        const potSections = block.permutation.getState("sgs_farm:pot_sections");
        const herb = block.permutation.getState("sgs_farm:herb");
        const stage = block.permutation.getState("sgs_farm:growth");

        //add pots to the pot pile
        if (heldItem != undefined && potSections != MAX_SECTIONS && heldItem.typeId == "sgs_farm:herb_pot_item") {
            this.pot(block, player, mainhand, heldItem, potSections);
            return;
        }

        //plant herb in pot and reset growth state to 0
        if (herbData != undefined) {
            block.setPermutation(block.permutation.withState("sgs_farm:herb", herbData.state));
            block.setPermutation(block.permutation.withState("sgs_farm:growth", 0));
            block.dimension.playSound("block.sweet_berry_bush.place", block.center());

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

        //check herb situation
        if (herb != "NONE") {
            //can sickle if fully grown, drop 1 herb per pot
            if (stage >= 7) {
                if (heldItem != undefined && heldItem.typeId == "sgs_farm:sickle_iron") {
                    const itemType = herbBlocks.find((f) => f.state == herb);
                    const dropItem = new mc.ItemStack(itemType.cropID, potSections + 1);
                    const { x, y, z } = block.location;
                    block.dimension.playSound("mob.sheep.shear", block.center());
                    block.dimension.spawnItem(dropItem, { x: x + 0.5, y: y + 0.5, z: z + 0.5 });
                    block.setPermutation(block.permutation.withState("sgs_farm:growth", 0));
                    return;
                };
            }
            //can bonemeal if not fully grown
            else {
                if (heldItem != undefined && heldItem.typeId == "minecraft:bone_meal") {
                    this.bonemeal(block, player, mainhand, heldItem, stage);
                    return;
                }
            }
        }
    }

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

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

        //drop pots
        const potSections = permutation.getState("sgs_farm:pot_sections");
        let dropItem = new mc.ItemStack("sgs_farm:herb_pot_item", potSections + 1);
        const { x, y, z } = block.location;
        block.dimension.spawnItem(dropItem, { x: x + 0.5, y: y + 0.5, z: z + 0.5 });

        //drop herbs if fully grown
        const herb = permutation.getState("sgs_farm:herb");
        if (herb == "NONE") return;
        const stage = permutation.getState("sgs_farm:growth");
        if (stage < 7) return;
        const itemType = herbBlocks.find((f) => f.state == herb);
        dropItem = new mc.ItemStack(itemType.cropID, potSections + 1);
        block.dimension.spawnItem(dropItem, { x: x + 0.5, y: y + 0.5, z: z + 0.5 });
    }

    //interact with bone meal
    static bonemeal(block, player, mainhand, heldItem, stage) {
        let setStage = stage + 2;
        if (setStage > 7) setStage = 7;
        try {
            block.dimension.spawnParticle("minecraft:crop_growth_emitter", block.center());
        } catch { }
        block.dimension.playSound("item.bone_meal.use", block.center());
        block.setPermutation(block.permutation.withState("sgs_farm:growth", setStage));
        if (player.getGameMode() == GameMode.creative) return;
        mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
    }

    static pot(block, player, mainhand, heldItem, pot_sections) {
        let setsections = pot_sections + 1;
        if (setsections > MAX_SECTIONS) setsections = MAX_SECTIONS;

        block.dimension.playSound("place.tuff", block.center());

        block.setPermutation(block.permutation.withState("sgs_farm:pot_sections", setsections));
        if (player.getGameMode() == GameMode.creative) return;
        mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
    }

    static getGrowthStage(block, cropData) {
        //return the block's growth stage
        return block.permutation.getState(cropData.stateID);
    }
}
