using Assets.Scripts.Managers;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Assets.Scripts.Constants;

public class PlayerMovement : MonoBehaviour {

	#region variables
	private static bool _controlsEnabled = true;
	private static bool _shiftEnabled = true;

    public float _moveSpeed = 2.5f;

	protected Rigidbody _rigidbody;
	protected Animator _animator;
	protected Transform _camera;
	protected Transform _moveController;
    #endregion


    #region getters and setters
	public static bool ControlsEnabled {
    	get {
    		return _controlsEnabled;
    	}
    }

    public static bool ShiftEnabled {
    	get {
    		return _shiftEnabled;
    	}
	}
    #endregion


	#region events
	void Awake () {

		_camera = Camera.main.transform;
		_rigidbody = GetComponent <Rigidbody> ();
		_animator = transform.GetComponentInChildren <Animator> ();
		
	}

	void Start () {

		_moveController = GameObject.FindGameObjectWithTag ("MoveController").transform;
		
	}

	void FixedUpdate () {

		if (ControlsEnabled) {

			float horz = Input.GetAxisRaw ("Horizontal");
			float vert = Input.GetAxisRaw ("Vertical");

			if (CheckForEvent ()) {
				SetUserControl (false);
			}

			if (horz != 0) {
				_rigidbody.rotation = Quaternion.LookRotation(_moveController.TransformDirection (Vector3.right) * horz);

				if (CheckInFront ())
					_rigidbody.MovePosition (transform.position + (transform.TransformDirection (Vector3.forward) * _moveSpeed * Time.deltaTime));
			} else if (vert != 0) {
				_rigidbody.rotation = Quaternion.LookRotation(_moveController.TransformDirection (Vector3.forward) * vert);

				if (CheckInFront ())
				_rigidbody.MovePosition (transform.position + (transform.TransformDirection (Vector3.forward) * _moveSpeed * Time.deltaTime));
			}

		}

		_animator.SetBool ("isWalking", _rigidbody.velocity != Vector3.zero);

	}

	void OnEnable () {

		_animator.Rebind ();
		GameObject.FindObjectOfType <GameCamera> ().SetActivePlayer (transform);
		
	}
    #endregion


	#region methods
	bool CheckInFront () {
		
		Vector3 direction = transform.TransformDirection (Vector3.forward) / 2;
		Vector3 origin = transform.position + (Vector3.up * 3) + direction;

		Ray ray = new Ray (origin, Vector3.down);
		RaycastHit hit;

		if (Physics.Raycast (ray, out hit, 3f)) {
			if (hit.transform.tag == "Tile") {
				return true;	
			}
		}

		return false;

	}

	bool CheckForEvent () {

		Vector3 direction = transform.TransformDirection (Vector3.forward) / 2;
		Vector3 origin = transform.position + (Vector3.up * 3) + direction;

		Ray ray = new Ray (origin, Vector3.down);
		RaycastHit hit;

		if (Physics.Raycast (ray, out hit, 3f, 1 << LayerMask.NameToLayer (Layers.Events))) {
			hit.transform.SendMessage ("Activate");
			return true;
		}

		return false;
		
	}

	void OnDrawGizmos () {

		Vector3 direction = transform.TransformDirection (Vector3.forward) / 2;
		Vector3 origin = transform.position + (Vector3.up * 3) + direction;

		Ray ray = new Ray (origin, Vector3.down);

		Gizmos.DrawRay (ray);
		
	}

    public static void SetUserControl(bool controlsEnabled) {
        _controlsEnabled = controlsEnabled;
    }

	public static void SetUserShift(bool shiftEnabled) {
        _shiftEnabled = shiftEnabled;
    }
	#endregion
	
}
