import { BlockPermutation, BlockVolume } from '@minecraft/server';
import WBVector3 from '../util/wbVector3';
function isBoneBlock(block) {
    return block !== undefined && (block.permutation.matches('minecraft:bone_block')
        || block.permutation.matches('twb_wisu:withered_bone_block')
        || block.permutation.matches('twb_wisu:corrupted_withered_bone_block'));
}
function placeFireOnTop(block) {
    const above = block.above();
    if (above?.isAir) {
        above.setType('twb_wisu:soul_fire');
    }
}
export default class Totem {
    constructor(lodestone, top, bottom, left, right, isRotated) {
        this.lodestone = lodestone;
        this.top = top;
        this.bottom = bottom;
        this.left = left;
        this.right = right;
        this.isRotated = isRotated;
    }
    static fromLodestone(lodestone) {
        const top = lodestone.above();
        const bottom = lodestone.below();
        if (!(top && bottom))
            return undefined;
        let left = lodestone.south();
        let right = lodestone.north();
        if (left && right) {
            const totem = new Totem(lodestone, top, bottom, left, right, false);
            if (totem.isSurroundedByBoneBlocks())
                return totem;
        }
        left = lodestone.west();
        right = lodestone.east();
        if (left && right) {
            const totem = new Totem(lodestone, top, bottom, left, right, true);
            if (totem.isSurroundedByBoneBlocks())
                return totem;
        }
        return undefined;
    }
    isSurroundedByBoneBlocks() {
        return (isBoneBlock(this.top)
            && isBoneBlock(this.bottom)
            && isBoneBlock(this.left)
            && isBoneBlock(this.right));
    }
    isBuiltByPlayer() {
        if (!this.lodestone.permutation.matches('minecraft:lodestone'))
            return false;
        if (!this.top.permutation.matches('minecraft:bone_block'))
            return false;
        if (!this.bottom.permutation.matches('minecraft:bone_block'))
            return false;
        if (!this.left.permutation.matches('minecraft:bone_block'))
            return false;
        if (!this.right.permutation.matches('minecraft:bone_block'))
            return false;
        return true;
    }
    isIntact() {
        if (!this.lodestone.permutation.matches('twb_wisu:withered_lodestone'))
            return false;
        if (!this.top.permutation.matches('twb_wisu:corrupted_withered_bone_block', { 'minecraft:block_face': 'up' }))
            return false;
        if (!this.bottom.permutation.matches('twb_wisu:corrupted_withered_bone_block', { 'minecraft:block_face': 'down' }))
            return false;
        if (!this.left.permutation.matches('twb_wisu:corrupted_withered_bone_block', { 'minecraft:block_face': this.isRotated ? 'west' : 'south' }))
            return false;
        if (!this.right.permutation.matches('twb_wisu:corrupted_withered_bone_block', { 'minecraft:block_face': this.isRotated ? 'east' : 'north' }))
            return false;
        return true;
    }
    setBuildCompleted() {
        this.lodestone.setPermutation(BlockPermutation.resolve('twb_wisu:withered_lodestone'));
        this.top.setPermutation(BlockPermutation.resolve('twb_wisu:corrupted_withered_bone_block', { 'minecraft:block_face': 'up' }));
        this.bottom.setPermutation(BlockPermutation.resolve('twb_wisu:corrupted_withered_bone_block', { 'minecraft:block_face': 'down' }));
        this.left.setPermutation(BlockPermutation.resolve('twb_wisu:corrupted_withered_bone_block', { 'minecraft:block_face': this.isRotated ? 'west' : 'south' }));
        this.right.setPermutation(BlockPermutation.resolve('twb_wisu:corrupted_withered_bone_block', { 'minecraft:block_face': this.isRotated ? 'east' : 'north' }));
        const center = this.lodestone.location;
        this.lodestone.dimension.fillBlocks(new BlockVolume(new WBVector3(1, -1, 1).add(center), new WBVector3(-1, 1, -1).add(center)), 'minecraft:air', {
            blockFilter: {
                includeTypes: [
                    'minecraft:short_grass',
                    'minecraft:stall_grass',
                    'minecraft:snow_layer',
                    'minecraft:beetroot',
                    'minecraft:big_dripleaf',
                    'minecraft:carrots',
                    'minecraft:deadbush',
                    'minecraft:fern',
                    'minecraft:mangrove_propagule',
                    'minecraft:moss_carpet',
                    'minecraft:potatoes',
                    'minecraft:oak_sapling',
                    'minecraft:spruce_sapling',
                    'minecraft:birch_sapling',
                    'minecraft:jungle_sapling',
                    'minecraft:acacia_sapling',
                    'minecraft:dark_oak_sapling',
                    'minecraft:cherry_sapling',
                    'minecraft:pale_oak_sapling',
                    'minecraft:spore_blossom',
                    'minecraft:reeds',
                    'minecraft:sweet_berry_bush',
                    'minecraft:wheat',
                    'minecraft:blue_orchid',
                    'minecraft:cornflower',
                    'minecraft:dandelion',
                    'minecraft:closed_eyeblossom',
                    'minecraft:open_eyeblossom',
                    'minecraft:lily_of_the_valley',
                    'minecraft:oxeye_daisy',
                    'minecraft:poppy',
                    'minecraft:sunflower',
                    'minecraft:crimson_fungus',
                    'minecraft:warped_fungus',
                    'minecraft:brown_mushroom',
                    'minecraft:red_mushroom',
                    'minecraft:nether_wart'
                ]
            }
        });
        // TODO: FX when player builds the totem
    }
    setActivated() {
        this.lodestone.setPermutation(BlockPermutation.resolve('twb_wisu:withered_lodestone', { 'twb_wisu:energized': true }));
        this.top.setPermutation(BlockPermutation.resolve('twb_wisu:corrupted_withered_bone_block', { 'twb_wisu:energized': true, 'minecraft:block_face': 'up' }));
        this.bottom.setPermutation(BlockPermutation.resolve('twb_wisu:corrupted_withered_bone_block', { 'twb_wisu:energized': true, 'minecraft:block_face': 'down' }));
        this.left.setPermutation(BlockPermutation.resolve('twb_wisu:corrupted_withered_bone_block', { 'twb_wisu:energized': true, 'minecraft:block_face': this.isRotated ? 'west' : 'south' }));
        this.right.setPermutation(BlockPermutation.resolve('twb_wisu:corrupted_withered_bone_block', { 'twb_wisu:energized': true, 'minecraft:block_face': this.isRotated ? 'east' : 'north' }));
        this.lodestone.dimension.playSound('twb_wisu:totem_activate', this.lodestone, { volume: 16 });
        this.lodestone.dimension.playSound('mob.wither.spawn', this.lodestone, { volume: 16 });
        this.lodestone.dimension.playSound('block.end_portal.spawn', this.lodestone, { volume: 16 });
        this.lodestone.dimension.spawnEntity('minecraft:lightning_bolt', { x: this.top.x + 0.5, y: this.top.y, z: this.top.z + 0.5 });
        placeFireOnTop(this.top);
        placeFireOnTop(this.left);
        placeFireOnTop(this.right);
        this.lodestone.dimension.fillBlocks(new BlockVolume(new WBVector3(2, 2, 2).add(this.lodestone), new WBVector3(-2, -2, -2).add(this.lodestone)), 'minecraft:air', {
            blockFilter: {
                includeTypes: [
                    'twb_wisu:wilt',
                    'twb_wisu:blooming_wilt',
                    'minecraft:snow_layer',
                    'minecraft:short_grass'
                ]
            }
        });
    }
}
