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

export class fenceManager {
    static beforeplace(data) {
        const block = data.block;
        
        const north = block.north();
        const east = block.east();
        const south = block.south();
        const west = block.west();

        const northConnects = (north.hasTag("sgs_farm_fence"));
        const eastConnects = (east.hasTag("sgs_farm_fence"));
        const southConnects = (south.hasTag("sgs_farm_fence"));
        const westConnects = (west.hasTag("sgs_farm_fence"));

        if(northConnects && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 1));
        if(eastConnects  && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 1));
        if(southConnects && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 1));
        if(westConnects && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 1));

        data.permutationToPlace = mc.BlockPermutation.resolve(data.permutationToPlace.type.id, {'sgs_farm:fence_inventory': 1,'sgs_farm:fence_north': northConnects ? 1 : 0,'sgs_farm:fence_south': southConnects ? 1: 0,'sgs_farm:fence_east': eastConnects ? 1 : 0,'sgs_farm:fence_west': westConnects ? 1 : 0});
    }

    static destroy(data) {
        
        const northConnects = data.destroyedBlockPermutation.getState("sgs_farm:fence_north");
        const eastConnects = data.destroyedBlockPermutation.getState("sgs_farm:fence_east");
        const southConnects = data.destroyedBlockPermutation.getState("sgs_farm:fence_south");
        const westConnects = data.destroyedBlockPermutation.getState("sgs_farm:fence_west");

        try {
            const block = data.block;
            const north = block.north();
            const east = block.east();
            const south = block.south();
            const west = block.west();
    
            if(northConnects && north.hasTag("sgs_farm_fence") && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 0));
            if(eastConnects  && east.hasTag("sgs_farm_fence") && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 0));
            if(southConnects && south.hasTag("sgs_farm_fence") && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 0));
            if(westConnects && west.hasTag("sgs_farm_fence") && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 0));
            
        } catch (error) { };
    }

    static tick(block) {
        try {
            if(block.isAir) return;

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

            const northConnects = (north.hasTag("sgs_farm_fence"));
            const eastConnects = (east.hasTag("sgs_farm_fence"));
            const southConnects = (south.hasTag("sgs_farm_fence"));
            const westConnects = (west.hasTag("sgs_farm_fence"));

            block.setPermutation(block.permutation.withState('sgs_farm:fence_north', northConnects ? 1 : 0));
            block.setPermutation(block.permutation.withState('sgs_farm:fence_south', southConnects ? 1: 0));
            block.setPermutation(block.permutation.withState('sgs_farm:fence_east', eastConnects ? 1 : 0));
            block.setPermutation(block.permutation.withState('sgs_farm:fence_west', westConnects ? 1 : 0));
        }
        catch (error) { };
    };

    static beforeplaceGate(data) {
        const block = data.block;
        
        const north = block.north();
        const east = block.east();
        const south = block.south();
        const west = block.west();

        const northConnects = (north.hasTag("sgs_farm_fence"));
        const eastConnects = (east.hasTag("sgs_farm_fence"));
        const southConnects = (south.hasTag("sgs_farm_fence"));
        const westConnects = (west.hasTag("sgs_farm_fence"));

        if(northConnects && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 1));
        if(eastConnects  && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 1));
        if(southConnects && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 1));
        if(westConnects && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 1));
    }

    static interactGate(block, player) {
        const currentState = block.permutation.getState('sgs_farm:open');
        const newOpenState = !currentState;
        const sound = newOpenState ? 'open.fence_gate' : 'close.fence_gate';

        const rotationAngle = player.getRotation().y;
        const openSouth = block.permutation.getState('minecraft:cardinal_direction') != directionDisplay(rotationAngle);

        //player.sendMessage("block: " + block.permutation.getState('minecraft:cardinal_direction') + " player:" + directionDisplay(rotationAngle));

        const newPermutation = mc.BlockPermutation.resolve(block.typeId, {
            ...block.permutation.getAllStates(),
            'sgs_farm:open': newOpenState,
            'sgs_farm:open_south': openSouth
        });

        block.setPermutation(newPermutation);
        block.dimension.playSound(sound, block.location);
    }

    static destroyGate(data) {
        try {
            const block = data.block;
            const north = block.north();
            const east = block.east();
            const south = block.south();
            const west = block.west();
    
            if(north.hasTag("sgs_farm_fence") && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 0));
            if(east.hasTag("sgs_farm_fence") && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 0));
            if(south.hasTag("sgs_farm_fence") && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 0));
            if(west.hasTag("sgs_farm_fence") && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 0));
            
        } catch (error) { };
    }
}

function directionDisplay(angle) {
    if (Math.abs(angle) > 112.5) return 'north';
    if (Math.abs(angle) < 67.5) return 'south';
    if (angle < 157.5 && angle > 22.5) return 'west';
    if (angle > -157.5 && angle < -22.5) return 'east';
    return '';
}