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

public class CableScript : MonoBehaviour {

	public GameObject[] ends;


	void Update () {
		foreach (GameObject end in ends) {
			if (end.GetComponent<PlugScript> ()) {
				if (end.GetComponent<PlugScript> ().signalReceivedPlug >= 0) {
					Send (end.GetComponent<PlugScript> ().signalReceivedPlug);
					break;
				} else {
					//
				}
			} else if (end.GetComponent<SocketScript> ()) {
				if (end.GetComponent<SocketScript> ().signalReceivedSocket >= 0) {
					Send (end.GetComponent<SocketScript> ().signalReceivedSocket);
					break;
				} else {
					//
				}
			}
		}
	}

	public void Send(int index){
		foreach (GameObject end in ends) {
			if (end.GetComponent<PlugScript> ()) {
				end.GetComponent<PlugScript> ().signalReceivedPlug = index;
			} else if (end.GetComponent<SocketScript> ()){
				end.GetComponent<SocketScript> ().signalReceivedSocket = index;
			}
		}
	}

	public void Reset(){
		foreach (GameObject end in ends) {
			if (end.GetComponent<PlugScript> ()) {
				end.GetComponent<PlugScript> ().signalReceivedPlug = -1;
			} else if (end.GetComponent<SocketScript> ()){
				end.GetComponent<SocketScript> ().signalReceivedSocket = -1;
			}
		}
	}
}
