import { world, BlockPermutation } from '@minecraft/server';

// Checks if the stair has correct direction & vertical_half
function isCornerCandidateStair(block, direction, verticalHalf) {
    if (!block?.hasTag('pu_biomes:stair')) return false;

    const state = block.permutation.getAllStates();
    const isDirMatch = state['minecraft:cardinal_direction'] === direction;
    const isVertMatch = !verticalHalf || state['minecraft:vertical_half'] === verticalHalf;

    return isDirMatch && isVertMatch;
}

// Determines if a block matches a specific corner pattern (inner/outer, top/bottom)
function isMatchingCornerPattern(block, patternId) {
    const n = block.north();
    const s = block.south();
    const e = block.east();
    const w = block.west();

    const currentHalf = block.permutation.getAllStates()['minecraft:vertical_half']; // 'bottom' or 'top'

    switch (patternId) {
        // === Inner Corners ===
        case 'iNE':
            return (
                (isCornerCandidateStair(block, 'north', currentHalf) && isCornerCandidateStair(s, 'east', currentHalf)) ||
                (isCornerCandidateStair(block, 'east', currentHalf) && isCornerCandidateStair(w, 'north', currentHalf)) ||
                (isCornerCandidateStair(block, 'east', currentHalf) && isCornerCandidateStair(s, 'east', currentHalf) && isCornerCandidateStair(w, 'north', currentHalf))
            );
        case 'iNW':
            return (
                (isCornerCandidateStair(block, 'north', currentHalf) && isCornerCandidateStair(s, 'west', currentHalf)) ||
                (isCornerCandidateStair(block, 'west', currentHalf) && isCornerCandidateStair(e, 'north', currentHalf)) ||
                (isCornerCandidateStair(block, 'west', currentHalf) && isCornerCandidateStair(s, 'west', currentHalf) && isCornerCandidateStair(e, 'north', currentHalf))
            );
        case 'iSE':
            return (
                (isCornerCandidateStair(block, 'south', currentHalf) && isCornerCandidateStair(n, 'east', currentHalf)) ||
                (isCornerCandidateStair(block, 'east', currentHalf) && isCornerCandidateStair(w, 'south', currentHalf))
            );
        case 'iSW':
            return (
                (isCornerCandidateStair(block, 'south', currentHalf) && isCornerCandidateStair(n, 'west', currentHalf)) ||
                (isCornerCandidateStair(block, 'west', currentHalf) && isCornerCandidateStair(e, 'south', currentHalf))
            );

        // === Outer Corners ===
        case 'oNE':
            return (
                (isCornerCandidateStair(block, 'south', currentHalf) && isCornerCandidateStair(s, 'west', currentHalf)) ||
                (isCornerCandidateStair(block, 'west', currentHalf) && isCornerCandidateStair(w, 'south', currentHalf))
            );
        case 'oNW':
            return (
                (isCornerCandidateStair(block, 'south', currentHalf) && isCornerCandidateStair(s, 'east', currentHalf)) ||
                (isCornerCandidateStair(block, 'east', currentHalf) && isCornerCandidateStair(e, 'south', currentHalf))
            );
        case 'oSE':
            return (
                (isCornerCandidateStair(block, 'north', currentHalf) && isCornerCandidateStair(n, 'east', currentHalf)) ||
                (isCornerCandidateStair(block, 'east', currentHalf) && isCornerCandidateStair(e, 'north', currentHalf))
            );
        case 'oSW':
            return (
                (isCornerCandidateStair(block, 'north', currentHalf) && isCornerCandidateStair(n, 'west', currentHalf)) ||
                (isCornerCandidateStair(block, 'west', currentHalf) && isCornerCandidateStair(w, 'north', currentHalf))
            );
    }

    return false;
}

// Sets the corner state (weirdo_direction) based on matched pattern
function applyCornerStateToStair(block) {
    if (!block?.hasTag('pu_biomes:stair')) return;

    const states = block.permutation.getAllStates();
    let cornerState = 0;

    if (isMatchingCornerPattern(block, 'iNE')) cornerState = 1;
    else if (isMatchingCornerPattern(block, 'iNW')) cornerState = 2;
    else if (isMatchingCornerPattern(block, 'iSE')) cornerState = 3;
    else if (isMatchingCornerPattern(block, 'iSW')) cornerState = 4;
    else if (isMatchingCornerPattern(block, 'oNE')) cornerState = 5;
    else if (isMatchingCornerPattern(block, 'oNW')) cornerState = 6;
    else if (isMatchingCornerPattern(block, 'oSE')) cornerState = 7;
    else if (isMatchingCornerPattern(block, 'oSW')) cornerState = 8;

    if (states['pu_biomes:weirdo_direction'] === cornerState) return;

    states['pu_biomes:weirdo_direction'] = cornerState;
    const newPerm = BlockPermutation.resolve(block.typeId, states);
    block.setPermutation(newPerm);
}

// Updates the corner state for the block and its cardinal neighbors
function updateStairCornersAroundBlock(block) {
    [block, block.north(), block.south(), block.east(), block.west()].forEach(applyCornerStateToStair);
}

// Auto update on placement and destruction
world.afterEvents.playerPlaceBlock.subscribe(e => updateStairCornersAroundBlock(e.block));
world.afterEvents.playerBreakBlock.subscribe(e => updateStairCornersAroundBlock(e.block));