﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
public class Movement : MonoBehaviour
{
    private Vector2 trackpad;
    private Vector3 moveDirection;
    private CapsuleCollider CapCollider;
    private Rigidbody RBody;
    
    public SteamVR_Input_Sources MovementHand;//Set Hand To Get Input From
    public SteamVR_Action_Vector2 TrackpadAction;
    public SteamVR_Action_Boolean JumpAction;
    public float jumpHeight;
    public float MovementSpeed;
    public float BodyRaduis;
    public float Deadzone;//the Deadzone of the trackpad. used to prevent unwanted walking.
    public GameObject Head;
    public GameObject AxisOffsetObject;//Hand Controller GameObject
    public PhysicMaterial FrictionMaterial;
    public PhysicMaterial NoFrictionMaterial;
    private void Awake()
    {
        CapCollider = GetComponent<CapsuleCollider>();
        CapCollider.center = new Vector3(0,.5f,0);
        CapCollider.radius = BodyRaduis;
        RBody = GetComponent<Rigidbody>();
        RBody.constraints = RigidbodyConstraints.FreezeRotation;
    }

    void Update()
    {
        moveDirection = Quaternion.AngleAxis(Angle(trackpad) + AxisOffsetObject.transform.localRotation.eulerAngles.y, Vector3.up) * Vector3.forward;//get the angle of the touch and correct it for the rotation of the controller
        updateInput();
        updateCollider();
        RaycastHit Hit;
        int layerMask = 1 << 9;
        layerMask = ~layerMask;
        if (trackpad.magnitude > Deadzone)
        {//make sure the touch isn't in the deadzone and we aren't going to fast.


            CapCollider.material = NoFrictionMaterial;

            if (JumpAction.GetStateDown(MovementHand) && Physics.Raycast(transform.position+Vector3.up,  Vector3.down, out Hit, 1.1f, layerMask))
            {
                float jumpSpeed = Mathf.Sqrt(2 * jumpHeight * 9.81f);
                RBody.AddForce(0, jumpSpeed, 0, ForceMode.VelocityChange);
            }
            RBody.AddForce(moveDirection.x* trackpad.magnitude * MovementSpeed - RBody.velocity.x, 0, moveDirection.z* trackpad.magnitude * MovementSpeed - RBody.velocity.z, ForceMode.VelocityChange);

            Debug.Log("Velocity" + moveDirection);
            Debug.Log("Movement Direction:" + moveDirection);
        }
        else
        {
            CapCollider.material = FrictionMaterial;
        }
    }
    public static float Angle(Vector2 p_vector2)
    {
        if (p_vector2.x < 0)
        {
            return 360 - (Mathf.Atan2(p_vector2.x, p_vector2.y) * Mathf.Rad2Deg * -1);
        }
        else
        {
            return Mathf.Atan2(p_vector2.x, p_vector2.y) * Mathf.Rad2Deg;
        }
    }
    private void updateCollider()
    {
        CapCollider.height = Head.transform.localPosition.y;
        CapCollider.center = new Vector3(Head.transform.localPosition.x, Head.transform.localPosition.y / 2, Head.transform.localPosition.z);
    }
    private void updateInput()
    {
        trackpad = TrackpadAction.GetAxis(MovementHand);
    }
    
}
