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 { mission2part1Conditions } from "../../levelConditions/mission2part1Conditions";
import { checkBlockCondition } from "../../Commandeer/level/levelTypes";
import { startLevel } from "../../Commandeer/utils/levelUtils";

const mission2part1CommandBlockPos: Vector3 = vector3(2460, -1, 4);

const mission2part1StartPosition: Vector3 = vector3(2455, -1, 11);
const mission2part1EndPosition: Vector3 = vector3(2465, -1, 11);

const mission2part1ResetCommandBlockPos: Vector3 = vector3(2456, -1, 4);
const mission2part1: Level = new Level(
  () => {
    pupeteer.sendWorldMessage("%message.mission2part1.started");
    pupeteer.setTitleTimed("%message.mission2part1.name", 2.5);
    startLevel(mission2part1CommandBlockPos);
    teleportAgent(mission2part1StartPosition);
    startLevel(mission2part1ResetCommandBlockPos);
  },
  () => {
    pupeteer.setActionBar("%message.mission2part1.make");
  },
  () => {
    pupeteer.clearActionBar();
    pupeteer.sendWorldMessage("%message.mission2part1.complete");
    pupeteer.setTitleTimed("%message.mission2part1.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(mission2part1EndPosition);
    if (isAgentAtEndPosition) {
      let isCorrect = checkBlockCondition(mission2part1Conditions);

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

    return isComplete;
  }
);

export default mission2part1;
