using UnityEngine;
using System.Collections;
using Util;

public class Room : MonoBehaviour {

	public GameObject Jailer,Prisoner,Crane;
	public float BREAK_DISTANCE = 10f;
	public int BREAK_POWER = 1;
	public int ROOM_HP = 100;
	public int ACTIVITY = 5; // escape probability.
	public GameObject objct_prefab;
	public float object_pos = 0.1f;
	public Vector2 object_distance;
	public float prisoner_distance;
	private const int BREAK_PATTERN = 2; // number of break patterns.
	bool isBreaking = false;
	bool canBreak = true;
	int roomStatus = 0; // 0: clean room, 1-8: there're something broken, 9: Jailbreak.
	int damage = 0;
	private GameObject myobject;
	private Vector3 default_position_prisoner;
	private bool object_reset = false;
	private bool escape = false;

	void Start () {
		JailerRouteManager.Instance.addRoom (this);
		default_position_prisoner = Prisoner.transform.localPosition;
		// put object
		myobject = (GameObject)Instantiate (objct_prefab);
		resetObjectPosition ();
    }
	
	void Update () {
		JailBreak();
		updateRoom();
				if (Input.GetKeyDown (KeyCode.L))
						cleanRoom ();
				if (Input.GetKeyDown (KeyCode.Space))
						Crane.SendMessage ("enterExecution",Prisoner);
				if (Input.GetKeyDown (KeyCode.Space)) {
						forbidBreak ();
						Crane.SendMessage ("enterExecution", Prisoner);
				}


    }

	void JailBreak(){

		// prisoner is out of sight.
		if(Vector3.Distance(this.transform.position, Jailer.transform.position) > BREAK_DISTANCE){
			if (isBreaking) { // start a jailbreak.
				if (roomStatus == 0) {
					roomStatus = Random.Range (1, BREAK_PATTERN+1);
				}
				if (!object_reset) {
					object_reset = true;
					resetObjectPosition ();
				}
				renderer.material.color = Color.yellow;
				damage++;
			} else if (Random.Range (0, 1001) < ACTIVITY) {
				isBreaking = true;
			} else {
				object_reset = false;
			}
		}
		else{
			isBreaking = false;
			renderer.material.color = Color.blue;
		}
	}

	void resetObjectPosition(){
		myobject.transform.parent = transform;
		myobject.transform.localPosition = new Vector3 (Random.Range (-object_distance.x, object_distance.x), 0, object_pos+Random.Range (-object_distance.y, object_distance.y));
		Prisoner.transform.localPosition = default_position_prisoner + new Vector3 (Random.Range(-prisoner_distance,prisoner_distance),0,0);
	}

	void updateRoom(){
		if(ROOM_HP<damage){ // broken (go to gameover scene).
			roomStatus = 9;
			renderer.material.color = Color.red;
			if (!escape) {
				escape = true;
				EffectManager.GetInstance ().PlayEffectGameOver ();
				Prisoner.SetActive (false);
			}
		}
		else if(!isBreaking){
			switch(roomStatus){ // move Object according to room status code.
			case 1:
				renderer.material.color = Color.green;
				break;
			case 2:
				renderer.material.color = Color.cyan;
				break;
			case 3:
				break;
			case 4:
				break;
			case 5:
				break;
			case 6:
				break;
			case 7:
				break;
			case 8:
				break;
			}
		}
	}

	public void cleanRoom(){
		isBreaking = false;
		roomStatus = 0;
		damage = 0;
		Prisoner.transform.localPosition =  new Vector3 (0f,0f,0f);
		renderer.material.color = Color.blue;
		allowBreak ();
	}

	public bool isBroken(){
		return roomStatus!=0 ? true : false;
	}

	public void forbidBreak(){
		canBreak = false;
	}
	
	public void allowBreak(){
		canBreak = true;
	}

	public GameObject getPrisoner(){
		return Prisoner;
	}
}
