using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterMovement : MonoBehaviour
{
    private Rigidbody rb;
    private RaycastHit hitInfo;
    public LayerMask wallOnly;
    public int speed;
    private float turnRate = 50;
    private Quaternion targetRot;
    public static Vector3 direction;

    // Use this for initialization
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        Physics.Raycast(transform.position, direction, out hitInfo, Mathf.Infinity, wallOnly);
    }


    // Update is called once per frame
    void Update()
    {
        if (Physics.gravity.y != 0)
        {
            if(Physics.gravity.y < 0)
            {
                Vector3 to = new Vector3(0, transform.rotation.y, 0);
                if (Vector3.Distance(transform.eulerAngles, to) > 0.1f)
                {
                    transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime * turnRate);
                }
                else
                {
                    transform.eulerAngles = to;
                }


                if (Input.GetKey(KeyCode.W))
                {
                    rb.AddForce(Vector3.forward * speed * Time.deltaTime);

                    Vector3 next = new Vector3(0, -90, 0);
                    if (Vector3.Distance(transform.eulerAngles, next) > 0.1f)
                    {
                        transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, next, Time.deltaTime * turnRate);
                    }
                    else
                    {
                        transform.eulerAngles = next;
                    }
                    SoundManager.instance.PlayWalkStep(3);
                }
                else if (Input.GetKey(KeyCode.S))
                {
                    rb.AddForce(Vector3.back * speed * Time.deltaTime);
                    Debug.Log("Should be turning down");
                    Vector3 next = new Vector3(0, 90, 0);
                    if (Vector3.Distance(transform.eulerAngles, next) > 0.1f)
                    {
                        transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, next, Time.deltaTime * turnRate);
                    }
                    else
                    {
                        transform.eulerAngles = next;
                    }
                    SoundManager.instance.PlayWalkStep(3);
                }
            }
            else
            {
                Vector3 to = new Vector3(0, transform.rotation.y, 180);
                if (Vector3.Distance(transform.eulerAngles, to) > 0.1f)
                {
                    transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime * turnRate);
                }
                else
                {
                    transform.eulerAngles = to;
                }


                if (Input.GetKey(KeyCode.W))
                {
                    Vector3 next = new Vector3(0, -90, 0);
                    if (Vector3.Distance(transform.eulerAngles, next) > 0.1f)
                    {
                        transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, next, Time.deltaTime * turnRate);
                    }
                    else
                    {
                        transform.eulerAngles = next;
                    }
                    rb.AddForce(Vector3.back * speed * Time.deltaTime);
                    SoundManager.instance.PlayWalkStep(3);
                }
                else if (Input.GetKey(KeyCode.S))
                {
                    Debug.Log("Should be turning down");
                    Vector3 next = new Vector3(0, 90, 0);
                    if (Vector3.Distance(transform.eulerAngles, next) > 0.1f)
                    {
                        transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, next, Time.deltaTime * turnRate);
                    }
                    else
                    {
                        transform.eulerAngles = next;
                    }
                    rb.AddForce(Vector3.forward * speed * Time.deltaTime);
                    SoundManager.instance.PlayWalkStep(3);
                }
            }

            if (Input.GetKey(KeyCode.A))
            {
                Vector3 next = new Vector3(0, 180,0);
                if (Vector3.Distance(transform.eulerAngles, next) > 0.1f)
                {
                    transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, next, Time.deltaTime * turnRate);
                }
                else
                {
                    transform.eulerAngles = next;
                }
                rb.AddForce(Vector3.left * speed * Time.deltaTime);
                SoundManager.instance.PlayWalkStep(3);
            }
            else if (Input.GetKey(KeyCode.D))
            {
                rb.AddForce(Vector3.right * speed * Time.deltaTime);
                SoundManager.instance.PlayWalkStep(3);
            }
        }
        else
        {
            if (Physics.gravity.x != 0)
            {
                if (Physics.gravity.x < 0)
                {
                    Vector3 to = new Vector3(0, transform.rotation.y, 270);
                    if (Vector3.Distance(transform.eulerAngles, to) > 0.1f)
                    {
                        transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime * turnRate);
                    }
                    else
                    {
                        transform.eulerAngles = to;
                    }
                }
                else
                {
                    Vector3 to = new Vector3(0, transform.rotation.y, 90);
                    if (Vector3.Distance(transform.eulerAngles, to) > 0.1f)
                    {
                        transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, to, Time.deltaTime * turnRate);
                    }
                    else
                    {
                        transform.eulerAngles = to;
                    }
                }

                if (Input.GetKey(KeyCode.A))
                {
                    rb.AddForce(Vector3.up * speed * Time.deltaTime);
                    SoundManager.instance.PlayWalkStep(3);
                }
                else if (Input.GetKey(KeyCode.D))
                {
                    rb.AddForce(Vector3.down * speed * Time.deltaTime);
                    SoundManager.instance.PlayWalkStep(3);
                }
                if (Input.GetKey(KeyCode.W))
                {
                    rb.AddForce(Vector3.forward * speed * Time.deltaTime);
                    SoundManager.instance.PlayWalkStep(3);
                }
                else if (Input.GetKey(KeyCode.S))
                {
                    rb.AddForce(Vector3.back * speed * Time.deltaTime);
                    SoundManager.instance.PlayWalkStep(3);
                }
            }
        }
    }
}
