import { system, EasingType, world, ItemStack, MolangVariableMap, StructureAnimationMode, StructureRotation, StructureSaveMode } from "@minecraft/server";
import { getEntityRotation, stringBetween } from "../utilities/helpers";

export function cameraCutscene(player, tent, timeDifference) {
  let delay = timeDifference / 100;
  let delaySeconds = timeDifference / 100 / 20;
  let PlayerLocationLoc = player.getHeadLocation();

  player.runCommand("/inputpermission set @s camera disabled");
  player.runCommand("/inputpermission set @s movement disabled");
  let newLoc = getPositionInFrontOfPlayer(player, -3);
  let cameraPos = { x: newLoc.x, y: newLoc.y, z: newLoc.z };
  let currentRotationX = tent.getRotation().x;

  // Normalize rotationX to -90 to 90 degrees
  currentRotationX = ((currentRotationX + 90) % 180) - 90;

  let tentPos = tent.location; // Assuming tent.getLocation() gives x, y, z of the tent
  let deltaX = tentPos.x - cameraPos.x;
  let deltaZ = tentPos.z - cameraPos.z;

  // Calculate the angle in radians and convert to degrees
  let yawToTent = Math.atan2(deltaZ, deltaX) * (180 / Math.PI) - 90;

  let YRotation = yawToTent;

  let cameraRot = { x: currentRotationX + 25, y: YRotation };
  let cameraInitRot = { x: -90, y: YRotation };
  if (tent.typeId.includes("t3")) {
    let tentRot = getEntityRotation(tent);

    // //////////world.sendMessage(tentRot);
    switch (tentRot) {
      case 0:
        cameraPos = { x: tent.location.x + 1, y: tent.location.y + 1.5, z: tent.location.z - 4 };
        PlayerLocationLoc = { x: PlayerLocationLoc.x + 1.5, y: PlayerLocationLoc.y, z: PlayerLocationLoc.z };

        tentPos = player.location; // Assuming tent.getLocation() gives x, y, z of the tent
        deltaX = tentPos.x - cameraPos.x;
        deltaZ = tentPos.z - cameraPos.z;

        // Calculate the angle in radians and convert to degrees
        yawToTent = Math.atan2(deltaZ, deltaX) * (180 / Math.PI) - 90;

        YRotation = yawToTent;

        cameraRot = { x: currentRotationX + 15, y: YRotation };
        cameraInitRot = { x: -90, y: 90 };
        break;
      case 90:
        cameraPos = { x: tent.location.x + 4, y: tent.location.y + 1.5, z: tent.location.z + 1 };
        PlayerLocationLoc = { x: PlayerLocationLoc.x, y: PlayerLocationLoc.y, z: PlayerLocationLoc.z + 1.5 };

        tentPos = player.location; // Assuming tent.getLocation() gives x, y, z of the tent
        deltaX = tentPos.x - cameraPos.x;
        deltaZ = tentPos.z - cameraPos.z;

        // Calculate the angle in radians and convert to degrees
        yawToTent = Math.atan2(deltaZ, deltaX) * (180 / Math.PI) - 90;

        YRotation = yawToTent;

        cameraRot = { x: currentRotationX + 15, y: YRotation };
        cameraInitRot = { x: -90, y: 180 };
        break;
      case 180:
        cameraPos = { x: tent.location.x - 1, y: tent.location.y + 1.5, z: tent.location.z + 4 };
        PlayerLocationLoc = { x: PlayerLocationLoc.x - 1.5, y: PlayerLocationLoc.y, z: PlayerLocationLoc.z };

        tentPos = player.location; // Assuming tent.getLocation() gives x, y, z of the tent
        deltaX = tentPos.x - cameraPos.x;
        deltaZ = tentPos.z - cameraPos.z;

        // Calculate the angle in radians and convert to degrees
        yawToTent = Math.atan2(deltaZ, deltaX) * (180 / Math.PI) - 90;

        YRotation = yawToTent;

        cameraRot = { x: currentRotationX + 15, y: YRotation };
        cameraInitRot = { x: -90, y: -90 };
        break;
      case 270:
        cameraPos = { x: tent.location.x - 4, y: tent.location.y + 1.5, z: tent.location.z - 1 };
        PlayerLocationLoc = { x: PlayerLocationLoc.x, y: PlayerLocationLoc.y, z: PlayerLocationLoc.z - 1.5 };

        tentPos = player.location; // Assuming tent.getLocation() gives x, y, z of the tent
        deltaX = tentPos.x - cameraPos.x;
        deltaZ = tentPos.z - cameraPos.z;

        // Calculate the angle in radians and convert to degrees
        yawToTent = Math.atan2(deltaZ, deltaX) * (180 / Math.PI) - 90;

        YRotation = yawToTent;

        cameraRot = { x: currentRotationX + 15, y: YRotation };
        cameraInitRot = { x: -90, y: 0 };
        break;

      default:
        break;
    }

    // add camera pos regarding the tent rotation, left exit from the tent
  }

  //////////world.sendMessage("YRotation:" + YRotation.toString());
  system.run(() => {
    player.camera.setCamera("minecraft:free", {
      location: cameraPos,
      rotation: cameraInitRot,
    });

    system.waitTicks(delay / 2).then(() => {
      player.camera.setCamera("minecraft:free", {
        location: PlayerLocationLoc,
        rotation: cameraRot,
        easeOptions: {
          easeTime: delaySeconds / 2,
          easeType: EasingType.InOutQuad,
        },
      });
    });
    system.waitTicks(delay).then(() => {
      player.camera.clear();
    });
  });
}

function getPositionInFrontOfPlayer(player, distance) {
  const rotation = player.getRotation();
  const yaw = rotation.y * (Math.PI / 180); // Convert yaw from degrees to radians

  // Calculate the x- and z-coordinates in front of the player
  const inFrontX = player.getHeadLocation().x + Math.sin(yaw) * distance;
  const inFrontZ = player.getHeadLocation().z - Math.cos(yaw) * distance;

  return {
    x: inFrontX,
    y: player.getHeadLocation().y,
    z: inFrontZ,
  };
}
function getPositionInFrontOfTent(tent, distance) {
  const rotation = tent.getRotation();
  const yaw = rotation.y * (Math.PI / 180); // Convert yaw from degrees to radians

  // Calculate the x- and z-coordinates in front of the tent
  const inFrontX = tent.location.x + Math.sin(yaw) * distance;
  const inFrontZ = tent.location.z - Math.cos(yaw) * distance;

  return {
    x: inFrontX,
    y: tent.location.y,
    z: inFrontZ,
  };
}

function getPositionBehindPlayer(player, distance) {
  const rotation = player.getRotation();
  const yaw = rotation.y * (Math.PI / 180); // Konvertiere Yaw von Grad zu Bogenmaß

  // Berechne die x- und z-Koordinaten hinter dem Spieler
  const behindX = player.getHeadLocation().x + Math.sin(yaw) * distance;
  const behindZ = player.getHeadLocation().z - Math.cos(yaw) * distance;

  return {
    x: behindX,
    y: player.getHeadLocation().y - 1,
    z: behindZ,
  };
}

function getOffsetFromEntityRotation(entity, offsetDistanceX, offsetDistanceZ) {
  const rotationYaw = entity.getRotation().y * (Math.PI / 180); // Convert yaw from degrees to radians

  // Calculate the offset in the X and Z directions based on the entity's rotation
  const offsetX = Math.sin(rotationYaw) * offsetDistanceX; // X offset based on yaw
  const offsetZ = -Math.cos(rotationYaw) * offsetDistanceZ; // Z offset based on yaw

  return { x: offsetX, z: offsetZ };
}
