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

public class SkinlessMonsterAnimator : MonoBehaviour
{
    [SerializeField]
    private Animator animator;
    private float speed = 0f;

    [SerializeField]
    [Tooltip("This is the object with the skin dissolve material")]
    private GameObject modelObject;
    [SerializeField]
    private List<GameObject> objectsThatFallOnDeath= new List<GameObject>();

    private Material dissolveMaterial;
    private bool isAlive = true;
    [SerializeField]
    private float deathSpeed = 5f;
    private float curDeathSpeed = 5f;

    private bool isRunning = false;
    public bool IsRunning { get { return isRunning; } }
    [SerializeField]
    private float fastDeathduration = 1f;
    [SerializeField]
    private float deathDuration = 5f;
    


    // Start is called before the first frame update
    void Start()
    {
        dissolveMaterial = modelObject.GetComponent<SkinnedMeshRenderer>().materials[0];
        curDeathSpeed = deathSpeed;
    }

    // Update is called once per frame
    void Update()
    {
        
        if (isAlive)
        {
            animator.SetFloat("Speed", speed);
            
        }
        else
        {
            
            float quantity = dissolveMaterial.GetFloat("_DissolveQuantity");
            quantity = Mathf.Lerp(quantity, -2, Time.deltaTime *curDeathSpeed);
            dissolveMaterial.SetFloat("_DissolveQuantity", quantity);
        }

       
    }

    public void StartMoving()
    {
        if (isAlive)
        {
            isRunning = true;
            speed = 1;
        }
        
        
        
    }
    public void StopMoving()
    {
        if (isAlive)
        {
            speed = 0;
            isRunning = false;
        }
        
        
    }
    public void Attack()
    {
        if (isAlive)
        {
            animator.SetTrigger("Attack");
           

        }
        
        
    }


    /// <summary>
    /// 0,1,2,3
    /// </summary>
    /// <param name="attackType"></param>
    public void SetAttackType(int attackType)
    {
        
        animator.SetInteger("AttackIndex", attackType);
    }
    public void InLight()
    {
        if(isAlive)
        animator.SetBool("InLight", true);
    }
    public void NotInLight()
    {
        if(isAlive)
        animator.SetBool("InLight", false);
    }

    public void AttackScream()
    {
        if(isAlive)
        animator.SetTrigger("AttackScream");
    }

    public void Kill(bool fast = false)
    {
        //animator.speed = 0;
        InLight();
        

        isAlive = false;
        foreach(GameObject obj in this.objectsThatFallOnDeath)
        {
            obj.AddComponent<Rigidbody>();
            if (obj.GetComponent<Collider>() != null)
            {
                obj.GetComponent<Collider>().enabled = false;
            }
        }
        float dur = this.deathDuration;
        if (fast)
        {
            dur = this.fastDeathduration;
            curDeathSpeed = deathSpeed * (this.deathDuration / this.fastDeathduration);
        }
        StartCoroutine(destroyAfterSeconds(dur));
    }

    
    private IEnumerator destroyAfterSeconds(float duration)
    {
        yield return new WaitForSeconds(duration);
        Destroy(this.gameObject);
    }

}