using UnityEngine;
using System.Collections;

public class RouteHandler : MonoBehaviour {

	public enum Direction {
		westEast, northSouth, westSouth, eastSouth, westNorth, eastNorth
	}
	
	public enum BlockedDirection {
		north, east, south, west
	}
	
	public Transform north;
	public Transform east;
	public Transform south;
	public Transform west;
	
	public bool trigger4Coder = true;
	public bool trigger4Designer = true;
	public bool trigger4Sound = true;
	
	public SpriteManager spriteManager;
	
	public Direction currentDirection;
	public BlockedDirection blockedDirection;

	// Use this for initialization
	void Start () {
		if(north == null) {
			blockedDirection = BlockedDirection.north;
		} else if(west == null) {
			blockedDirection = BlockedDirection.west;
		} else if(south == null) {
			blockedDirection = BlockedDirection.south;
		} else if(east == null) {
			blockedDirection = BlockedDirection.east;
		} 
		
		updateSprite();
		
	}
	
	// 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>().target);
		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(north, actor);
		ActivateTrigger(east, actor);
		ActivateTrigger(south, actor);
		ActivateTrigger(west, actor);
		
	}
	
	Transform findTargetFromSource(Transform source, Transform target) {
		
		if(currentDirection == Direction.westEast) {
			if(source == east) {
				return west;
			} else if(source == west) {
				return east;
			} else {
				return source;
			}
		} else if(currentDirection == Direction.northSouth) {
			if(source == north) {
				return south;
			} else if(source == south) {
				return north;
			} else {
				return source;
			}
		} else if(currentDirection == Direction.westSouth) {
			if(source == west) {
				return south;
			} else if(source == south) {
				return west;
			} else {
				return source;
			}
		} else if(currentDirection == Direction.eastSouth) {
			if(source == east) {
				return south;
			} else if(source == south) {
				return east;
			} else {
				return source;
			}
		} else if(currentDirection == Direction.westNorth) {
			if(source == west) {
				return north;
			} else if(source == north) {
				return west;
			} else {
				return source;
			}
		} else if(currentDirection == Direction.eastNorth) {
			if(source == east) {
				return north;
			} else if(source == north) {
				return east;
			} else {
				return source;
			}
		}
		return null;
	}
	
	public void OnMouseDown() {
		if(currentDirection == Direction.westEast) {
			if(blockedDirection == BlockedDirection.south) {
				currentDirection = Direction.westNorth;
			} else {
				currentDirection = Direction.westSouth;
			}
		} else if(currentDirection == Direction.westSouth) {
			if(blockedDirection == BlockedDirection.north) {
				currentDirection = Direction.eastSouth;
			} else {
				currentDirection = Direction.westNorth;
			}
		} else if(currentDirection == Direction.westNorth) {
			if(blockedDirection == BlockedDirection.south) {
				currentDirection = Direction.eastNorth;
			} else {
				currentDirection = Direction.northSouth;
			}
		} else if(currentDirection == Direction.northSouth) {
			if(blockedDirection == BlockedDirection.east) {
				currentDirection = Direction.westSouth;
			} else {
				currentDirection = Direction.eastNorth;
			}
		} else if(currentDirection == Direction.eastNorth) {
			if(blockedDirection == BlockedDirection.south) {
				currentDirection = Direction.westEast;
			} else {
				currentDirection = Direction.eastSouth;
			}
		} else if(currentDirection == Direction.eastSouth) {
			if(blockedDirection == BlockedDirection.west) {
				currentDirection = Direction.northSouth;
			} else {
				currentDirection = Direction.westEast;
			}
		}
		updateSprite();
	}
	
	private void updateSprite() {
		if(currentDirection == Direction.westEast) {
			GetComponent<SpriteRenderer>().sprite = spriteManager.westEast;
		} else if(currentDirection == Direction.westSouth) {
			GetComponent<SpriteRenderer>().sprite = spriteManager.westSouth;
		} else if(currentDirection == Direction.westNorth) {
			GetComponent<SpriteRenderer>().sprite = spriteManager.westNorth;
		} else if(currentDirection == Direction.northSouth) {
			GetComponent<SpriteRenderer>().sprite = spriteManager.northSouth;
		} else if(currentDirection == Direction.eastNorth) {
			GetComponent<SpriteRenderer>().sprite = spriteManager.eastNorth;
		} else if(currentDirection == Direction.eastSouth) {
			GetComponent<SpriteRenderer>().sprite = spriteManager.eastSouth;
		} 
	}
}
