import { Player } from "@minecraft/server";
import { world, overworld } from "./Eternal/Constants";
import { runInterval, runTimeout, test } from "./Eternal/Utils";

Player.prototype.startTheiaTP = function () {
  this.commandRunAsync(
    `tag @s add startTheiaTp`,
    `title @s title eternal.theia_portal`,
    "effect @s nausea 10 255 true"
  );

  this.runInterval = runInterval(() => {
    if (!this.hasTag("inPortal")) {
      this.cancelTheiaTP();
    }
  });
  this.runTimeout = runTimeout(() => {
    this.runInterval.dispose();
    this.commandRunAsync(
      `event entity @e[type=eternal:pointer] e:add-instant_despawn`,
      `title @s actionbar §r`,
      `playanimation @s animation.model.new`,
      "playsound random.portal @s",
      "effect @s blindness 15 255 true",
      "event entity @s[tag=!intro_title] e:set-is_teleporting-true",
      "effect @s[tag=intro_title] invisibility 15 255 true",
      "tag @s add teleporting",
      "tag @s remove finished_teleporting",
      `tag @s remove startTheiaTp`,
      "function eternal/use_portal/teleport"
    );
    runTimeout(() => {
      this.commandRunAsync(`title @s title eternal.hide_ui`);
    }, 3);
    runTimeout(() => {
      this.commandRunAsync(
        "execute as @a[scores={s-intro=26},tag=intro_title,tag=!in_overworld] run scoreboard players set @a s-intro 731",
        "execute as @s[scores={s-intro=26},tag=!intro_title] run camera @s fade time 0 2 0",
        "execute as @s[scores={s-intro=26},tag=!intro_title] run title @s title eternal.hide_ui",
        "execute as @s[scores={s-intro=26},tag=!intro_title] run effect @s blindness 0",
        "execute as @s[scores={s-intro=26},tag=!intro_title] run tag @a add cutscene1",
        "execute unless entity @s[scores={s-intro=26}] if entity @s[tag=!intro_title] run title @s clear",
        "execute unless entity @s[scores={s-intro=26}] if entity @s[tag=!intro_title] run effect @s blindness 0 0 true",
        "execute unless entity @s[scores={s-intro=26}] if entity @s[tag=!intro_title] run effect @s invisibility 0 0 true",
        "execute if entity @s[scores={s-intro=26},tag=intro_title] run title @s clear",
        "execute if entity @s[scores={s-intro=26},tag=intro_title] run effect @s blindness 0 0 true",
        "execute if entity @s[scores={s-intro=731},tag=intro_title] run title @s title clear",
        "execute if entity @s[scores={s-intro=731},tag=intro_title] run effect @s blindness 0 0 true",

        "execute if entity @s[scores={s-intro=26},tag=!intro_title] run gamemode spectator @s",

        "tag @s remove teleporting",
        "event entity @s e:set-is_teleporting-false",
        "tag @s add finished_teleporting"
      );
      this.cancelTheiaTP();
    }, 110);
  }, 80);
};

Player.prototype.cancelTheiaTP = function () {
  this.commandRunAsync(
    `effect @s nausea 0 255 true`,
    `execute unless entity @s[scores={s-intro=26},tag=intro_title] run title @s title §r`,
    `tag @s remove startTheiaTp`
  );
  this.runInterval.dispose();
  this.runTimeout.dispose();
};

runInterval(() => {
  overworld.commandRunAsync(
    `tag @a remove inPortal`,
    `execute as @a positioned as @s if block ~~~ eternal:theia_portal_north run tag @s[tag=!inPortal] add inPortal`
  );
  const players = overworld.fetchEntities("@a[tag=inPortal]");

  players.forEach((player) => {
    if (!player.hasTag("startTheiaTp")) player.startTheiaTP();
  });
});
