using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class TargetWithLife : TargetBase
{
    public enum DamageType
    {
        Shot,
        Swing,
        Explosion,
    }
    public struct DeathInfo
    {
        public DamageType type;

        public Vector3 direction;

        public Vector3 explosionPosition;
        public float explosionRadius;
    }

    [SerializeField] float lifeLostPerShot = 0.3f;
    [SerializeField] float lifeLostPerSwing = 0.4f;
    [SerializeField] float lifeLostPerExplosion = 20f;
    [SerializeField] public UnityEvent<TargetWithLife, DeathInfo> onDeath;
    [SerializeField] public UnityEvent<TargetWithLife, float> onLifeLost;

    [SerializeField] Animator HitAnimator;
    [SerializeField] SpriteRenderer Spr;

    bool Invulnerable = false;

    DeathInfo deathInfo = new DeathInfo();

    public float Life = 1f;

    public override void NotifyShot()
    {
        LoseLife(DamageType.Shot, lifeLostPerShot);
    }

    public override void NotifyShot(float damage)
    {
        LoseLife(DamageType.Shot, damage);
    }

    public override void NotifySwing()
    {
        if(!Invulnerable)
        {
            Invulnerable = true;
            LoseLife(DamageType.Swing, lifeLostPerSwing);
            Invoke("DisableInvulnerability", 0.1f);
        }
    }

    public override void NotifySwing(float damage)
    {
        if (!Invulnerable)
        {
            Invulnerable = true;
            LoseLife(DamageType.Swing, damage);
            Invoke("DisableInvulnerability", 0.1f);
        }
    }

    public override void NotifyExplosion()
    {
        LoseLife(DamageType.Explosion, lifeLostPerExplosion);
    }

    public override void NotifyExplosion(float damage)
    {
        LoseLife(DamageType.Explosion, damage);
    }


    protected virtual void LoseLife(DamageType damageType, float howMuch)
    {
        /*deathInfo.type = damageType;
        switch (deathInfo.type)
        {
            case DamageType.Shot:
                deathInfo.direction = transform.position - PlayerMovement.instance.transform.position;
                break;

            case DamageType.Swing:
                deathInfo.direction = transform.position - PlayerMovement.instance.transform.position;
                break;

            case DamageType.Explosion:
                deathInfo.explosionPosition = Explosion.lastExplosionPosition;
                deathInfo.explosionRadius = Explosion.lastExplosionRadius;
                break;

            case DamageType.Particle:
                deathInfo.direction = Vector3.zero;
                break;

            default:
                deathInfo.direction = transform.position - PlayerMovement.instance.transform.position;
                break;
        }*/

        if(HitAnimator)
        {
            HitAnimator.SetTrigger("HitEnemy");
            if(Spr)
            {
                Color color = Color.red;
                color.a = 0.6f;
                Spr.color = color;
                Invoke("ResetColor", 0.3f);
            }
        }

        Life -= howMuch;
        onLifeLost.Invoke(this, Life);
        CheckStillAlive();
    }

    protected virtual void CheckStillAlive()
    {
        if (Life <= 0)
        {
            Destroy(this.gameObject);
            onDeath.Invoke(this, deathInfo);
        }
    }

    void DisableInvulnerability()
    {
        Invulnerable = false;
    }

    private void ResetColor()
    {
        Spr.color = Color.white;
    }
}
