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

const SIZE = 2;
const layout_south = [
    "top_northwest", "top_northeast",
    "top_southwest", "top_southeast",

    "bottom_northwest", "bottom_northeast",
    "bottom_southwest", "bottom_southeast"
]

const layout_north = [
    "top_southeast", "top_southwest",
    "top_northeast", "top_northwest",

    "bottom_southeast", "bottom_southwest",
    "bottom_northeast", "bottom_northwest"
]

const layout_west = [
    "top_southwest", "top_northwest",
    "top_southeast", "top_northeast",

    "bottom_southwest", "bottom_northwest",
    "bottom_southeast", "bottom_northeast"
]

const layout_east = [
    "top_northeast", "top_southeast",
    "top_northwest", "top_southwest",

    "bottom_northeast", "bottom_southeast",
    "bottom_northwest", "bottom_southwest"
]

const layout_coordinates = [
    { x: 0, y: 1, z: 0 },
    { x: 1, y: 1, z: 0 },
    { x: 0, y: 1, z: 1 },
    { x: 1, y: 1, z: 1 },

    { x: 0, y: 0, z: 0 },
    { x: 1, y: 0, z: 0 },
    { x: 0, y: 0, z: 1 },
    { x: 1, y: 0, z: 1 }
]

export class multiblockManager {
    static beforeplace(data) {
        const block = data.block;
        const blockId = data.permutationToPlace.type.id;

        let height
        let direction
        let layer

        if (block.above().type.id == blockId) {
            height = "bottom";
            direction = checkLayer(block, blockId);
            layer = checkLayer(block.above(), blockId);
        }

        if (block.below().type.id == blockId) {
            height = "top";
            direction = checkLayer(block, blockId);
            layer = checkLayer(block.below(), blockId);
        }

        if (height != undefined && direction != undefined && layer != undefined) {
            //mc.world.sendMessage("passed: " + height + "_" + direction);
            mc.system.runTimeout(() => {
                applyBlocksFromOrigin(block, height, direction);
            }, 1)
        }
    }

    static destroy(data) {
        if (data.brokenBlockPermutation.getState('sgs_cake:multiblock_sections') != undefined) {

            const originLocation = getOriginLocation(data.brokenBlockPermutation, data.block);
            const dimension = data.block.dimension;

            for (let index = 0; index < layout_coordinates.length; index++) {
                const block = dimension.getBlock({ x: originLocation.x + layout_coordinates[index].x, y: originLocation.y + layout_coordinates[index].y, z: originLocation.z + layout_coordinates[index].z });
                if (block.type.id == data.brokenBlockPermutation.type.id) {
                    const type = block.type.id;
                    block.setPermutation(mc.BlockPermutation.resolve(type.substring(0, type.length - 3), { 'minecraft:cardinal_direction': data.brokenBlockPermutation.getState('minecraft:cardinal_direction') }));
                }
            }
        }
    }
}


function checkLayer(block, blockId) {

    if (block.north().type.id == blockId && block.east().type.id == blockId && block.north().east().type.id == blockId) {
        return "southwest";
    }
    if (block.north().type.id == blockId && block.west().type.id == blockId && block.north().west().type.id == blockId) {
        return "southeast";
    }
    if (block.south().type.id == blockId && block.east().type.id == blockId && block.south().east().type.id == blockId) {
        return "northwest";
    }
    if (block.south().type.id == blockId && block.west().type.id == blockId && block.south().west().type.id == blockId) {
        return "northeast";
    }
}

function applyBlocksFromOrigin(block, height, direction) {
    let type = block.type.id + "_mb";
    let origin = block.location;
    let dimension = block.dimension;

    let cardinal = block.permutation.getState('minecraft:cardinal_direction');
    let layout = layout_north;

    if (cardinal == "north") layout = layout_north;
    if (cardinal == "south") layout = layout_south;
    if (cardinal == "east") layout = layout_east;
    if (cardinal == "west") layout = layout_west;

    if (height == "top") origin = { x: origin.x, y: origin.y - 1, z: origin.z };
    if (direction == "southwest") origin = { x: origin.x, y: origin.y, z: origin.z - 1 };
    if (direction == "southeast") origin = { x: origin.x - 1, y: origin.y, z: origin.z - 1 };
    if (direction == "northwest") origin = { x: origin.x, y: origin.y, z: origin.z };
    if (direction == "northeast") origin = { x: origin.x - 1, y: origin.y, z: origin.z };

    dimension.getBlock({ x: origin.x + 0, y: origin.y + 1, z: origin.z + 0 }).setPermutation(mc.BlockPermutation.resolve(type, { 'minecraft:cardinal_direction': cardinal, 'sgs_cake:multiblock_sections': layout[0] }));
    dimension.getBlock({ x: origin.x + 1, y: origin.y + 1, z: origin.z + 0 }).setPermutation(mc.BlockPermutation.resolve(type, { 'minecraft:cardinal_direction': cardinal, 'sgs_cake:multiblock_sections': layout[1] }));
    dimension.getBlock({ x: origin.x + 0, y: origin.y + 1, z: origin.z + 1 }).setPermutation(mc.BlockPermutation.resolve(type, { 'minecraft:cardinal_direction': cardinal, 'sgs_cake:multiblock_sections': layout[2] }));
    dimension.getBlock({ x: origin.x + 1, y: origin.y + 1, z: origin.z + 1 }).setPermutation(mc.BlockPermutation.resolve(type, { 'minecraft:cardinal_direction': cardinal, 'sgs_cake:multiblock_sections': layout[3] }));

    dimension.getBlock({ x: origin.x + 0, y: origin.y + 0, z: origin.z + 0 }).setPermutation(mc.BlockPermutation.resolve(type, { 'minecraft:cardinal_direction': cardinal, 'sgs_cake:multiblock_sections': layout[4] }));
    dimension.getBlock({ x: origin.x + 1, y: origin.y + 0, z: origin.z + 0 }).setPermutation(mc.BlockPermutation.resolve(type, { 'minecraft:cardinal_direction': cardinal, 'sgs_cake:multiblock_sections': layout[5] }));
    dimension.getBlock({ x: origin.x + 0, y: origin.y + 0, z: origin.z + 1 }).setPermutation(mc.BlockPermutation.resolve(type, { 'minecraft:cardinal_direction': cardinal, 'sgs_cake:multiblock_sections': layout[6] }));
    dimension.getBlock({ x: origin.x + 1, y: origin.y + 0, z: origin.z + 1 }).setPermutation(mc.BlockPermutation.resolve(type, { 'minecraft:cardinal_direction': cardinal, 'sgs_cake:multiblock_sections': layout[7] }));

    highlightBlock(dimension.getBlock({ x: origin.x + 0, y: origin.y + 1, z: origin.z + 0 }));
    highlightBlock(dimension.getBlock({ x: origin.x + 1, y: origin.y + 1, z: origin.z + 0 }));
    highlightBlock(dimension.getBlock({ x: origin.x + 0, y: origin.y + 1, z: origin.z + 1 }));
    highlightBlock(dimension.getBlock({ x: origin.x + 1, y: origin.y + 1, z: origin.z + 1 }));
    highlightBlock(dimension.getBlock({ x: origin.x + 0, y: origin.y + 0, z: origin.z + 0 }));
    highlightBlock(dimension.getBlock({ x: origin.x + 1, y: origin.y + 0, z: origin.z + 0 }));
    highlightBlock(dimension.getBlock({ x: origin.x + 0, y: origin.y + 0, z: origin.z + 1 }));
    highlightBlock(dimension.getBlock({ x: origin.x + 1, y: origin.y + 0, z: origin.z + 1 }));
}

function getOriginLocation(permutation, block) {
    const cardinal = permutation.getState('minecraft:cardinal_direction');
    const section = permutation.getState('sgs_cake:multiblock_sections');
    const location = block.location;

    let origin = { x: 0, y: 0, z: 0 };

    if (cardinal == "north") {
        for (let index = 0; index < layout_north.length; index++) {
            if (section == layout_north[index]) {
                origin = layout_coordinates[index];
            }
        }
    }

    if (cardinal == "south") {
        for (let index = 0; index < layout_south.length; index++) {
            if (section == layout_south[index]) {
                origin = layout_coordinates[index];
            }

        }
    }

    if (cardinal == "east") {
        for (let index = 0; index < layout_east.length; index++) {
            if (section == layout_east[index]) {
                origin = layout_coordinates[index];
            }
        }
    }

    if (cardinal == "west") {
        for (let index = 0; index < layout_west.length; index++) {
            if (section == layout_west[index]) {
                origin = layout_coordinates[index];
            }
        }
    }

    return { x: location.x - origin.x, y: location.y - origin.y, z: location.z - origin.z };
}