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

const BLOCK_ENTITY = "sgs_cake:villager_baker_stand";
const SPAWN_ANIMATION = "interact";

mc.system.afterEvents.scriptEventReceive.subscribe((eventData) => {
    const { id, sourceEntity, message } = eventData;
    if (sourceEntity == undefined) return;

    if (id == "sgs_cake:trader" && message == "open") {

        const location = sourceEntity.location;
        const dimension = sourceEntity.dimension.id;
        const block = mc.world.getDimension(dimension).getBlock({ x: location.x, y: location.y + 0.5, z: location.z })
        const day = mc.world.getDay();

        //check and initialize trader day
        let date = sourceEntity.getDynamicProperty("sgs_cake_day");
        if (date == undefined) {
            date = day;
            sourceEntity.setDynamicProperty("sgs_cake_day", date);
        }

        //if day has progressed, transform to resupply trades & open up store
        if (date != day) {
            sourceEntity.triggerEvent("sgs_cake:resupply");
        }
        block.setPermutation(block.permutation.withState("sgs_cake:is_closed", 0));
        return;
    };

    if (id == "sgs_cake:trader" && message == "close") {
        const location = sourceEntity.location;
        const dimension = sourceEntity.dimension.id;
        const block = mc.world.getDimension(dimension).getBlock({ x: location.x, y: location.y + 0.5, z: location.z });
        block.setPermutation(block.permutation.withState("sgs_cake:is_closed", 1));
    };
})

export class tradeStationManager {

    static onplace(data) {
        const block = data.block;
        const location = block.bottomCenter();

        let canPlace = true;
        const direction = block.permutation.getState("minecraft:cardinal_direction");
        switch (direction) {
            case "north":
                if (!block.north().isAir) canPlace = false;
                break;
            case "east":
                if (!block.east().isAir) canPlace = false;
                break;
            case "south":
                if (!block.south().isAir) canPlace = false;
                break;
            case "west":
                if (!block.west().isAir) canPlace = false;
                break;
            default:
                break;
        }

        if (canPlace) {
            const display_entity = mc.world.getDimension(block.dimension.id).spawnEntity(BLOCK_ENTITY, location);

            switch (direction) {
                case "north":
                    display_entity.setProperty("sgs_cake:rotation", 0);
                    highlightBlock(block);
                    highlightBlock(block.north());
                    break;
                case "east":
                    display_entity.setProperty("sgs_cake:rotation", 1);
                    highlightBlock(block);
                    highlightBlock(block.east());
                    break;
                case "south":
                    display_entity.setProperty("sgs_cake:rotation", 2);
                    highlightBlock(block);
                    highlightBlock(block.south());
                    break;
                case "west":
                    display_entity.setProperty("sgs_cake:rotation", 3);
                    highlightBlock(block);
                    highlightBlock(block.west());
                    break;
                default:
                    break;
            }

            mc.system.runTimeout(() => {
                display_entity.playAnimation(SPAWN_ANIMATION);
            }, 3)
        }
        else {
            const loc = block.location;
            block.dimension.runCommand(`setblock ${loc.x} ${loc.y} ${loc.z} air [] destroy`);
        }
    }

    static destroy(data) {
        const block = data.block;
        const display_entities = getDisplayEntities(block);
        display_entities.forEach(ent => {
            ent.remove();
        });

    }
}

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

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