import * as mc from "@minecraft/server";
import * as ui from "@minecraft/server-ui";
import { randomNum, randomWholeNum } from "../sgs_farm_lib";
import { animalList } from "../u3/sgs_farm_animalconfig";

const SOUND_TURN_PAGE = "item.book.page_turn";
const SOUND_OPTIONS = { pitch: 1.0, volume: 5.0, };

export const vanillaAnimalList = [
    {
        ID: "minecraft:cow",
        transforms: [
            "sgs_farm:cow",
            "sgs_farm:cow_m"
        ]
    },
    {
        ID: "minecraft:pig",
        transforms: [
            "sgs_farm:pig",
            "sgs_farm:pig_m"
        ]
    },
    {
        ID: "minecraft:sheep",
        transforms: [
            "sgs_farm:sheep",
            "sgs_farm:sheep_m"
        ]
    },
    {
        ID: "minecraft:goat",
        transforms: [
            "sgs_farm:goat",
            "sgs_farm:goat_m"
        ]
    },
    {
        ID: "minecraft:rabbit",
        transforms: [
            "sgs_farm:hare",
            "sgs_farm:hare_m"
        ]
    },
    {
        ID: "minecraft:chicken",
        transforms: [
            "sgs_farm:chicken",
            "sgs_farm:chicken_m",
            "sgs_farm:chicken",
            "sgs_farm:chicken_m",
            "sgs_farm:duck",
            "sgs_farm:duck_m",
            "sgs_farm:duck",
            "sgs_farm:duck_m",
            "sgs_farm:turkey",
            "sgs_farm:turkey_m"
        ]
    }
]

transformInteract();

function transformInteract() {

    mc.world.beforeEvents.playerInteractWithEntity.subscribe((data) => {
        const entity = data.target;
        const player = data.player;

        const mainhand = player.getComponent(mc.EntityEquippableComponent.componentId).getEquipmentSlot(mc.EquipmentSlot.Mainhand);
        const heldItem = mainhand.getItem();
        if (heldItem == undefined) return;
        if (heldItem.typeId != "sgs_farm:farmer_hat") return;

        const entityData = vanillaAnimalList.find((f) => f.ID == entity.typeId);
        if (entityData != undefined) {

            if (mc.world.scoreboard.getObjective("sgs_farm_animals").getScore("HAT_TRANSFORM") == 0) {
                mc.system.runTimeout(() => {
                    player.onScreenDisplay.setActionBar("§aFarmer's Hat§r transform option is §cdisabled");
                }, 1)
                return;
            }

            const dimension = entity.dimension;
            const num = randomWholeNum(0, entityData.transforms.length);
            const location = entity.location;
            const rotation = entity.getRotation();

            mc.system.runTimeout(() => {
                let transform = dimension.spawnEntity(entityData.transforms[num], location);
                transform.addTag("sgs_farm_transformed");
                transform.setRotation(rotation);

                entity.remove();


                transform.playAnimation("animation.sgs_farm_animals.scale_in");
                transform.dimension.spawnParticle("sgs_farm:feeder_hay", location);
                transform.dimension.playSound("block.decorated_pot.insert", location);
            }, 0)
            data.cancel = true;
            return;
        }

        const animalData = animalList.find((f) => f.ID == entity.typeId);
        if (animalData != undefined) {

            const location = entity.location;
            const has_hat = entity.getProperty("sgs_farm:has_hat");
            if (has_hat == undefined) return;


            mc.system.runTimeout(() => {
                if (has_hat) {
                    entity.setProperty("sgs_farm:has_hat", false);
                    entity.dimension.playSound("random.pop", location);
                }
                else {
                    entity.setProperty("sgs_farm:has_hat", true);
                    entity.dimension.playSound("armor.equip_generic", location);
                }
            }, 0)
            data.cancel = true;
            return;
        }
    });
}