//Created by Neodrop
//neodrop@unity3d.ru
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//#pragma warning disable

public class CheckEnvironment : MonoBehaviour
{
    public AnimationClip animWarn, animIdle;
    private AI_Range _range;
    private HeadLookControllerImproved _headLook;

	void Start () {
        _range = GetComponent<AI_Range>();
        _headLook = GetComponent<HeadLookControllerImproved>();
	}
	
	void Update () 
    {
        List<string> zones = _range.ObjectInZoneSimple(Camera.main.transform);
        if (zones.Count == 0)
        {
            ModePeace();
            return;
        }
        int count = zones.Count;
        if (zones.Contains("Action Zone") || zones.Contains("Touch Zone"))
            ModeWarning();
        else ModeIdle();
	}

    void ModeWarning()
    {
        _headLook.target = Camera.main.transform.position;
        if (AntaresAddonController.ObjectHasMultitag(gameObject, "state _warning")) return;
        AntaresAddonController.RemoveMultitagFromObject(gameObject, "state _peace");
        AntaresAddonController.AddMultitagToObject(gameObject, "state _warning");
        animation.CrossFade(animWarn.name);
    }

    void ModeIdle()
    {
        _headLook.target = Camera.main.transform.position;
        if (AntaresAddonController.ObjectHasMultitag(gameObject, "state _warning"))
        {
            AntaresAddonController.RemoveMultitagFromObject(gameObject, "state _warning");
            animation.CrossFade(animIdle.name);
        }
        if (AntaresAddonController.ObjectHasMultitag(gameObject, "state _idle")) return;
        AntaresAddonController.RemoveAllMultitagsFromObject(gameObject);
        AntaresAddonController.AddMultitagToObject(gameObject, "state _idle");
        animation.CrossFade(animIdle.name);
    }

    void ModePeace()
    {
        if (AntaresAddonController.ObjectHasMultitag(gameObject, "state _peace")) return;
        AntaresAddonController.RemoveAllMultitagsFromObject(gameObject);
        AntaresAddonController.AddMultitagToObject(gameObject, "state _peace");
        _headLook.target = Vector3.zero;
        animation.CrossFade(animIdle.name);
    }

    public Texture antaresLogo;
    void OnGUI()
    {
        GUI.Label(new Rect(10, 10, 128, 128), antaresLogo);
       
        GUILayout.BeginHorizontal();
        GUILayout.Space(10);
        if (AntaresAddonController.ObjectHasMultitag(gameObject, "state _peace"))
        {
            GUILayout.Label("I'm in peace");
            return;
        }
        
        if (AntaresAddonController.ObjectHasMultitag(gameObject, "state _idle"))GUILayout.Label("I see you");
            
        if (AntaresAddonController.ObjectHasMultitag(gameObject, "state _warning"))
            GUILayout.Label(" and I warn you!");
            
        GUILayout.EndHorizontal();
    }
}
