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

const BOTTLES = [
    {
        id: "sgs_cake:bottle",
        variant: 0
    },
    {
        id: "sgs_cake:apple_juice",
        variant: 1
    },
    {
        id: "sgs_cake:banana_juice",
        variant: 2
    },
    {
        id: "sgs_cake:blueberry_juice",
        variant: 3
    },
    {
        id: "sgs_cake:strawberry_juice",
        variant: 4
    },
    {
        id: "sgs_cake:sweet_berry_juice",
        variant: 5
    },
    {
        id: "sgs_cake:mango_juice",
        variant: 6
    },
    {
        id: "sgs_cake:plum_juice",
        variant: 7
    },
    {
        id: "sgs_cake:fruit_juice",
        variant: 11
    },
    {
        id: "sgs_cake:cream_bottle",
        variant: 9
    },
    {
        id: "sgs_cake:water_bottle",
        variant: 8
    },
    {
        id: "sgs_cake:milk_bottle",
        variant: 10
    },
    //C&F
    {
        id: "sgs_farm:bottle",
        variant: 0
    },
    {
        id: "sgs_farm:juice_apple",
        variant: 5
    },
    {
        id: "sgs_farm:juice_blackcurrant",
        variant: 14
    },
    {
        id: "sgs_farm:juice_cherry",
        variant: 5
    },
    {
        id: "sgs_farm:juice_grape_green",
        variant: 13
    },
    {
        id: "sgs_farm:juice_grape_red",
        variant: 11
    },
    {
        id: "sgs_farm:juice_lemon",
        variant: 2
    },
    {
        id: "sgs_farm:juice_lime",
        variant: 13
    },
    {
        id: "sgs_farm:juice_orange",
        variant: 12
    },
    {
        id: "sgs_farm:juice_passionfruit",
        variant: 6
    },
    {
        id: "sgs_farm:juice_peach",
        variant: 4
    },
    {
        id: "sgs_farm:juice_pear",
        variant: 1
    },
    {
        id: "sgs_farm:juice_pineapple",
        variant: 6
    },
    {
        id: "sgs_farm:juice_redcurrant",
        variant: 5
    },
    {
        id: "sgs_farm:milk_buffalo",
        variant: 10
    },
    {
        id: "sgs_farm:milk_cow",
        variant: 10
    },
    {
        id: "sgs_farm:milk_goat",
        variant: 10
    },
    {
        id: "sgs_farm:milk_sheep",
        variant: 10
    },
    {
        id: "sgs_farm:water_bottle",
        variant: 8
    }
]

const JARS = [
    {
        id: "sgs_cake:glass_jar",
        variant: 0
    },
    {
        id: "sgs_cake:apple_jam",
        variant: 1
    },
    {
        id: "sgs_cake:banana_jam",
        variant: 2
    },
    {
        id: "sgs_cake:blueberry_jam",
        variant: 3
    },
    {
        id: "sgs_cake:strawberry_jam",
        variant: 4
    },
    {
        id: "sgs_cake:sweet_berry_jam",
        variant: 5
    },
    {
        id: "sgs_cake:mango_jam",
        variant: 6
    },
    {
        id: "sgs_cake:plum_jam",
        variant: 7
    },
    {
        id: "sgs_cake:fruit_jam",
        variant: 7
    },
    //C&F
    {
        id: "sgs_cake:jar",
        variant: 0
    },
    {
        id: "sgs_farm:jam_blackberry",
        variant: 7
    },
    {
        id: "sgs_farm:jam_blueberry",
        variant: 3
    },
    {
        id: "sgs_farm:jam_gooseberry",
        variant: 1
    },
    {
        id: "sgs_farm:jam_raspberry",
        variant: 5
    },
    {
        id: "sgs_farm:jam_strawberry",
        variant: 4
    },
    {
        id: "sgs_farm:pesto",
        variant: 1
    },
    {
        id: "sgs_farm:sauce_bolognese",
        variant: 5
    },
    {
        id: "sgs_farm:sauce_tomato",
        variant: 5
    },
    {
        id: "sgs_farm:spread_chocolate",
        variant: 9
    },
    {
        id: "sgs_farm:spread_peanut",
        variant: 9
    }
]


export class glassRackManager {

    static interact(data, parameters) {
        const block = data.block;
        const player = data.player;
        const location = block.bottomCenter();

        const type = parameters.params.type;

        const mainhand = player.getComponent(EntityEquippableComponent.componentId).getEquipmentSlot(EquipmentSlot.Mainhand);
        const heldItem = mainhand.getItem();
        if (heldItem == undefined)
        {
            if(player.isSneaking) blocklib.clearDisplay(block);
            return;
        }

        let itemData;
        if (type == "jar") itemData = JARS.find((f) => f.id == heldItem.typeId);
        if (type == "bottle") itemData = BOTTLES.find((f) => f.id == heldItem.typeId);
        if (itemData == undefined) return;

        let display_entity = getDisplayEntity(block);

        if (display_entity == undefined) {

            if (type == "jar") {

                const itemData = JARS.find((f) => f.id == heldItem.typeId);
                if (itemData == undefined) return;

                display_entity = mc.world.getDimension(player.dimension.id).spawnEntity("sgs_cake:jar_display", location);
                const direction = block.permutation.getState("minecraft:cardinal_direction");
                switch (direction) {
                    case "north":
                        display_entity.setProperty("sgs_cake:rotation", 0);
                        break;
                    case "east":
                        display_entity.setProperty("sgs_cake:rotation", 1);
                        break;
                    case "south":
                        display_entity.setProperty("sgs_cake:rotation", 2);
                        break;
                    case "west":
                        display_entity.setProperty("sgs_cake:rotation", 3);
                        break;
                    default:
                        break;
                }
            }

            if (type == "bottle") {

                const itemData = BOTTLES.find((f) => f.id == heldItem.typeId);
                if (itemData == undefined) return;

                display_entity = mc.world.getDimension(player.dimension.id).spawnEntity("sgs_cake:bottle_display", location);
            }

            block.dimension.playSound("block.decorated_pot.insert", block.center());

            const inventory = display_entity.getComponent('minecraft:inventory').container;
            const addItem = heldItem.clone();
            addItem.amount = 1;
            inventory.setItem(0, addItem);

            display_entity.setProperty("sgs_cake:variant_1", itemData.variant);
            display_entity.setProperty("sgs_cake:rack_sections", 0);
            if (player.getGameMode() == mc.GameMode.Creative) return;
            mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
            return;
        }

        const inventory = display_entity.getComponent('minecraft:inventory').container;

        //interacting with full display, dismantle
        if (inventory.emptySlotsCount == 0) {
            blocklib.clearDisplay(block);
            return;
        }

        let property;
        let slot = inventory.size - inventory.emptySlotsCount;

        if (slot == 0) property = "sgs_cake:variant_1";
        if (slot == 1) property = "sgs_cake:variant_2";
        if (slot == 2) property = "sgs_cake:variant_3";
        if (slot == 3) property = "sgs_cake:variant_4";
        display_entity.setProperty("sgs_cake:rack_sections", slot);

        block.dimension.playSound("block.decorated_pot.insert", block.center());

        const addItem = heldItem.clone();
        addItem.amount = 1;
        inventory.setItem(slot, addItem);

        display_entity.setProperty(property, itemData.variant);
        if (player.getGameMode() == mc.GameMode.Creative) return;
        mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
    }

    static destroy(data) {
        const block = data.block;
        blocklib.clearDisplay(block);
    }
}

function getDisplayEntity(block) {
    const location = block.bottomCenter();
    return mc.world.getDimension(block.dimension.id).getEntities({ location, families: ["sgs_cake_display"], maxDistance: 0.2 })[0];
}

function addItem(slot, heldItem, entity) {

}