using UnityEngine;
using System.Collections;

public class DemonController : MonoBehaviour {

    public GameObject[] DemonSetups;
	public GameObject[] SlashSprites;
	public AudioSource SlashSound;
	public AudioSource DeathSound;
	public FireSpawn Fire;

	public AudioSource[] growls;

	public enum DemonDirection
	{
        Up,
        Left,
        Down,
        Right
	}

    private DemonDirection _direction;

	void Start()
	{
		Growl ();
	}

    public DemonDirection Direction
    {
        get 
        {
            return _direction; 
        }
        set 
        { 
            if (value != _direction) 
            { 
                _direction = value;
                SetDemonDirection(_direction);
            } 
        }
    }

    private void SetDemonDirection(DemonDirection direction)
    {
		foreach (GameObject go in SlashSprites) {
			go.SetActive (false);
		}

		_direction = direction;
        DemonSetups[0].SetActive(false);
        DemonSetups[1].SetActive(false);
        DemonSetups[2].SetActive(false);
        DemonSetups[3].SetActive(false);
        switch (direction)
        {
            case DemonDirection.Up:
                DemonSetups[0].SetActive(true);
                break;
            case DemonDirection.Left:
                DemonSetups[1].SetActive(true);
                break;
            case DemonDirection.Down:
                DemonSetups[2].SetActive(true);
                break;
            case DemonDirection.Right:
                DemonSetups[3].SetActive(true);
                break;
        }
    }

	public void Attack()
	{
		SlashSound.Play();
		switch (_direction)
		{
		case DemonDirection.Up:
			SlashSprites[0].SetActive(true);
			break;
		case DemonDirection.Left:
			SlashSprites[1].SetActive(true);
			break;
		case DemonDirection.Down:
			SlashSprites[2].SetActive(true);
			break;
		case DemonDirection.Right:
			SlashSprites[3].SetActive(true);
			break;
		}
	}

	public void Die()
	{
		Fire.Spawn ();
		DeathSound.Play ();
	}

	public void Growl()
	{
		growls [Random.Range (0, growls.Length)].Play ();
		Invoke ("Growl", Random.Range (3f, 5f));
	}



    void Update()
    {
        // Test for DemonDirection:
//        var horizontal = Input.GetAxis("Horizontal");
//        var vertical = Input.GetAxis("Vertical");
//
//        rigidbody2D.velocity = new Vector2(horizontal * 5.0f, vertical * 5.0f);
//
//        if (rigidbody2D.velocity.magnitude > 0.05)
//        {
//            if (Mathf.Abs(vertical) >= Mathf.Abs(horizontal))
//            {
//                Direction = (vertical > 0 ? DemonDirection.Up : DemonDirection.Down);
//            }
//            else
//            {
//                Direction = (horizontal > 0 ? DemonDirection.Right : DemonDirection.Left);
//            }
//        }
//
//		if (Input.GetKeyDown(KeyCode.Space))
//		{
//			Attack();
//		}

    }

}
