using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class PlayerScript : MonoBehaviour {

    public float speed = 0;
    public float timeToActivate = 0;
    public float timeToFade;
    public string scene;

    private MovingScript.Dir _direction = MovingScript.Dir.NONE;
    private Animator _animator;
    private bool _isDaying = false;
    private float _timeWaiting = 0;
    private bool _waiting = true;
    private bool _isWalking = false;
    private bool _isIdle = true;
    private bool _fading = false;
    private float _timeFading = 0;

	// Use this for initialization
	void Start () {
        _animator = GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {

        if (_fading)
        {
            _timeFading += Time.deltaTime;

            if (_timeFading >= timeToFade)
            {
                SceneManager.LoadScene(scene);
            }

            return;
        }

        if (_waiting)
        {
            _timeWaiting += Time.deltaTime;

            if (_timeWaiting >= timeToActivate)
            {
                _waiting = false;
                this.gameObject.GetComponent<QueueScript>().enabled = false;
            }

            return;
        }

        float l_hDir = Input.GetAxis("Horizontal");
        float l_vDir = Input.GetAxis("Vertical");

        _direction = MovingScript.getDirection(l_hDir, l_vDir);

        if (!_isIdle && _direction == MovingScript.Dir.NONE)
        {
            _isIdle = true;
            _isWalking = false;
            MovingScript.setAnimation(_animator, _direction, transform);
        }
        else if (!_isWalking && _direction != MovingScript.Dir.NONE)
        {
            _isIdle = false;
            _isWalking = true;
            MovingScript.setAnimation(_animator, _direction, transform);
        }
        
        MovingScript.setMovement(_animator, transform, _direction, l_hDir, l_vDir, speed);

	}

    public void Die()
    {
        _animator.SetInteger("Special", 2);
        GetComponent<AudioSource>().Play();
        GameObject.Find("FadeOut").GetComponent<Animator>().SetBool("End", true);
        _fading = true;
    }
}
