using UnityEngine;
using System.Collections;

public class MilitarBasic : MonoBehaviour {

    public enum Intentions
    {
        KILL,
        TAKE_OUT,
        NONE
    }

    public enum State
    {
        SEARCHING,
        TAKING_OUT,
        HITTING,
        WAITING,
        IDLE
    }

    public Intentions intention;
    public GameObject target = null;
    public float speed;
    public float timeToWait;
    public float timeToStart;

    private State _state = State.WAITING;
    private Animator _animator;
    private float _timeWaiting = 0;
    private bool _starting = true;
    private float _timeStarting = 0;
    private bool _finished = true;
    private bool _removeHit = false;

	// Use this for initialization
	void Start () {

        _animator = GetComponent<Animator>();

        switch(intention)
        {
            case Intentions.KILL:
                _state = State.SEARCHING;
                break;
            case Intentions.TAKE_OUT:
                _state = State.SEARCHING;
                break;
        }

	}
	
	// Update is called once per frame
	void Update () {

        if (_starting)
        {
            _timeStarting += Time.deltaTime;

            if (_timeStarting >= timeToStart)
            {
                GetComponentsInChildren<AudioSource>()[1].Play();
                _starting = false;
            }

            return;
        }

        if (_finished && intention != Intentions.NONE)
        {
            GetComponent<BasicScript>().ReceiveAction(2, Intentions.NONE, this.gameObject);
            _finished = false;
        }
	
        switch(_state)
        {
            case State.TAKING_OUT:
                MovingScript.setAnimation(_animator, MovingScript.Dir.NORTH, transform);
                MovingScript.setMovement(_animator, transform, MovingScript.Dir.NORTH, 0f, 1f, speed);
                break;
            case State.SEARCHING:
                float l_hDir, l_vDir;

                l_hDir = target.transform.position.x - gameObject.transform.position.x;
                l_vDir = target.transform.position.y - gameObject.transform.position.y;

                MovingScript.Dir l_direction = MovingScript.getDirection(l_hDir, l_vDir);
                MovingScript.setAnimation(_animator, l_direction, gameObject.transform);
                MovingScript.setMovement(_animator, gameObject.transform, l_direction, l_hDir, l_vDir, speed);
                break;
            case State.HITTING:
                _animator.SetInteger("Special", 1);
                target.AddComponent<HitScript>();
                target.GetComponent<HitScript>().Hit();
                target.GetComponent<AudioSource>().PlayDelayed(1.0f);
                GetComponent<AudioSource>().PlayDelayed(2.6f);
                _state = State.WAITING;
                _removeHit = true;
                break;
            case State.WAITING:

                if (_removeHit)
                {
                    Destroy(target.GetComponent<HitScript>());
                    _removeHit = false;
                }

                if (timeToWait == 0)
                    break;

                _timeWaiting += Time.deltaTime;

                if (_timeWaiting >= timeToWait)
                {
                    _timeWaiting = 0;
                    
                    target.GetComponent<BasicScript>().Die();                    
                    _state = State.IDLE;
                }

                break;
            case State.IDLE:
                _animator.SetInteger("Direction", -1);
                break;
        }

	}
 
    void OnTriggerEnter2D(Collider2D coll)
    {
        if (_state == State.SEARCHING && intention == Intentions.TAKE_OUT && coll.gameObject.name == target.gameObject.name)
        {
            _state = State.TAKING_OUT;

            coll.gameObject.GetComponent<BasicScript>().ReceiveAction(1, intention, this.gameObject);
            speed = 0.2f;
        }

        if (coll.gameObject.tag == "Player" && _state == State.WAITING)
        {
            _state = State.HITTING;
            coll.gameObject.AddComponent<HitScript>();
            coll.gameObject.GetComponent<HitScript>().Hit();
        }
    }

    public void TakenOut()
    {
        _state = State.HITTING;
        Destroy(target.GetComponent<TakeOut>());
    }
}
