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

[ExecuteAlways]
public class MeshConnectionsScript : MonoBehaviour {

	#if UNITY_EDITOR
	public Transform followPiece;

	[Header("Outer Left Positions")]
	[ReadOnly]
	public Transform outerLeftTopFront;
	[ReadOnly]
	public Transform outerLeftTopBack;
	[ReadOnly]
	public Transform outerLeftBottomFront;
	[ReadOnly]
	public Transform outerLeftBottomBack;

	[Header("Inner Left Positions")]
	[ReadOnly]
	public Transform innerLeftTopFront;
	[ReadOnly]
	public Transform innerLeftTopBack;
	[ReadOnly]
	public Transform innerLeftBottomFront;
	[ReadOnly]
	public Transform innerLeftBottomBack;

	[Header("Middle Positions")]
	[ReadOnly]
	public Transform innerLeftMidFront;
	[ReadOnly]
	public Transform innerLeftMidBack;
	[ReadOnly]
	public Transform innerRightMidFront;
	[ReadOnly]
	public Transform innerRightMidBack;

	[Header("Inner Right Positions")]
	public Transform innerRightTopFront;
	public Transform innerRightTopBack;
	public Transform innerRightBottomFront;
	public Transform innerRightBottomBack;

	[Header("Outer Right Positions")]
	[ReadOnly]
	public Transform outerRightTopFront;
	[ReadOnly]
	public Transform outerRightTopBack;
	[ReadOnly]
	public Transform outerRightBottomFront;
	[ReadOnly]
	public Transform outerRightBottomBack;


	public void Update () {
		if (transform.name == "End_Connection") {
			FindEndPiece ();
		}
	}

	public void FindEndPiece () {
		ModularTrack[] tracks = Transform.FindObjectsOfType<ModularTrack> ();

		for (int i = 0; i < tracks.Length; i++) {
			if (!tracks [i].endLink) {
				followPiece = tracks [i].track.pivotObject;
				transform.position = followPiece.position;
				transform.rotation = followPiece.rotation;
				return;
			}
		}

	}

	public void CheckClearPath (MeshConnectionsScript start) {
		RaycastHit hit;
		if (Physics.Raycast (transform.position, start.transform.position, out hit)) {

		}
		//Physics.Raycast(
	}
	#endif
}
