import { Vector3, world } from "@minecraft/server";
import Level from "../../Commandeer/level/level";
import pupeteer from "../../Commandeer/pupeteer";
import { teleportAgent, isAgentAt, getAgentLocation, getAgent } from "../../Commandeer/utils/agentUtils";
import { vector3, Vector3Add, Vector3ToCommandString } from "../../Commandeer/utils/vectorUtils";
import { mindKeeper, CURRENT_LEVEL } from "../../main";
import { MinecraftBlockTypes } from "../../vanilla-data/mojang-block";
import { checkBlockCondition } from "../../Commandeer/level/levelTypes";
import { mission2part2Conditions } from "../../levelConditions/mission2part2Conditions";
import { startLevel } from "../../Commandeer/utils/levelUtils";

const mission2part2CommandBlockPos: Vector3 = vector3(2461, -1, 4);

const mission2part2StartPosition: Vector3 = vector3(2467, -1, 11);
const mission2part2EndPosition: Vector3 = vector3(2481, -1, 11);

const mission2part2ResetCommandBlockPos: Vector3 = vector3(2457, -1, 4);
const mission2part2: Level = new Level(
  () => {
    pupeteer.sendWorldMessage("%message.mission2part2.started");
    pupeteer.setTitleTimed("%message.mission2part2.name", 2.5);
    startLevel(mission2part2CommandBlockPos);
    teleportAgent(mission2part2StartPosition);
    startLevel(mission2part2ResetCommandBlockPos);
  },
  () => {
    pupeteer.setActionBar("%message.mission2part2.make");
  },
  () => {
    pupeteer.clearActionBar();
    pupeteer.sendWorldMessage("%message.mission2part2.complete");
    pupeteer.setTitleTimed("%message.mission2part2.complete", 2.5);

    mindKeeper.increment(CURRENT_LEVEL);
  },
  () => {
    //Check if the agent is at the end position
    //than check if the level is correct
    let isComplete = false;
    let isAgentAtEndPosition = isAgentAt(mission2part2EndPosition);
    if (isAgentAtEndPosition) {
      let isCorrect = checkBlockCondition(mission2part2Conditions);

      if (isCorrect) {
        isComplete = true;
        world.sendMessage("Shit works");
      } else {
        isComplete = false;
        pupeteer.sendWorldMessage("%message.incorrect");
        pupeteer.setTitleTimed("%message.incorrect", 2.5);
        teleportAgent(mission2part2StartPosition);
        world
          .getDimension("overworld")
          .setBlockType(mission2part2ResetCommandBlockPos, MinecraftBlockTypes.RedstoneBlock);
      }
    }

    return isComplete;
  }
);

export default mission2part2;
