import { BlockType, Vector3, world } from "@minecraft/server";
import Level from "../Commandeer/level/level";
import { leverOn } from "../Commandeer/level/levelTypes";
import { teleportAgent, isAgentAt } from "../Commandeer/utils/agentUtils";
import { startLevel } from "../Commandeer/utils/levelUtils";
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),
];

let currentBlockSequence: string[] = [];
let currentBlockSeuqenceIndex: number[] = [];

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

let 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) => {
      let block = world.getDimension("overworld").getBlock(pos);
      let index = blockPositions.indexOf(pos);
      let blockType = block!.type;
      currentBlockSequence[index] = blockType.id;
    });
  },
  () => {
    Pupeteer.setActionBar("%message.intro.make");

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

      if (currentState && !prevState) {
        buttonPressed[index] = true;
        //NextBlock
        let nextBlock = currentBlockSequence[index];
        let nextIndex = blockCycle.indexOf(nextBlock);
        nextIndex = (nextIndex + 1) % blockCycle.length;
        currentBlockSequence[index] = blockCycle[nextIndex];
        //Update the block
        let 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 counter = 0;
    let isComplete = true;
    levelIntroConditions.conditions.forEach((condition) => {
      let blockInworld = world.getDimension("overworld").getBlock(condition.position);
      if (blockInworld?.type.id !== condition.block) {
        isComplete = false;
        counter++;
      }
    });
    if (isComplete) {
      return true;
    }
    return false;
  },
  () => {
    blockPositions.forEach((pos) => {
      let block = world.getDimension("overworld").getBlock(pos);
      let randomBlock = blockCycle[Math.floor(Math.random() * blockCycle.length)];
      block!.setType(randomBlock);
    });
  }
);

export default levelIntro;
