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

public class GrenadeAttack : MonoBehaviour, IAction
{
    [SerializeField]
    private float m_spawnDistance = 1.0f;
    private Vector2 m_relativeSpawnPos = new Vector2(1.0f, 1.0f);
	[SerializeField] private AudioClip[] m_Sounds;

    private float m_TimeLeft;
	private Spawner m_spawner;

    public void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawSphere(transform.position + new Vector3(m_relativeSpawnPos.x, m_relativeSpawnPos.y, 0.0f), 0.1f);
    }
	public void SetSounds(AudioClip[] sounds) {
		m_Sounds = sounds;
	}

    public void doAction()
    {
        var lookDir = GetComponent<NinjaInputController>().lookDirection;
        Vector3 relativeSpawnPos = new Vector3(lookDir.x * m_relativeSpawnPos.x, m_relativeSpawnPos.y, 0.0f);
        var grenade = m_spawner.SpawnGrenade(transform.position + relativeSpawnPos, GetComponent<SoundEmitter>().color);
        grenade.GetComponent<FlashGrenade>().Shoot(lookDir);
        grenade.GetComponent<FlashGrenade>().userOfGrenade = gameObject;
		Destroy(this);

		var audio = GetComponent<AudioSource> ();
		if (audio != null && m_Sounds.Length>0) {
			audio.PlayOneShot(Util.RandomElement (m_Sounds));
		}
    }

    public int priority()
    {
        return 1;
    }

    // Use this for initialization
    void Start()
    {
        m_spawner = Util.Find<Spawner>("Spawner");
    }
}
