using UnityEngine;
using System.Collections;

public class AICharacterInput : MonoBehaviour {

    public GameObject markerObj;

    private BaseCharacterController baseController;

    public AudioSource sfxSource;
    public AudioClip questionSound;

    public Transform target;
    public LayerMask layerMask;
    public LayerMask jumpLayers;

    public float RaycastLength;

    public bool canFollow = true;
    public bool isFollowing;

    private Vector2 targetPoint;

    public GameObject ui;
    public GameObject marker;

    public LineRenderer line;
    bool updateLine;
    Vector3 lineHitPoint;

    // Use this for initialization
    void Awake()
    {
        baseController = GetComponent<BaseCharacterController>();

        marker = Instantiate(markerObj);
    }

    // Update is called once per frame
    void Update()
    {
        Vector2 direction = (target.position + (Vector3.up * target.localScale.y /2)) - (transform.position + (Vector3.up * transform.localScale.y / 2));
        float distance = Vector2.Distance(transform.position, target.transform.position);
        RaycastHit2D hit = Physics2D.Raycast((transform.position + (Vector3.up * transform.localScale.y / 2)), direction,Vector2.Distance(transform.position,target.transform.position),layerMask);
        Debug.DrawRay((transform.position + (Vector3.up * transform.localScale.y / 2)), direction, new Color(0,1,0), 0.1f);

        if (hit)
        {
            canFollow = false;
            lineHitPoint = hit.point;
        }
        else
            canFollow = true;

        if (baseController.state == BaseCharacterController.STATE.DEAD)
        {
            canFollow = false;
            isFollowing = false;
            marker.SetActive(false);
        }

        if (isFollowing)
        {
            MoveTowardsPlayer();
        }
        else
        {
            baseController.Move(0, 0);
        }

        if (updateLine)
        {
            line.SetPosition(0, transform.position);
            line.SetPosition(1, lineHitPoint);
        }
        else
        {
            line.SetVertexCount(0);
        }
    }

    void MoveTowardsPlayer()
    {
        RaycastHit2D rightWall = Physics2D.Raycast(new Vector3(transform.position.x + (transform.localScale.x / 2), transform.position.y - (transform.localScale.y / 1.1f), transform.position.z), Vector2.right, RaycastLength, jumpLayers);
        RaycastHit2D leftWall = Physics2D.Raycast(new Vector3(transform.position.x - (transform.localScale.x / 2), transform.position.y - (transform.localScale.y / 1.1f), transform.position.z), -Vector2.right, RaycastLength, jumpLayers);

        Debug.DrawRay(new Vector3(transform.position.x + (transform.localScale.x / 2), transform.position.y - (transform.localScale.y / 1.1f), transform.position.z), Vector2.right, new Color(1, 0, 0), 0.1f);
        Debug.DrawRay(new Vector3(transform.position.x - (transform.localScale.x / 2), transform.position.y - (transform.localScale.y / 1.1f), transform.position.z), -Vector2.right, new Color(1, 0, 0), 0.1f);

        float distance = targetPoint.x - transform.position.x;

        float vertical = 0;

        if (rightWall || leftWall)
        {
            vertical = 1;
        }

        if (distance > 0)
        {
            if (distance > 0.5f)
                baseController.Move(1, vertical);
            else
            {
                isFollowing = false;
                marker.SetActive(false);
            }
        }
        else
        {
            if (distance < -0.5f)
                baseController.Move(-1, vertical);
            else
            {
                isFollowing = false;
                marker.SetActive(false);
            }
        }
    }

    public bool ToggleCall()
    {
        if(isFollowing)
        {
            isFollowing = false;
            marker.SetActive(false);
        }
        else if (!canFollow)
        {
            marker.SetActive(false);
            StartCoroutine(ShowQuestionMark());
        }
        else if (canFollow)
        {
            isFollowing = true;
            
            marker.SetActive(true);
            RaycastHit2D hitFloor = Physics2D.Raycast(target.transform.position, -target.transform.up, 100.0f, jumpLayers);
            marker.transform.position = hitFloor.point;
        }

        targetPoint = target.transform.position;

        return isFollowing;
    }

    IEnumerator ShowQuestionMark()
    {
        ui.SetActive(true);

        line.SetVertexCount(2);
        updateLine = true;

        //sfxSource.clip = questionSound;
        //sfxSource.Play();
        yield return new WaitForSeconds(1.0f);
        updateLine = false;
        ui.SetActive(false);
        yield return null;
    }
}

