using UnityEngine;
using System.Collections;

public class Robot : MonoBehaviour {
	enum Mode {broken, repairing, working};
	PatrolBehavior pb;
	ChaseHeroBehaviour ch;
	RangedAttackBehaviour ra;
	Interact inter;

	public AudioClip fixSound;
	public AudioClip activateSound;
	[SerializeField]
	private string requiredKeys;
	public string RequiredKeys {
		get
		{
			return requiredKeys;
		}
	}
	[SerializeField]
	private int noRequiredKeys;
	public int NoRequiredKeys
	{
		get
		{
			return noRequiredKeys;
		}
	}
	Mode mode {get; set;}
	// Use this for initialization
	void Start () {
		mode = Mode.broken;
		inter = GetComponent<Interact>();
		ch = GetComponent<ChaseHeroBehaviour>();
		pb = GetComponent<PatrolBehavior>();
		ra = GetComponent<RangedAttackBehaviour>();

        rigidbody.isKinematic = true;

        UpdateTooltip();
	}
	
	// Update is called once per frame
	void Update () {
		if (inter.Interacted() == true){
			interact();
		}
		if (mode == Mode.working)
		{
            rigidbody.isKinematic = false;
			if (ch.withinDistance())
			{
				pb.deactivate();
				ch.activate();
				ra.activate ();
			}
			else
			{
				pb.activate();
				ch.deactivate();
				ra.deactivate();
			}
			if (ra.isAttacking)
			{
				ch.deactivate();
				pb.deactivate();
			}
		}
		else
		{
			animation["anim_die"].time = 1.0f;
			animation.CrossFade("anim_die", 1.0f);
		}
	}


	void Awake()
	{
		inter = GetComponent<Interact>();
	}

	public void interact()
	{
		Hero h = GameObject.FindGameObjectWithTag("Hero").GetComponent<Hero>();
        bool hasKey = (string.IsNullOrEmpty(requiredKeys) || h.hasKey(requiredKeys, 1));
        if (mode == Mode.broken && hasKey)
        {
            mode = Mode.repairing;

			if(audio != null)
			{
				//Debug.Log ("Fix Sound");
				//audio.clip = fixSound;
				//audio.Play();
				audio.PlayOneShot(fixSound);
			}
        }

        if (mode == Mode.repairing && hasKey)
        {
            --noRequiredKeys;
            if (!string.IsNullOrEmpty(requiredKeys))
            {
                h.removekey(requiredKeys);
            }
        }

        if (mode == Mode.repairing && noRequiredKeys <= 0)
        {
            Objective o = GetComponent<Objective>();
            if (o)
                o.objective = true;
            mode = Mode.working;
            pb.activate();
            ch.activate();
            ra.activate();


			animation.CrossFade("anim_idle", 1.0f);

			if(audio != null)
			{
				//Debug.Log ("Activate");
				//audio.clip = activateSound;
				//audio.Play ();
				audio.PlayOneShot(activateSound);
			}
        }

        UpdateTooltip();
	}

    void UpdateTooltip()
    {
        if (inter != null)
        {
            if (mode == Mode.broken || mode == Mode.repairing)
            {
                inter.tooltipText = string.Format("Install Gear ({0} Missing)", NoRequiredKeys);
            }
            else
            {
                inter.tooltipText = string.Empty;
            }
        }
    }

	public bool isAttacking()
	{
		return ra.isAttacking;
	}

	public bool isAlive()
	{
		if (mode == Mode.working)
			return true;
		return false;
	}



	public void repair()
	{

	}
}
