using ninja;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item : MonoBehaviour
{
    public enum Type
    {
        Shuriken,
        FlashGranade
    }

    [SerializeField]
    private Type m_type;

    [SerializeField]
    private float m_minRespawnTime = 5.0f;
    [SerializeField]
	private float m_maxRespawnTime = 8.0f;

	[SerializeField]
	private AudioClip[] m_ItemSounds;
	[SerializeField]
	private AudioClip m_CollectSound;

    private float m_respawnTimeLeft = 0.0f;

    private bool m_available = true;

    public void OnTriggerStay2D(Collider2D collider)
    {
        if (m_available && collider.transform.tag == "Player")
        {
            // Delete all actions except SwordAttack
            foreach (var action in collider.transform.GetComponents<IAction>())
            {
                if (!(action is SwordAttack))
                {
                    Debug.Assert(action is MonoBehaviour);
                    Destroy((action as MonoBehaviour));
                }
            }

            // Replace with new Skill
            AddComponent(collider.gameObject);
            m_respawnTimeLeft = Random.Range(m_minRespawnTime, m_maxRespawnTime);
            GetComponent<SpriteRenderer>().enabled = false;
            m_available = false;

			var audio = GetComponent<AudioSource> ();
			if (audio != null && m_CollectSound!=null) {
				audio.PlayOneShot (m_CollectSound, 6f);
			}
        }
    }

    private void AddComponent(GameObject go)
    {
        switch (m_type)
        {
            case Type.Shuriken:
				go.AddComponent<ShurikenAttack>().SetSounds(m_ItemSounds);
                break;
            case Type.FlashGranade:
				go.AddComponent<GrenadeAttack>().SetSounds(m_ItemSounds);
                break;
        }
    }

    // Use this for initialization
    void Start()
    {



    }

    // Update is called once per frame
    void Update()
    {
        if (!m_available)
        {
            m_respawnTimeLeft -= Time.deltaTime;

            if (m_respawnTimeLeft <= 0.0f)
            {
                m_available = true;
                GetComponent<SpriteRenderer>().enabled = true;
            }
        }
    }
}
