using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AngelBotMovement : MonoBehaviour
{
    public GameObject target;
    public GameObject[] nav;
    public float Speed;
    private List<GameObject> visited;
    private GameObject trigger;
    private bool seen;
    private Light ls;
    private float timer;

    // Use this for initialization
    void Start()
    {
        trigger = target.transform.Find("AngelTrigger").gameObject;
        seen = false;
        GameObject spotlight;
        spotlight = target.transform.Find("Spotlight").gameObject;
        ls = spotlight.GetComponent<Light>();
        timer = Time.time;
        visited = new List<GameObject>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Time.time - timer >= 20f)
        {
            visited.Clear();
            timer = Time.time;
        }
    }
    void FixedUpdate()
    {
        if (seen && ls.intensity >= 1)
        {
            return;
        }
        else {
            foreach (GameObject test in nav)
            {
                if (Vector3.Distance(transform.position, test.transform.position) <= 1f && !visited.Contains(test))
                {
                    visited.Add(test);
                }
            }
            Vector3 temp = (target.transform.position - transform.position);
            temp.Normalize();
            RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2(temp.x, temp.y), Vector3.Distance(target.transform.position, transform.position));
            Debug.DrawRay(transform.position, new Vector2(temp.x, temp.y), Color.green);
            if (hit == false)
            {
                return;
            }
            if (GameObject.Find(hit.collider.gameObject.name) == target)
            {
                float anglerad = Mathf.Atan2(target.transform.position.y - transform.position.y, target.transform.position.x - transform.position.x);
                float angledeg = (180 / Mathf.PI) * anglerad;
                transform.rotation = Quaternion.Euler(0, 0, angledeg);
                transform.Translate(Speed, 0, 0);
                if (Mathf.Abs(transform.position.y - target.transform.position.y) <= 1f && Mathf.Abs(transform.position.x - target.transform.position.x) < 1f)
                {
                    return;
                }
            }
            else
            {
                //find navpoint that it can raycast to thats closest to target
                List<GameObject> availalable = new List<GameObject>();
                foreach (GameObject test in nav)
                {
                    Vector3 temp2 = (test.transform.position - transform.position);
                    temp2.Normalize();
                    RaycastHit2D hit2 = Physics2D.Raycast(transform.position, new Vector2(temp2.x, temp2.y));
                    //Debug.Log(test.transform.name);
                    //Debug.DrawRay(transform.position, new Vector2(temp2.x, temp2.y), Color.green);
                    //if (hit2 == false)
                    //{
                    //     Debug.LogError("ERROR");
                    //     return;
                    //  }
                    if (GameObject.Find(hit2.collider.gameObject.name) == test && !visited.Contains(test))
                    {
                        availalable.Add(test);
                    }
                }
                if (availalable.Count == 0)
                {
                    visited.Clear();
                    return;
                }
                GameObject leader = availalable[0];
                foreach (GameObject test in availalable)
                {
                    if (Vector3.Distance(test.transform.position, target.transform.position) < Vector3.Distance(leader.transform.position, target.transform.position))
                    {
                        leader = test;
                    }
                }
            //    Debug.Log(leader.transform.name + "leader");
                float anglerad2 = Mathf.Atan2(leader.transform.position.y - transform.position.y, leader.transform.position.x - transform.position.x);
                float angledeg2 = (180 / Mathf.PI) * anglerad2;
                transform.rotation = Quaternion.Euler(0, 0, angledeg2);
                transform.Translate(Speed, 0, 0);
            }
        }
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject == trigger)
        {
            RaycastHit2D hit = Physics2D.Raycast(transform.position, target.transform.position - transform.position);
            if (GameObject.Find(hit.collider.gameObject.name) == target)
            {
                seen = true;
                visited.Clear();
            }
        }
    }

    void OnTriggerExit2D(Collider2D col)
    {
        if (col.gameObject == trigger)
        {
            seen = false;
        }
    }
}

