import * as mc from "@minecraft/server"
import { Vector3 } from "./VectorMath/index.js"
import { getScore } from "./lib/score.js"

export function parseCoordinates(coordString) {

  const [x, y, z] = coordString.split(' ').map(Number)
  return { x, y, z }


}

export const calculate = {

    random: function random(min, max) {
      
      return Math.floor(Math.random() * (max - min + 1)) + min
      
      
    },
    clamp: function(num, min, max) {
      
      return parseInt(Math.min(Math.max(num, min), max))
    
    
    }
  
  
}

export const actions = {

    getFront: function lookDirection(player, distance) {
  
      const playerViewDirection = player.getViewDirection();
  
      const playerHeadLocation = player.getHeadLocation();
  
      const scaledViewDirection = Vector3.Scale(playerViewDirection, distance);
  
      let result = Vector3.Add(playerHeadLocation, scaledViewDirection);
  
      return result;
  
      
    },
    rayCast: function(player, width, distance, comando, excludeFamilies = ["none"], excludeTypes = ["none"], repeat = false) {
      // Array to store entities that have been hit
      let hitEntities = [];
    
      for (let i = 0; i <= distance; i++) {
        try {
          // Spawn a temporary entity to use for raycasting
          const entity = player.dimension.spawnEntity("ld_aw:skills.null", actions.getFront(player, i * width));
          entity.nameTag = player.nameTag + "_ray";
    
          // Get entities in the ray's path
          const entitiesFromRay = entity.dimension.getEntities({
            location: entity.location,
            maxDistance: width,
            excludeNames: [player.nameTag, player.nameTag + "_ray"],
            excludeFamilies: excludeFamilies,
            excludeTypes: excludeTypes
          });
    
          // Filter out entities that have already been hit
          const newTargets = entitiesFromRay.filter(target => !hitEntities.includes(target));
    
          // Execute the command for each new target
          newTargets.forEach(target => {
            repeat ? null: hitEntities.push(target);  // Add the target to the hit list
            comando(target);  // Execute the command on the target
          });
    
          // Remove the temporary entity after a short delay
          mc.system.runTimeout(() => { entity.remove(); }, 0.2 * 20);
    
        } catch (e) {
          console.error(e);  // Handle any errors
        }
      }
    },
    
    sound: {
  
      play: function(player, sound) {
  
        if (!mc.world.getDynamicProperty("ld_aw.:sound")) { return }
        player.runCommandAsync("playsound " + sound.id + " @a[r=" + sound.ray + "]")
    
    
      },
      stop: function(player, sound) {
    
        player.runCommandAsync("stopsound @a[r=" + sound.ray + "] " + sound.id)
    
    
      }
  
  
    },
    hud: {
  
      hide: {
  
        all: function(player) {
    
          player.runCommandAsync("hud @s hide air_bubbles")
          player.runCommandAsync("/hud @s hide armor")
          player.runCommandAsync("/hud @s hide crosshair")
          player.runCommandAsync("/hud @s hide health")
          player.runCommandAsync("/hud @s hide horse_health")
          player.runCommandAsync("/hud @s hide hotbar")
          player.runCommandAsync("/hud @s hide hunger")
          player.runCommandAsync("/hud @s hide paperdoll")
          player.runCommandAsync("/hud @s hide progress_bar")
          player.runCommandAsync("/hud @s hide tooltips")
          // player.runCommandAsync("/hud @s hide touch_controls")
      
      
        },
        reset: function(player) { player.runCommandAsync("hud @s reset") }
  
  
      }
  
  
    }
  
  
  }