import { world, system } from "@minecraft/server";
import { fo } from './utilities/fo_constants.js';


export function initHorn() {

  
  const chance = 1 / 12; 
  const forebodingCheck = fo.scoreChapter3; 

  let hornTriggered = false; 

  world.afterEvents.itemReleaseUse.subscribe((eventData) => {
    const player = eventData.source, item = eventData.itemStack;

    if (item.typeId === "minecraft:goat_horn" && !hornTriggered) {

      if (fo.forebodingScore >= forebodingCheck && Math.random() <= chance) {
        const startPosition = player.location;

        
        system.runTimeout(() => {
          const hornPosition = getPositionAround(startPosition, 100);
          fo.overworld.playSound("horn.call.7", hornPosition, {
            pitch: 1.0, volume: 10,
          });

          hornTriggered = true; 
          return;

        }, 300); 
      }
    }
  });

  function getPositionAround(center, radius) {
    const angle = Math.random() * Math.PI * 2;
    const distance = Math.random() * radius;
    return {
      x: center.x + Math.cos(angle) * distance,
      y: center.y,
      z: center.z + Math.sin(angle) * distance,
    };
  }

};
