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

public enum GearBoxPosition { Reverse, FirstGear, NeturalFirstRev, NeutralSecondThird,
	SecondGear, ThirdGear, NeutralFourthFifth, FourthGear, FifthGear};

public class GearboxControl : MonoBehaviour {

	public GearboxPos[] _gearBoxPositions;
	public Transform CurrentSelectedGear;
	private Ray _directionRay;
	private RaycastHit _directionRayHit;
	private GearboxPos targetGearBox;
	public GearBoxPosition SelectedGearBoxPosition;
	public bool clutchDepressed;

	void Start () 
	{
		_gearBoxPositions = GetComponentsInChildren<GearboxPos>();
		SelectedGearBoxPosition = GearBoxPosition.Reverse;
	}
		

	public void SetClutchPosition(bool isClutchDepressed)
	{
		clutchDepressed = isClutchDepressed;
	}


	public void SetGearBoxPosition(Transform GearboxLocation, GearBoxPosition targetGearboxPosition)
	{
		CurrentSelectedGear = GearboxLocation;
		SelectedGearBoxPosition = targetGearboxPosition;

		foreach (var gb in _gearBoxPositions) {
			if (gb.gp.Equals(targetGearboxPosition)) 
			{
				gb.SelectOrDeselectGear (true);
			} else 
			{
				gb.SelectOrDeselectGear (false);
			}
		}
	}

	public void CheckGearOption(Vector3 direction)
	{
		if (Physics.Raycast (CurrentSelectedGear.position, direction, out _directionRayHit, 100F)) 
		{
			if (_directionRayHit.collider.tag == "GearPos") 
			{
				targetGearBox = _directionRayHit.collider.GetComponent<GearboxPos> ();
				if (targetGearBox != null) 
				{
					if (clutchDepressed) 
					{
						targetGearBox.SelectGear ();
					}

				}
			}
		}
	}
}
