import { Vector3, world } from "@minecraft/server";
import Level from "../Commandeer/level/level";
import { vector3 } from "../Commandeer/utils/vectorUtils";
import { levelIntroConditions } from "../levelConditions/levelIntro";
import { CURRENT_LEVEL, mindKeeper } from "../main";
import Pupeteer from "../Commandeer/pupeteer";
import { MinecraftBlockTypes } from "../vanilla-data/mojang-block";

// const levelIntroCommandBlockPos: Vector3 = vector3(58, 66, 276);
// const levelIntroStartPosition: Vector3 = vector3(28, 70, 269);
// const levelIntroEndPosition: Vector3 = vector3(39, 70, 269);
const buttonPositions: Vector3[] = [
  vector3(2471, 11, 106),
  vector3(2469, 11, 106),
  vector3(2468, 11, 106),
  vector3(2464, 11, 106),
];

const blockPositions: Vector3[] = [
  vector3(2471, 12, 108),
  vector3(2469, 12, 108),
  vector3(2468, 12, 108),
  vector3(2464, 12, 108),
];

const currentBlockSequence: string[] = [];

const blockCycle: string[] = [
  // MinecraftBlockTypes.RedstoneBlock,
  // MinecraftBlockTypes.GoldBlock,
  // MinecraftBlockTypes.DiamondBlock,
  MinecraftBlockTypes.EmeraldBlock,
  MinecraftBlockTypes.LapisBlock,
];

const buttonPressed: boolean[] = buttonPositions.map(() => false);

const levelIntro: Level = new Level(
  () => {
    Pupeteer.sendWorldMessage("%message.intro.started");
    Pupeteer.setTitleTimed("%message.intro.started", 2.5);
    // startLevel(levelIntroCommandBlockPos);
    // teleportAgent(levelIntroStartPosition);
    blockPositions.forEach((pos) => {
      const block = world.getDimension("overworld").getBlock(pos);
      const index = blockPositions.indexOf(pos);
      const blockType = block!.type;
      currentBlockSequence[index] = blockType.id;
    });
  },
  () => {
    Pupeteer.setActionBar("%message.intro.make");

    buttonPositions.forEach((pos) => {
      const block = world.getDimension("overworld").getBlock(pos);
      if (!block) return;
      const index = buttonPositions.indexOf(pos);
      const prevState = buttonPressed[index];
      const currentState = block!.getRedstonePower()! > 0;

      if (currentState && !prevState) {
        buttonPressed[index] = true;
        //NextBlock
        const nextBlock = currentBlockSequence[index];
        let nextIndex = blockCycle.indexOf(nextBlock);
        nextIndex = (nextIndex + 1) % blockCycle.length;
        currentBlockSequence[index] = blockCycle[nextIndex];
        //Update the block
        const blockPos = blockPositions[index];
        world.getDimension("overworld").getBlock(blockPos)!.setType(blockCycle[nextIndex]);
      }

      if (!currentState && prevState) {
        buttonPressed[index] = false;
      }
    });
  },
  () => {
    Pupeteer.clearActionBar();
    world.sendMessage("%message.intro.done");
    Pupeteer.setTitleTimed("%message.intro.done", 2.5);

    mindKeeper.increment(CURRENT_LEVEL);
  },
  () => {
    let isComplete = true;
    levelIntroConditions.conditions.forEach((condition) => {
      const blockInworld = world.getDimension("overworld").getBlock(condition.position);
      if (blockInworld?.type.id !== condition.block) {
        isComplete = false;
      }
    });
    if (isComplete) {
      return true;
    }
    return false;
  },
  () => {
    blockPositions.forEach((pos) => {
      const block = world.getDimension("overworld").getBlock(pos);
      const randomBlock = blockCycle[Math.floor(Math.random() * blockCycle.length)];
      block!.setType(randomBlock);
    });
  }
);

export default levelIntro;
