import { Player } from "@minecraft/server";
import { world, overworld } from "./Eternal/Constants";
import { runInterval, runTimeout, display } 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 10 255 true",
      "effect @s invisibility 10 255 true",
      "event entity @s[tag=!intro_title] e:set-is_teleporting-true",
      `tag @s remove startTheiaTp`
      // "function eternal/use_portal/teleport"
    );
    runTimeout(() => {
      this.commandRunAsync(
        "tag @s[x=50000,y=200,z=0,rm=5001] remove in_overworld",
        "tag @s[x=50000,y=200,z=0,r=5000] add in_overworld"
      );

      this.runInterval2 = runInterval(() => {
        this.commandRunAsync(
          // Give time to portal particle
          `title @s title eternal.hide_ui`,

          "execute as @s[tag=!in_overworld] run teleport 50001 108 2",
          "execute as @s[tag=in_overworld] run teleport -63 115 -413"
        );
      });
    }, 3);
    runTimeout(() => {
      this.runInterval2.dispose();
      runTimeout(() => {
        this.commandRunAsync(
          "tp @s[tag=in_overworld] -63.00 105.00 -415.88 180",
          "tp @s[tag=!in_overworld] 50001.03 98.00 0.57 180 -10",
          "effect @s invisibility 0 255 true",
          "effect @s blindness 0 255 true",
          "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 tag @a add cutscene1",
          "event entity @s e:set-is_teleporting-false"
        );
        this.cancelTheiaTP();
      }, 2);
    }, 110);
  }, 80);
};

Player.prototype.cancelTheiaTP = function () {
  this.commandRunAsync(`effect @s nausea 0 255 true`, `title @s title §r`, `tag @s remove startTheiaTp`);
  this.runInterval.dispose();
  this.runTimeout.dispose();
};

runInterval(() => {
  overworld.commandRunAsync(
    `tag @a remove inPortal`,
    `tag @a[x=-63,y=107,z=-413,dx=-1,dy=1,dz=0,tag=!inPortal] add inPortal`,
    `tag @a[x=50001,y=100,z=2,dx=-1,dy=1,dz=0,tag=!inPortal] add inPortal`

    // Portal particle
    // "execute as @a[tag=teleporting,tag=!in_overworld] run teleport 50001 108 2",
    // "execute as @a[tag=teleporting,tag=in_overworld] run teleport -63 115 -413",

    // "execute as @a[tag=!teleporting,tag=in_overworld,tag=finished_teleporting] run teleport -63.00 105.00 -415.88 ",
    // "execute as @a[tag=!teleporting,tag=!in_overworld,tag=finished_teleporting] run teleport 50001.03 98.00 0.57 180 -10",
    // "execute as @a[tag=!teleporting,tag=finished_teleporting] run tag @s remove finished_teleporting"
  );
  const players = overworld.fetchEntities("@a[tag=inPortal]");

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