import { world, BlockPermutation, Vector, system, Block } from "@minecraft/server"

// Array defining pairs of bottom and top blocks for double plants
const tallBottomDoors = [
    {
        id: "better_on_bedrock:chorus_door_bottom",
        topBit: {
            id: "better_on_bedrock:chorus_door_top"
        }
    },
    {
        id: "better_on_bedrock:vacant_door_bottom",
        topBit: {
            id: "better_on_bedrock:vacant_door_top"
        }
    },
    {
        id: "better_on_bedrock:voiding_door_bottom",
        topBit: {
            id: "better_on_bedrock:voiding_door_top"
        }
    }
]

world.afterEvents.playerPlaceBlock.subscribe(ev => {
    const above = ev.block.above();

    
    // Find a matching bottom door based on the placed block's permutation
    const matchingBottomDoors = tallBottomDoors.find((door) => ev.block.permutation.matches(door.id));
    const left = ev.block.dimension.getBlock(new Vector(ev.block.location.x-1, ev.block.location.y, ev.block.location.z)).typeId==matchingBottomDoors.id
    const right = ev.block.dimension.getBlock(new Vector(ev.block.location.x+1, ev.block.location.y, ev.block.location.z)).typeId==matchingBottomDoors.id
    const left1 = ev.block.dimension.getBlock(new Vector(ev.block.location.x, ev.block.location.y, ev.block.location.z-1)).typeId==matchingBottomDoors.id
    const right2 = ev.block.dimension.getBlock(new Vector(ev.block.location.x, ev.block.location.y, ev.block.location.z+1)).typeId==matchingBottomDoors.id
    ev.block.setPermutation(ev.block.permutation.withState(`test:door_open`, false))
    ev.block.setPermutation(ev.block.permutation.withState(`test:mirrored`, false))

    // Check if the above block is air to place the corresponding top door
    if (matchingBottomDoors != undefined && above.isAir) {
        // Access the corresponding top door ID directly from the structure
        const matchingBottomDoorsId = matchingBottomDoors.topBit.id;

        // Place the corresponding top door
        above.setPermutation(BlockPermutation.resolve(matchingBottomDoorsId));
        above.setPermutation(BlockPermutation.resolve(matchingBottomDoorsId, ev.block.permutation.getAllStates()));
        if(matchingBottomDoors  != undefined && left || right || left1 || right2){
            ev.block.setPermutation(ev.block.permutation.withState(`test:door_open`, false))
            ev.block.setPermutation(ev.block.permutation.withState(`test:mirrored`, true))
            above.setPermutation(BlockPermutation.resolve(matchingBottomDoorsId, ev.block.permutation.getAllStates()));
        }
    } else if (matchingBottomDoors != undefined && !above.isAir) {
        // If the above block is not air, break the placed bottom block
        const { x, y, z } = ev.block.location;
        ev.dimension.runCommand(`setblock ${x} ${y} ${z} air[] destroy`);
    }
})

world.afterEvents.playerBreakBlock.subscribe(ev => {
    // Find a matching top door based on the broken block's permutation
    const matchingTopDoor = tallBottomDoors.find((door) => ev.brokenBlockPermutation.matches(door.topBit.id));

    // Break the corresponding bottom block if a matching top door is found
    if (matchingTopDoor) {
        const { x, y, z } = ev.block.location;
        ev.dimension.runCommand(`setblock ${x} ${y - 1} ${z} air[] destroy`);
    }
})

world.afterEvents.playerInteractWithBlock.subscribe((ev) => {
    switch (ev.block.typeId) {
      case "better_on_bedrock:chorus_door_bottom":
        const permutation = ev.block.permutation;
        const state = permutation.getState("test:door_open");
        ev.block.setPermutation(permutation.withState("test:door_open", !state));
        ev.block.dimension.runCommand('playsound random.door_close @p')
        const above = ev.block.above();
        above.setPermutation(above.permutation.withState("test:door_open", !state));

        

 if(permutation.getState("test:door_open") == false) {
    ev.block.dimension.runCommand('playsound random.door_open @p')
 }
 break;
 case "better_on_bedrock:vacant_door_bottom":
    const permutations = ev.block.permutation;
    const states = permutations.getState("test:door_open");
    const aboves = ev.block.above();
    ev.block.setPermutation(permutations.withState("test:door_open", !states));
    ev.block.dimension.runCommand('playsound random.door_close @p')
    aboves.setPermutation(aboves.permutation.withState("test:door_open", !states));

    

if(permutation.getState("test:door_open") == false) {
ev.block.dimension.runCommand('playsound random.door_open @p')
}
      break;
      case "better_on_bedrock:voiding_door_bottom":
        const permutationss = ev.block.permutation;
        const statess = permutationss.getState("test:door_open");
        const abovess = ev.block.above();
        ev.block.setPermutation(permutationss.withState("test:door_open", !statess));
        ev.block.dimension.runCommand('playsound random.door_close @p')
        abovess.setPermutation(abovess.permutation.withState("test:door_open", !statess));
    
        
    
    if(permutation.getState("test:door_open") == false) {
    ev.block.dimension.runCommand('playsound random.door_open @p')
    }
          break;
    };
    switch (ev.block.typeId) {
        case "better_on_bedrock:chorus_door_top":
          const permutation = ev.block.permutation;
          const state = permutation.getState("test:door_open");
          ev.block.setPermutation(permutation.withState("test:door_open", !state));
          ev.block.dimension.runCommand('playsound random.door_close @p')
          const above = ev.block.below();
          above.setPermutation(above.permutation.withState("test:door_open", !state));
          if(permutation.getState("test:door_open") == false) {
            ev.block.dimension.runCommand('playsound random.door_open @p')
         }
         break;
         case "better_on_bedrock:vacant_door_top":
            const permutations = ev.block.permutation;
          const states = permutations.getState("test:door_open");
          ev.block.setPermutation(permutations.withState("test:door_open", !states));
          ev.block.dimension.runCommand('playsound random.door_close @p')
          const aboves = ev.block.below();
          aboves.setPermutation(aboves.permutation.withState("test:door_open", !states));
          if(permutation.getState("test:door_open") == false) {
            ev.block.dimension.runCommand('playsound random.door_open @p')
         }
        break;
        case "better_on_bedrock:voiding_door_top":
            const permutationss = ev.block.permutation;
          const statess = permutationss.getState("test:door_open");
          ev.block.setPermutation(permutationss.withState("test:door_open", !statess));
          ev.block.dimension.runCommand('playsound random.door_close @p')
          const abovess = ev.block.below();
          abovess.setPermutation(abovess.permutation.withState("test:door_open", !statess));
          if(permutation.getState("test:door_open") == false) {
            ev.block.dimension.runCommand('playsound random.door_open @p')
         }
        break;
      };
  });
