using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Player : MonoBehaviour {

    //public NavMeshAgent navmesh;
    public float distance = 50f;
    public GameObject marker;
    private BoxCollider collider;
    private Rigidbody rb;
    public LayerMask layer;

    public bool canMove = true;


    Animator anim;
    NavMeshAgent agent;
    Vector2 smoothDeltaPosition = Vector2.zero;
    Vector2 velocity = Vector2.zero;
    // Use this for initialization
    void Start()
    {
        collider = GetComponent<BoxCollider>();

        anim = GetComponent<Animator>();
        agent = GetComponent<NavMeshAgent>();
        // Don’t update position automatically
        agent.updatePosition = false;
    }

    void Update()
    {
        //if mouse button (left hand side) pressed instantiate a raycast
        if (Input.GetMouseButtonDown(0) && canMove)
        {
            //create a ray cast and set it to the mouses cursor position in game
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, distance, layer))
            //if (Physics.Raycast(ray, out hit, distance))
            {
                //draw invisible ray cast/vector
                //Debug.DrawLine(ray.origin, hit.point);
                //log hit area to the console
                //Debug.Log(hit.point);
                agent.SetDestination(hit.point);
                marker.transform.position = hit.point;
                anim.SetBool("walking", true);
            }
        }
        //comment out to remove walk anim
        NavigationAnimation();


    }

    void NavigationAnimation() {
        Vector3 worldDeltaPosition = agent.nextPosition - transform.position;

        // Map 'worldDeltaPosition' to local space
        float dx = Vector3.Dot(transform.right, worldDeltaPosition);
        float dy = Vector3.Dot(transform.forward, worldDeltaPosition);
        Vector2 deltaPosition = new Vector2(dx, dy);

        // Low-pass filter the deltaMove
        float smooth = Mathf.Min(1.0f, Time.deltaTime / 0.15f);
        smoothDeltaPosition = Vector2.Lerp(smoothDeltaPosition, deltaPosition, smooth);

        // Update velocity if time advances
        if (Time.deltaTime > 1e-5f)
            velocity = smoothDeltaPosition / Time.deltaTime;

        bool shouldMove = velocity.magnitude > 0.5f && agent.remainingDistance > agent.radius;

        /*if (velocity.x == 0 && velocity.y == 0)
        {
            anim.SetBool("walking", false);
        }
        else {
            anim.SetBool("walking", true);
        }*/
        // Update animation parameters
        anim.SetBool("walking", shouldMove);
        //anim.SetFloat("velx", velocity.x);
        //anim.SetFloat("vely", velocity.y);

       // GetComponent<LookAt>().lookAtTargetPosition = agent.steeringTarget + transform.forward;
    }


    void OnAnimatorMove()
    {
        // Update position to agent position
        transform.position = agent.nextPosition;
    }


    public void DisableCollisions() {
        collider.enabled = false;
        
    }
    public void EnableCollisions()
    {
        collider.enabled = true;

    }

}
