using UnityEngine;
using System.Collections;

public class CornerHandler : MonoBehaviour {

	public Transform target1;
	public Transform target2;
	
	public bool trigger4Coder = true;
	public bool trigger4Designer = true;
	public bool trigger4Sound = true;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	void ActivateTrigger(Transform t, Actor actor) {
		if(t != null) {
			RouteHandler rh = t.gameObject.GetComponent<RouteHandler>();
			CornerHandler ch = t.gameObject.GetComponent<CornerHandler>();
			ActionObject ao = t.gameObject.GetComponent<ActionObject>();
			if(rh != null) {
				if(actor == Actor.coder) {
					rh.trigger4Coder = true;
				} else if(actor == Actor.designer) {
					rh.trigger4Designer = true;
				} else if(actor == Actor.sound) {
					rh.trigger4Sound = true;
				} 
			} else if(ch != null) {
				if(actor == Actor.coder) {
					ch.trigger4Coder = true;
				} else if(actor == Actor.designer) {
					ch.trigger4Designer = true;
				} else if(actor == Actor.sound) {
					ch.trigger4Sound = true;
				} 
			} else if(ao != null) {
				if(actor == Actor.coder) {
					ao.trigger4Coder = true;
				} else if(actor == Actor.designer) {
					ao.trigger4Designer = true;
				} else if(actor == Actor.sound) {
					ao.trigger4Sound = true;
				} 
			} 
		}
	}
	
	void OnTriggerEnter(Collider other) {
		Actor actor = other.GetComponent<NavigationController>().actor;
		if(actor == Actor.coder) {
			if(!trigger4Coder) return;
		} else if(actor == Actor.designer) {
			if(!trigger4Designer) return;
		} else if(actor == Actor.sound) {
			if(!trigger4Sound) return;
		}
		
		other.GetComponent<NavigationController>().target = findTargetFromSource(other.GetComponent<NavigationController>().source);
		other.GetComponent<NavigationController>().source = transform;
		if(actor == Actor.coder) {
			trigger4Coder = false;
		} else if(actor == Actor.designer) {
			trigger4Designer = false;
		} else if(actor == Actor.sound) {
			trigger4Sound = false;
		}
		
		ActivateTrigger(target1, actor);
		ActivateTrigger(target2, actor);
		
	}
	
	Transform findTargetFromSource(Transform source) {
		if(source == target1) {
				return target2;
		} else if(source == target2) {
			return target1;
		}
		return null;
	}
	
	
}
