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

public class Disparador : MonoBehaviour {

    public int shootLimit = 1;
    public float speed = 6.0f;
    int shotParticles = 0;
    public AudioSource audio;

    ParticlePool pool;

	// Use this for initialization
	void Start () {
        //pool = GameObject.FindGameObjectWithTag("ParticlePool").GetComponentInChildren<ParticlePool>();

        GameObject sprite;
        Vector3 pos = new Vector3(transform.position.x, transform.position.y, -1);
        if (System.DateTime.Now.Hour <= 6 || System.DateTime.Now.Hour >= 20) {
            sprite = (GameObject)Instantiate(Resources.Load("noitecanhao"), pos, transform.rotation);
        } else {
            sprite = (GameObject)Instantiate(Resources.Load("diacanhao"), pos, transform.rotation);
        }
        sprite.transform.SetParent(transform);

        audio = GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update () {
        //var pos = Camera.main.WorldToScreenPoint(transform.position);
        //var dir = Input.mousePosition - pos;
        //var angle = (Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg) -90;

        //transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        
        //if (Input.GetKeyDown(KeyCode.Space)) {
        //    Shoot();
        //} 
        
        //if (Input.GetKey(KeyCode.W)) {
        //    transform.position += Vector3.up * speed * Time.deltaTime;
        //}
        
        //if (Input.GetKey(KeyCode.A)) {
        //    transform.position += Vector3.left * speed * Time.deltaTime;
        //} 
        
        //if (Input.GetKey(KeyCode.S)) {
        //    transform.position += Vector3.down * speed * Time.deltaTime;
        //}
        
        //if (Input.GetKey(KeyCode.D)) {
        //    transform.position += Vector3.right * speed * Time.deltaTime;
        //}
	}

    void FixedUpdate() {
        var angle = transform.rotation;
        if (Input.GetKey(KeyCode.LeftArrow)) {
            angle *= Quaternion.Euler(0, 0, 5);
            transform.rotation = Quaternion.Slerp(transform.rotation, angle, 0.5f);
        } else if (Input.GetKey(KeyCode.RightArrow)) {
            angle *= Quaternion.Euler(0, 0, -5);
            transform.rotation = Quaternion.Slerp(transform.rotation, angle, 0.5f);
        }
    }

    //Instantiates a particle on each particle exit
    public void Shoot() {
        if (shotParticles < shootLimit) {
            audio.Play();
            foreach (Transform t in transform) {
                if (t.CompareTag("ShootExit")) {
                    Instantiate(Resources.Load("Particle"), t.position, t.rotation);
                    //pool.Shoot(t.position, t.rotation);
                    shotParticles++;
                }
            }
        }

    }  
    //TODO shotParticles incrementa por particula ou por tiro? ex: um disparador que atira 2 ao mesmo tempo incrementa de 2 em 2?
}
