import * as mc from "@minecraft/server";

export class pergolaManager {

    static beforeplace(data) {
        
        data.permutationToPlace = placeNewFrameBlockNorthSouth(data.block, data.permutationToPlace);
    }

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

function placeNewFrameBlock(block, permutation) {

    const below = block.below();
    const above = block.above();
    const north = block.north();
    const east = block.east();
    const south = block.south();
    const west = block.west();

    const northConnects = (north.hasTag("sgs_farm_pergola"));
    const southConnects = (south.hasTag("sgs_farm_pergola"));
    const eastConnects = (east.hasTag("sgs_farm_pergola"));
    const westConnects = (west.hasTag("sgs_farm_pergola"));
    const belowConnects = (below.hasTag("sgs_farm_pergola"));
    const aboveConnects = (above.hasTag("sgs_farm_pergola"));

    let cardinal_direction = data.permutationToPlace.getState('minecraft:cardinal_direction');
    if (northConnects) cardinal_direction = north.getState('minecraft:cardinal_direction');
    if (southConnects) cardinal_direction = south.getState('minecraft:cardinal_direction');
    if (eastConnects) cardinal_direction = east.getState('minecraft:cardinal_direction');
    if (westConnects) cardinal_direction = west.getState('minecraft:cardinal_direction');

    let right;
    let left;
    let back;
    let front;
    let up = block.above();
    let down = block.below();

    switch (direction) {
        case "north":
            right = eastConnects;
            left = westConnects;
            back = northConnects;
            front = southConnects;
            break;
        case "east":
            right = southConnects;
            left = northConnects;
            back = eastConnects;
            front = westConnects;
            break;
        case "west":
            right = northConnects;
            left = southConnects;
            back = westConnects;
            front = eastConnects;
            break;
        case "south":
            right = westConnects;
            left = eastConnects;
            back = southConnects;
            front = northConnects;
            break;
        default:
            break;
    }

    if (right) {
        right.setPermutation(right.permutation.withState('sgs_farm:south', 1));
    }
    else
    {

    }
    if (left) east.setPermutation(east.permutation.withState('sgs_farm:west', 1));
    if (up) east.setPermutation(east.permutation.withState('sgs_farm:west', 1));
    if (down) east.setPermutation(east.permutation.withState('sgs_farm:west', 1));

    return mc.BlockPermutation.resolve(permutation.type.id, {
        'minecraft:cardinal_direction': cardinal_direction,
        'sgs_farm:corner': base ? 1 : 0,
        'sgs_farm:north': northConnects ? 1 : 0, 
        'sgs_farm:south': southConnects ? 1 : 0, 
        'sgs_farm:east': eastConnects ? 1 : 0, 
        'sgs_farm:west': westConnects ? 1 : 0 
    });
}

function placeNewFrameBlockNorthSouth(block, permutation) {

    const below = block.below();
    const above = block.above();
    const north = block.north();
    const east = block.east();
    const south = block.south();
    const west = block.west();

    let corner = above.isAir;

    let eastConnects = true;
    let westConnects = true;
    let cardinal_direction = permutation.getState('minecraft:cardinal_direction');

    /*
    eastConnects = (east.hasTag("sgs_farm_pergola"));
    westConnects = (west.hasTag("sgs_farm_pergola"));


    let northConnects = (north.hasTag("sgs_farm_pergola"));
    let southConnects = (south.hasTag("sgs_farm_pergola"));
    let eastConnects = (east.hasTag("sgs_farm_pergola"));
    let westConnects = (west.hasTag("sgs_farm_pergola"));
    let belowConnects = (below.hasTag("sgs_farm_pergola"));
    let aboveConnects = (above.hasTag("sgs_farm_pergola"));

    let cardinal_direction = permutation.getState('minecraft:cardinal_direction');
    if (northConnects) cardinal_direction = north.permutation.getState('minecraft:cardinal_direction');
    if (southConnects) cardinal_direction = south.permutation.getState('minecraft:cardinal_direction');
    if (eastConnects) cardinal_direction = east.permutation.getState('minecraft:cardinal_direction');
    if (westConnects) cardinal_direction = west.permutation.getState('minecraft:cardinal_direction');
    
    if (eastConnects) {
        east.setPermutation(east.permutation.withState('sgs_farm:west', 0));
        east.setPermutation(east.permutation.withState('sgs_farm:north', 0));
        if(!westConnects) corner = true;
    };
    if (westConnects) {
        west.setPermutation(west.permutation.withState('sgs_farm:east', 0));
        west.setPermutation(west.permutation.withState('sgs_farm:south', 0));
        if(!eastConnects) corner = true;
    }
     */

    if(above.isAir)
    {
        corner = true;
    }

    if(below.isAir)
    {
        westConnects = false;
        eastConnects = false;
    }

    if(below.hasTag("sgs_farm_pergola"))
    {
        below.setPermutation(below.permutation.withState('sgs_farm:corner', 0));
    }

    //mc.world.sendMessage("n: " + northConnects);
    mc.world.sendMessage("e: " + eastConnects);
    //mc.world.sendMessage("s: " + southConnects);
    mc.world.sendMessage("w: " + westConnects);

    return mc.BlockPermutation.resolve(permutation.type.id, {
        'sgs_farm:inventory': 1,
        'minecraft:cardinal_direction': cardinal_direction,
        'sgs_farm:corner': corner ? 1 : 0,
        'sgs_farm:north': westConnects ? 1 : 0, 
        'sgs_farm:west': westConnects ? 1 : 0, 
        'sgs_farm:east': eastConnects ? 1 : 0, 
        'sgs_farm:south': eastConnects ? 1 : 0 
    });
}