import * as mc from "@minecraft/server";
import * as vector from "../utils/vector";
import * as blockEvents from "../blockEvents";

const toBlockRot = {
  0: "south",
  90: "west",
  "-270": "west",
  180: "north",
  "-180": "north",
  "-90": "east",
  270: "east",
};

const SandBlockStates = [
  {
    "minecraft:cardinal_direction": "north",
    "hf_mzs:corner": false,
    "hf_mzs:north_east": false,
    "hf_mzs:north_west": false,
    "hf_mzs:south_east": false,
    "hf_mzs:south_west": false,
  },
  {
    "minecraft:cardinal_direction": "east",
    "hf_mzs:corner": false,
    "hf_mzs:north_east": false,
    "hf_mzs:north_west": false,
    "hf_mzs:south_east": false,
    "hf_mzs:south_west": false,
  },
  {
    "minecraft:cardinal_direction": "north",
    "hf_mzs:corner": true,
    "hf_mzs:north_east": true,
    "hf_mzs:north_west": false,
    "hf_mzs:south_east": false,
    "hf_mzs:south_west": false,
  },
  {
    "minecraft:cardinal_direction": "north",
    "hf_mzs:corner": true,
    "hf_mzs:north_east": false,
    "hf_mzs:north_west": true,
    "hf_mzs:south_east": false,
    "hf_mzs:south_west": false,
  },
  {
    "minecraft:cardinal_direction": "north",
    "hf_mzs:corner": true,
    "hf_mzs:north_east": false,
    "hf_mzs:north_west": false,
    "hf_mzs:south_east": true,
    "hf_mzs:south_west": false,
  },
  {
    "minecraft:cardinal_direction": "north",
    "hf_mzs:corner": true,
    "hf_mzs:north_east": false,
    "hf_mzs:north_west": false,
    "hf_mzs:south_east": false,
    "hf_mzs:south_west": true,
  },
];

function cycleSandState(block) {
  let variant = 0;
  const isCorner = block.permutation.getState("hf_mzs:corner");
  if (isCorner) {
    const north_east = block.permutation.getState("hf_mzs:north_east");
    if (north_east) {
      variant = 2;
    }
    const north_west = block.permutation.getState("hf_mzs:north_west");
    if (north_west) {
      variant = 3;
    }
    const south_east = block.permutation.getState("hf_mzs:south_east");
    if (south_east) {
      variant = 4;
    }
    const south_west = block.permutation.getState("hf_mzs:south_west");
    if (south_west) {
      variant = 5;
    }
  } else {
    const dir = block.permutation.getState("minecraft:cardinal_direction");
    if (dir === "north" || dir === "south") variant = 0;
    else if (dir === "east" || dir === "west") variant = 1;
  }
  let newVariant = (variant + 1) % SandBlockStates.length;
  return SandBlockStates[newVariant];
}

export function useSandRake(event) {
  const item = event.itemStack;
  const player = event.source;
  const block = event.block;

  if (
    item.typeId === "hf_mzs:sand_rake" &&
    (block.typeId === "minecraft:sand" || block.typeId === "minecraft:red_sand" || block.typeId === "hf_mzs:raked_sand" || block.typeId === "hf_mzs:red_raked_sand")
  ) {
    if (!player.lastTick) {
      player.lastTick = 0;
    }
    if (mc.system.currentTick - player.lastTick <= 5) return;
    player.lastTick = mc.system.currentTick;

    mc.system.run(() => {
      const rot = Math.round(player.getRotation().y / 90) * 90;
      if (!player.isSneaking) {
        switch (block.typeId) {
          case "minecraft:sand":
            block.setPermutation(mc.BlockPermutation.resolve("hf_mzs:raked_sand", { "minecraft:cardinal_direction": toBlockRot[rot] }));
            block.dimension.playSound("use.gravel", block.location, { volume: 1.0, pitch: 1.0 });
            blockEvents.rakedSandUpdate(block, player);
            break;
          case "minecraft:red_sand":
            block.setPermutation(mc.BlockPermutation.resolve("hf_mzs:red_raked_sand", { "minecraft:cardinal_direction": toBlockRot[rot] }));
            block.dimension.playSound("use.gravel", block.location, { volume: 1.0, pitch: 1.0 });
            blockEvents.rakedSandUpdate(block, player);
            break;
          case "hf_mzs:raked_sand":
            block.setPermutation(mc.BlockPermutation.resolve("hf_mzs:raked_sand", { "minecraft:cardinal_direction": toBlockRot[rot] }));
            block.dimension.playSound("use.gravel", block.location, { volume: 1.0, pitch: 1.0 });
            blockEvents.rakedSandUpdate(block, player);
            break;
          case "hf_mzs:red_raked_sand":
            block.setPermutation(mc.BlockPermutation.resolve("hf_mzs:red_raked_sand", { "minecraft:cardinal_direction": toBlockRot[rot] }));
            block.dimension.playSound("use.gravel", block.location, { volume: 1.0, pitch: 1.0 });
            blockEvents.rakedSandUpdate(block, player);
            break;
        }
      } else {
        switch (block.typeId) {
          case "minecraft:sand":
            block.setPermutation(mc.BlockPermutation.resolve("hf_mzs:raked_sand", { "minecraft:cardinal_direction": toBlockRot[rot] }));
            block.dimension.playSound("use.gravel", block.location, { volume: 1.0, pitch: 1.0 });
            blockEvents.rakedSandUpdate(block, player);
            break;
          case "minecraft:red_sand":
            block.setPermutation(mc.BlockPermutation.resolve("hf_mzs:red_raked_sand", { "minecraft:cardinal_direction": toBlockRot[rot] }));
            block.dimension.playSound("use.gravel", block.location, { volume: 1.0, pitch: 1.0 });
            blockEvents.rakedSandUpdate(block, player);
            break;
          case "hf_mzs:raked_sand":
            block.setPermutation(mc.BlockPermutation.resolve("hf_mzs:raked_sand", cycleSandState(block)));
            block.dimension.playSound("use.gravel", block.location, { volume: 1.0, pitch: 1.0 });
            break;
          case "hf_mzs:red_raked_sand":
            block.setPermutation(mc.BlockPermutation.resolve("hf_mzs:red_raked_sand", cycleSandState(block)));
            block.dimension.playSound("use.gravel", block.location, { volume: 1.0, pitch: 1.0 });
            break;
        }
      }

      if (player.getGameMode() != mc.GameMode.creative) {
        if (item.getComponent("minecraft:durability").damage < item.getComponent("minecraft:durability").maxDurability - 1) {
          const newItem = item.clone();
          newItem.getComponent("minecraft:durability").damage += 1;

          player.getComponent("minecraft:equippable").setEquipment(mc.EquipmentSlot.Mainhand, newItem);
        } else {
          player.playSound("random.break", { volume: 1.0, pitch: 1.0 });
          player.getComponent("minecraft:equippable").setEquipment(mc.EquipmentSlot.Mainhand, null);
        }
      }
    });
  }
}
