﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Comm.Shoot
{
    [RequireComponent(typeof(Rigidbody))]
    public class ObjectDeleteManager : MonoBehaviour
    {
        private float MaxLiveTime = 5;
        private string strName = "Int";
        private List<string> lstName = new List<string>();
        private Animator animator;

        private void OnEnable()
        {
            StartCoroutine(DelayRececle(MaxLiveTime));
        }

        private void OnTriggerEnter(Collider other)
        {
            CommMethod(other.gameObject);
        }

        private void OnCollisionEnter(Collision collision)
        {
            CommMethod(collision.gameObject);
        }
        IEnumerator DelayRececle(float interval)
        {
            yield return new WaitForSeconds(interval);
            CommMethod(null);
        }

        private void CommMethod(GameObject obj)
        {
            if (obj != null)
            {
                animator = obj.transform.GetComponentInChildren<Animator>();
                if (animator != null)
                {
                    lstName.Clear();
                    AnimatorControllerParameter[] acps = animator.parameters;
                    foreach (AnimatorControllerParameter aps in acps)
                    {
                        if (aps.type == AnimatorControllerParameterType.Int)
                        {
                            lstName.Add(aps.name);
                        }
                    }
                    if (lstName.Contains(strName))
                    {
                        int index = animator.GetInteger(strName) + ShootComm.Instance.AnimatorIndex;
                        animator.SetInteger(strName, index);
                    }
                }
            }
            ObjectPool.Instance.RecycleObj(gameObject);
        }
    }
}
