using UnityEngine;
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.Netcode;

[CreateAssetMenu(menuName = "Abilities/TheExecutioner")]
public class TheExecutionerAbility : Ability
{
    [Header("Executioner Settings")]
    public float travelTime = 2f;
    public float spawnRadius = 18f; // Fixed spawn radius around the map center
    public float warningTime = 5f;
    
    public override void Execute(ServerCharacter character, Vector3 ignoredStartPoint, Vector3 ignoredDirection)
    {
        Vector3 mapCenter = CrowManager.Instance.mapCenter.position; // Use dynamic map center
        
        // Generate a random point on the edge of the circle (radius = spawnRadius)
        float angle = Random.Range(0f, Mathf.PI * 2);
        Vector3 spawnPoint = new Vector3(
            mapCenter.x + Mathf.Cos(angle) * spawnRadius,
            mapCenter.y,
            mapCenter.z + Mathf.Sin(angle) * spawnRadius
        );
        
        // Calculate the exact opposite point across the center (radius * 2 away)
        Vector3 endPoint = mapCenter + (mapCenter - spawnPoint);
        
        // Spawn and initialize the executioner box
        GameObject boxInstance = Instantiate(prefab, spawnPoint, Quaternion.LookRotation(endPoint - spawnPoint));
        if (boxInstance.TryGetComponent<NetworkObject>(out var netObj))
        {
            netObj.Spawn();
            boxInstance.GetComponent<ExecutionerBox>().Initialize(
                character, 
                spawnPoint,
                endPoint,
                travelTime,
                warningTime
            );
        }
    }
}