using System.Collections;
using System.Collections.Generic;
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.Netcode;
using UnityEngine;

[RequireComponent(typeof(SphereCollider))]
public class DashNCrashPrefab : NetworkBehaviour
{
    [Header("Slow Zone Settings")]
    public Ability Ability;
    public GameObject visualEffectPrefab; // Prefab for visualizing the area

    private HashSet<ServerCharacter> affectedPlayers = new HashSet<ServerCharacter>();
    private SphereCollider zoneCollider;
    private GameObject visualEffectInstance;

    private void Start()
    {
        zoneCollider = GetComponent<SphereCollider>();

        // Spawn the visual effect
        if (visualEffectPrefab)
        {
            visualEffectInstance = Instantiate(visualEffectPrefab, transform.position, Quaternion.identity);
            visualEffectInstance.transform.localScale = Vector3.one * Ability.abilityRadius * 2f; // Adjust scale to match the radius
        }

        // Automatically destroy after the ability duration
        StartCoroutine(DelayedDestroy());
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.TryGetComponent<ServerCharacter>(out var player))
        {
            if (!player.IsCrow && affectedPlayers.Add(player)) // Only affect non-crow players
            {
                player.Movement.SetSpeedModifier(Ability.abilityMagnitude);
                Debug.Log($"{player.name} entered the slow zone. Speed reduced.");
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.TryGetComponent<ServerCharacter>(out var player))
        {
            if (affectedPlayers.Remove(player))
            {
                player.Movement.ResetSpeedModifier();
                Debug.Log($"{player.name} exited the slow zone. Speed restored.");
            }
        }
    }

    private void OnDestroy()
    {
        // Restore speed for all players when the zone is destroyed
        foreach (var player in affectedPlayers)
        {
            player.Movement.ResetSpeedModifier();
            Debug.Log($"{player.name}'s speed restored due to slow zone destruction.");
        }

        affectedPlayers.Clear();

        // Destroy the visual effect
        if (visualEffectInstance)
        {
            Destroy(visualEffectInstance);
        }
    }

    private IEnumerator DelayedDestroy()
    {
        yield return new WaitForSeconds(Ability.abilityDuration);

        if (IsServer)
        {
            DespawnZone();
        }
    }

    private void DespawnZone()
    {
        if (IsServer)
        {
            var networkObject = GetComponent<NetworkObject>();
            if (networkObject != null)
            {
                networkObject.Despawn(true); // Despawn and destroy on the server
                Debug.Log("SlowZonePrefab has been despawned and destroyed.");
            }
            else
            {
                Debug.LogError("SlowZonePrefab is missing a NetworkObject component!");
            }
        }
    }
}