import * as mc from "@minecraft/server"
import { Vector2, Vector3 } from "./vector"

export class playerCamera {
  /** @param {mc.Player} player */
  constructor(player) {
    if (!player || !player.isValid()) {
      throw new Error("Invalid player instance.");
    }
    this.player = player;
  }

  fade(color = [0, 0, 0], time = { in: 0, hold: 0, out: 0 }) {
    if (!this.player.isValid()) return;

    this.player.camera.fade({
      fadeColor: {
        red: color[0] / 255,
        green: color[1] / 255,
        blue: color[2] / 255,
      },
      fadeTime: {
        fadeInTime: time.in,
        holdTime: time.hold,
        fadeOutTime: time.out,
      },
    });
  }

  fakeZoom(power, duration) {
    if (!this.player.isValid()) return;

    // Uncomment if needed
    // this.player.addEffect("slowness", duration * 20, { showParticles: false, amplifier: power });
  }

  adjust() {
    if (!this.player.isValid()) return;

    this.player.teleport(this.player.location, { rotation: new Vector2(0, this.player.getRotation().y) });
  }

  removeAllGui() {
    if (!this.player.isValid()) return;

    this.player.onScreenDisplay.hideAllExcept();
  }

  setRelativeFree(pos = new Vector3(0, 0, 0), posFacing = new Vector3(0, 0, 0), ease = 0) {
    if (!this.player.isValid()) return;

    this.player.runCommand(`
      camera @s set minecraft:free
      ease ${ease} linear 
      pos ^${pos.x !== 0 ? pos.x : ""}^${pos.y !== 0 ? pos.y : ""}^${pos.z !== 0 ? pos.z : ""}
      facing ^${posFacing.x !== 0 ? posFacing.x : ""}^${posFacing.y !== 0 ? posFacing.y : ""}^${posFacing.z !== 0 ? posFacing.z : ""}
    `);
  }

  clear() {
    if (!this.player.isValid()) return;

    this.player.camera.clear();
    this.player.onScreenDisplay.resetHudElements();
  }
}

export class playerInput {
  constructor(player) {

    this.player = player


  }
  remove(string) {

    switch(string) {

      case "movement":
      this.player.inputPermissions.movementEnabled = false
      break
      case "camera":
      this.player.inputPermissions.cameraEnabled = false
      break
      default:
      console.warn("Parametros errados!")
      break


    }
    

  }
  removeAllInputs() {
    
    this.player.inputPermissions.cameraEnabled = false
    this.player.inputPermissions.movementEnabled = false
    

  }
  clear() {
    
    this.player.inputPermissions.cameraEnabled = true
    this.player.inputPermissions.movementEnabled = true
    

  }


}

export class PlayerEffectLoop {
  constructor(player) {
    this.player = player;
    this.effectLoopId = null;
  }

  startEffectLoop(effectName, duration, amplifier, interval) {
    if (this.effectLoopId === null) {
      if (!this.isValidEffect(effectName)) {
        console.error(`Effect ${effectName} is not valid.`);
        return;
      }

      this.effectLoopId = mc.system.runInterval(() => {
        if (this.player.isValid()) {
          this.addEffect(effectName, duration, { showParticles: false, amplifier: amplifier });
        } else {
          this.stopEffectLoop(); // Stop loop if player is no longer valid
        }
      }, interval * 20);
    }
  }

  addEffect(effectName, duration, effectOptions) {
    if (this.player.isValid()) {
      this.player.addEffect(effectName, duration * 20, effectOptions);
      console.log(`Efeito ${effectName} aplicado por ${duration} segundos.`);
    }
  }

  stopEffectLoop() {
    if (this.effectLoopId !== null) {
      mc.system.clearRun(this.effectLoopId);
      this.effectLoopId = null;
    }
  }

  isValidEffect(effectName) {
    // Implementasi untuk memeriksa validitas effectName
    const validEffects = ['effect1', 'effect2', 'effect3']; // Ganti dengan daftar efek yang valid
    return validEffects.includes(effectName);
  }
}








export class playersCamera {

    /** @param { mc.Player } mainPlayer **/
    constructor(players, mainPlayer) {
      this.players = [...players, mainPlayer];
      this.playerMain = mainPlayer;
    }
  
    fade(color = [0, 0, 0], time = { in: 0, hold: 0, out: 0 }) {
      this.players.forEach(player => {
        player.camera.fade({
          "fadeColor": { "red": color[0] / 255, "green": color[1] / 255, "blue": color[2] / 255 },
          "fadeTime": { "fadeInTime": time.in, "holdTime": time.hold, "fadeOutTime": time.out }
        });
      });
    }
  
    fakeZoom(power, duration) {
      if (!mc.world.getDynamicProperty("ld_aw.:camera")) return;
  
      this.players.forEach(player => {
        player.addEffect("slowness", duration * 20, { showParticles: false, amplifier: power });
      });
    }
  
    adjust() {
      if (!mc.world.getDynamicProperty("ld_aw.:camera")) return;
  
      this.players.forEach(player => {
        player.teleport(player.location, { rotation: new Vector2(0, player.getRotation().y) });
      });
    }
  
    removeAllGui() {
      if (!mc.world.getDynamicProperty("ld_aw.:camera")) return;
  
      this.players.forEach(player => {
        player.onScreenDisplay.hideAllExcept();
      });
    }
  
    setRelativeFree(pos = new Vector3(0, 0, 0), posFacing = new Vector3(0, 0, 0), ease = 0) {
      if (!mc.world.getDynamicProperty("ld_aw.:camera")) return;
  
      this.players.forEach(player => {
        player.runCommand(`execute at @a[name="${this.playerMain.nameTag}"] run camera @s set minecraft:free ease ${ease} linear pos ^${pos.x || ""} ^${pos.y || ""} ^${pos.z || ""} facing ^${posFacing.x || ""} ^${posFacing.y || ""} ^${posFacing.z || ""}`);
      });
    }
  
    clear() {
      this.players.forEach(player => {
        player.camera.clear();
        player.onScreenDisplay.resetHudElements();
      });
    }
  }
  