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

public enum SoundType
{
    Step,
    Medium,
    Big
}

public class Spawner : MonoBehaviour {
    private GameObjectPool m_stepSoundPool;
    private GameObjectPool m_mediumSoundPool;
    private GameObjectPool m_bigSoundPool;
    private GameObjectPool m_attackVisualPool;
    private GameObjectPool m_shurikenPool;
    private GameObjectPool m_grenadePool;
    private GameObjectPool m_grenadeExplosionPool;

    [SerializeField]
    private GameObject m_stepSoundSample;
    [SerializeField]
    private GameObject m_mediumSoundSample;
    [SerializeField]
    private GameObject m_bigSoundSample;
    [SerializeField]
    private GameObject m_attackVisualSample;
    [SerializeField]
    private GameObject m_shurikenSample;
    [SerializeField]
    private GameObject m_grenadeSample;
    [SerializeField]
    private GameObject m_grenadeExplosionSample;

    // Use this for initialization
    void Start()
    {
        Debug.Assert(m_stepSoundSample);
        m_stepSoundPool = new GameObjectPool(m_stepSoundSample);
        m_mediumSoundPool = new GameObjectPool(m_mediumSoundSample);
        m_bigSoundPool = new GameObjectPool(m_bigSoundSample);
        m_attackVisualPool = new GameObjectPool(m_attackVisualSample);
        m_shurikenPool = new GameObjectPool(m_shurikenSample);
        m_grenadePool = new GameObjectPool(m_grenadeSample, 10);
        m_grenadeExplosionPool = new GameObjectPool(m_grenadeExplosionSample);
    }

	public GameObject SpawnSound(Vector3 position, SoundType type)
	{
		return SpawnSound (position, type, new Color(1,1,1,1));
	}

	public GameObject SpawnSound(Vector3 position, SoundType type, Color color)
    {
        GameObject go = GetSoundPool(type).Create();
		go.transform.position = position;
		go.GetComponent<ParticleSystem> ().startColor = color;
        GetSoundPool(type).Free(go, 0.01f, true);
        return go;
    }

    public void FreeSound(SoundType type, GameObject sound, float waitForSeconds, bool waitForParticleDeath)
    {
        GetSoundPool(type).Free(sound, waitForSeconds, waitForParticleDeath);
    }

    private GameObjectPool GetSoundPool(SoundType type)
    {
        switch(type)
        {
            case SoundType.Step:
                return m_stepSoundPool;
            case SoundType.Medium:
                return m_mediumSoundPool;
            case SoundType.Big:
                return m_bigSoundPool;
        }

        return null;
    }

    public void SpawnAttack(Vector3 position)
    {
        GameObject go = m_attackVisualPool.Create();
        go.transform.position = position;
        m_attackVisualPool.Free(go, 0.01f, true);
    }

    public GameObject SpawnShuriken(Vector3 position)
    {
        GameObject go = m_shurikenPool.Create();
        go.transform.position = position;
        return go;
    }

    public GameObject SpawnGrenade(Vector3 position, Color color)
    {
        GameObject go = m_grenadePool.Create();
        go.transform.position = position;
        go.GetComponent<SpriteRenderer>().material.SetColor("_DestColor", color);
        return go;
    }

    public void FreeGrenade(GameObject grenade, float waitForSeconds, bool waitForParticleDeath)
    {
        m_grenadePool.Free(grenade, waitForSeconds, waitForParticleDeath);
    }

    public GameObject SpawnGrenadeExplosion(Vector3 position)
    {
        GameObject go = m_grenadeExplosionPool.Create();
        go.transform.position = position;
        go.GetComponent<FlashGrenadeExplosion>().Reset();
        return go;
    }

    public void FreeGrenadeExplosion(GameObject explosion, float waitForSeconds, bool waitForParticleDeath)
    {
        m_grenadeExplosionPool.Free(explosion, waitForSeconds, waitForParticleDeath);
    }

    public void FreeShuriken(GameObject shuriken, float waitForSeconds, bool waitForParticleDeath)
    {
        m_shurikenPool.Free(shuriken, waitForSeconds, waitForParticleDeath);
    }
}
