using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class PlayerDamageVisualizer : MonoBehaviour {
    private List<DamageVisual> _damageVisuals;
    private PlayerController _controller;

    public void Init(Player player)
    {
        _damageVisuals = new List<DamageVisual>();
        player.PlayerHit += OnPlayerHit;
        //foreach (var damageVisual in player.Data.DamageVisuals)
        //{
        //    var go = new GameObject(player.Data.Name + " damage " + damageVisual.Min + "-" + damageVisual.Max);
        //    go.transform.parent = transform;
        //    go.transform.localPosition = Vector3.zero;
        //    ParticleSystem system = Instantiate(damageVisual.SystemToBeSpawned, transform);
        //    var local = new DamageVisual()
        //    {
        //        Min = damageVisual.Min,
        //        Max = damageVisual.Max
        //    };
        //    system.transform.parent = go.transform;
        //    system.transform.localPosition = Vector3.zero;
        //    local.SetParticleSystem(system);
        //    _damageVisuals.Add(local);
        //}
        OnPlayerHit(player, 0, null);
    }

    private void OnPlayerHit(Player player, int amount, GameEntity source)
    {
        var damageVisual = _damageVisuals.FirstOrDefault(d => d.Min > player.HitPoints && d.Max < player.HitPoints);

        foreach (var visual in _damageVisuals)
        {
            if (damageVisual != null && visual == damageVisual)
            {
                visual.Play();
                continue;
            }
            Debug.Log("Pausing particle system", gameObject);
            visual.Pause();
        }

    }
}
