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

public class ShurikenAttack : MonoBehaviour, IAction {
	[SerializeField]
	private float m_MaxTime = 1f;
    [SerializeField]
    private float m_spawnDistance = 1.0f;
    [SerializeField]
	private float m_speed = 5.0f;
	[SerializeField] private AudioClip[] m_Sounds;

	private bool m_Fired = false;
	private float m_TimeLeft;
    private Spawner m_spawner;

	public void SetSounds(AudioClip[] sounds) {
		m_Sounds = sounds;
	}

    public void doAction()
    {
		m_Fired = true;
        var lookDir = GetComponent<NinjaInputController>().lookDirection;
        var shuriken = m_spawner.SpawnShuriken(transform.position + lookDir * m_spawnDistance);
        shuriken.GetComponent<Shuriken>().Shoot(lookDir);
		shuriken.GetComponent<Deadly>().immuneObject = gameObject;

		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");
		m_TimeLeft = m_MaxTime;
    }
	
	// Update is called once per frame
	void Update () {
		if (m_Fired) {
			m_TimeLeft -= Time.deltaTime;
			if (m_TimeLeft <= 0) {
				Destroy (this);
			}
		}
	}
}
