import * as mc from "@minecraft/server";
import { itemManager } from "../sgs_cake_lib";

export const DISPLAY_STAND_CONFIG = [
    {
        tag: "sgs_cake_display_plate",
        height: -1
    },
    {
        tag: "sgs_cake_display_tray",
        height: -1
    },
    {
        tag: "sgs_cake_display_stand",
        height: 4
    },
    {
        tag: "sgs_cake_display_stand_tall",
        height: 14
    },
    //STACK DISPLAYS
    {
        tag: "sgs_cake_display_stand_tier",
        max_stack: 2,
        height: [4, 14],
        scale: [1.25, 0.98]
    },
    /*
    {
        tag: "sgs_cake_display_basket_small",
        max_stack: 3,
        height: [2, 2, 2],
        x_off: [0, 0, 0],
        z_off: [0, -3, 3],

        rot_x: [-9, -9, -9],
        rot_y: [1, -1, 1],
        rot_z: [1, -1, 1]
    },
    */
    {
        tag: "sgs_cake_display_basket_small",
        max_stack: 3,
        height: [3, 2, 2],
        x_off: [1, 0, 0],
        z_off: [0, -3, 3],

        rot_x: [-11, -13, -13],
        rot_y: [1, -6, 6],
        rot_z: [1, -1, 1]
    },
    {
        tag: "sgs_cake_display_basket_angled",
        max_stack: 3,
        height: [3, 2, 2],
        x_off: [2, 0, 0],
        z_off: [0, -3, 3],

        rot_x: [-25, -20, -20],
        rot_y: [1, -1, 1],
        rot_z: [1, -1, 1]
    },
    /*
    {
        tag: "sgs_cake_display_basket_angled",
        max_stack: 3,
        height: [-1, -1, -1],
        x_off: [4, 1, -2],

        rot_x: [-15, -15, -15],
        rot_y: [3, -1, 1],
        rot_z: [3, -1, 1]

    },
    */
    {
        tag: "sgs_cake_display_basket_tall",
        max_stack: 5,
        height: [2, 4, 6, 8, 10],

        x_off: [0, 1, 0, -1, 0],
        z_off: [0, -2, 2, 0, -2],
        rot_y: [0, -5, 8, -7, 3]
    },
    {
        tag: "sgs_cake_display_basket_shelf",
        max_stack: 3,
        height: [4, 3, 3],
        x_off: [-1, -2, -2],
        z_off: [0, -3, 3],

        rot_x: [-25, -20, -20],
        rot_y: [1, -1, 1],
        rot_z: [1, -1, 1]
    },
]

export class foodDisplayManager {
    static interact(data) {
        const block = data.block;
        const player = data.player;
        const location = block.center();

        const display_entity = getDisplayEntity(block);

        if (display_entity == undefined) {
            const mainhand = player.getComponent(mc.EntityEquippableComponent.componentId).getEquipmentSlot(mc.EquipmentSlot.Mainhand);
            const heldItem = mainhand.getItem();
            if (heldItem == undefined) return;
            if (!heldItem.hasTag("is_food") && !heldItem.hasTag("is_fish")) return;    //only display items tagged food

            player.dimension.playSound("block.decorated_pot.insert", location);

            const display_entity = mc.world.getDimension(player.dimension.id).spawnEntity("sgs_cake:display", location);
            const direction = data.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;
            }

            let displayData = DISPLAY_STAND_CONFIG[0];
            DISPLAY_STAND_CONFIG.forEach(element => {
                if (block.hasTag(element.tag)) {
                    displayData = element;
                }
            });

            display_entity.setProperty("sgs_cake:height", displayData.height);

            //store item in display inventory
            const inventory = display_entity.getComponent('minecraft:inventory').container;
            const addItem = heldItem.clone();
            addItem.amount = 1;
            inventory.addItem(addItem);

            //place item visual
            mc.world.getDimension(player.dimension.id).runCommand("/execute positioned " + location.x + " " + location.y + " " + location.z + " run /replaceitem entity @e[type=sgs_cake:display,r=0.2]  slot.weapon.mainhand 1 " + heldItem.typeId);

            if (player.getGameMode() == mc.GameMode.Creative) return;
            mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
            return;
        }
        else {
            clearDisplay(block);
        }
    }

    static interactStack(data) {
        const block = data.block;
        const player = data.player;
        const location = block.center();

        let displayData = DISPLAY_STAND_CONFIG[0];
        DISPLAY_STAND_CONFIG.forEach(element => {
            if (block.hasTag(element.tag)) {
                displayData = element;
            }
        });
        let max_stack = displayData.max_stack - 1;    //count starts at 0

        const display_entities = getDisplayEntities(block);
        const count = display_entities.length;

        //max stack
        if (count <= max_stack) {
            const mainhand = player.getComponent(mc.EntityEquippableComponent.componentId).getEquipmentSlot(mc.EquipmentSlot.Mainhand);
            const heldItem = mainhand.getItem();
            if (heldItem == undefined) {
                clearDisplay(block);
                return;
            }
            if (!heldItem.hasTag("is_food") && !heldItem.hasTag("is_fish")) return;    //only display items tagged food

            player.dimension.playSound("block.decorated_pot.insert", location);

            const display_entity = mc.world.getDimension(player.dimension.id).spawnEntity("sgs_cake:display", location);
            const direction = data.block.permutation.getState("minecraft:cardinal_direction");

            let x_mod = 1;
            let z_mod = 1;


            if (direction == "north") { display_entity.setProperty("sgs_cake:rotation", 0) };
            if (direction == "east") { display_entity.setProperty("sgs_cake:rotation", 1) };
            if (direction == "south") {
                display_entity.setProperty("sgs_cake:rotation", 2),
                    z_mod = -1
            };
            if (direction == "west") {
                display_entity.setProperty("sgs_cake:rotation", 3),
                    x_mod = -1
            };

            /*
            mc.world.sendMessage("cardinal: " + direction);
            
            if (displayData.x_off != undefined) mc.world.sendMessage("z_off: " + z_mod * displayData.z_off[count]);
            if (displayData.z_off != undefined) mc.world.sendMessage("x_off: " + x_mod * displayData.x_off[count]);

            
            if (displayData.rot_x != undefined) mc.world.sendMessage("cardinal: " + displayData.rot_x[count]);
            if (displayData.rot_y != undefined) mc.world.sendMessage("cardinal: " + displayData.rot_y[count]);
            if (displayData.rot_z != undefined) mc.world.sendMessage("cardinal: " + displayData.rot_z[count]);
            */

            //adjust properties for this is a stack
            display_entity.setProperty("sgs_cake:height", displayData.height[count]);

            if (displayData.scale != undefined) {
                display_entity.setProperty("sgs_cake:scale", displayData.scale[count]);
            }

            if (displayData.x_off != undefined) {
                if (direction == "south" || direction == "north") display_entity.setProperty("sgs_cake:x_off", z_mod * displayData.z_off[count]);
                if (direction == "east" || direction == "west") display_entity.setProperty("sgs_cake:x_off", x_mod * displayData.x_off[count]);
            }
            if (displayData.z_off != undefined) {
                if (direction == "south" || direction == "north") display_entity.setProperty("sgs_cake:z_off", z_mod * displayData.x_off[count]);
                if (direction == "east" || direction == "west") display_entity.setProperty("sgs_cake:z_off", x_mod * displayData.z_off[count]);
            }

            if (displayData.rot_x != undefined) display_entity.setProperty("sgs_cake:rot_x", displayData.rot_x[count]);
            if (displayData.rot_y != undefined) display_entity.setProperty("sgs_cake:rot_y", displayData.rot_y[count]);
            if (displayData.rot_z != undefined) display_entity.setProperty("sgs_cake:rot_z", displayData.rot_z[count]);

            //store item in display inventory
            const inventory = display_entity.getComponent('minecraft:inventory').container;
            const addItem = heldItem.clone();
            addItem.amount = 1;
            inventory.addItem(addItem);

            //place item visual
            mc.world.getDimension(player.dimension.id).runCommand("/execute positioned " + location.x + " " + location.y + " " + location.z + " run /replaceitem entity @e[type=sgs_cake:display,r=0.2,tag=!sgs_cake_assigned]  slot.weapon.mainhand 1 " + heldItem.typeId);
            mc.system.runTimeout(() => { display_entity.addTag("sgs_cake_assigned") }, 1);    //add tag

            if (player.getGameMode() == mc.GameMode.Creative) return;
            mainhand.setItem(itemManager.reduceAmount(heldItem, 1));
            return;
        }
        else {
            clearDisplay(block);
        }
    }

    static destroy(data) {
        const block = data.block;
        const player = data.player;
        const location = block.center();

        const display_entities = getDisplayEntities(block);
        display_entities.forEach(ent => {
            const inventory = ent.getComponent('minecraft:inventory').container;
            let returnItem = inventory.getItem(0);
            const itemEntity = mc.world.getDimension(player.dimension.id).spawnItem(returnItem, location);
            itemEntity.clearVelocity();
            ent.remove();
        });

    }
}

function clearDisplay(block) {

    const display_entities = getDisplayEntities(block);
    display_entities.forEach(ent => {
        const inventory = ent.getComponent('minecraft:inventory').container;
        const returnItem = inventory.getItem(0);
        const itemEntity = mc.world.getDimension(block.dimension.id).spawnItem(returnItem, block.center());
        itemEntity.clearVelocity();
        ent.remove();
    });
}

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

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

