using UnityEngine;
using System.Collections;
using Util;

public class Jailer : MonoBehaviour {
	public float movespeed = 1.0f;
	public Animator animator;
	public Camera camera_object;
	public float checkroom_time_max = 3f;
	public Crane crane_object;

	private Vector3 position_default;
	private Vector3 scale_default;
	private RouteMarker nextmarker;
	private bool checkroom_in = false;
	private Room checkroom_room = null;
	private int checkroom_move = 0;
	private float checkroom_time = 0;
	private Vector3 checkroom_oldpos;
	// Use this for initialization
	void Start () {
		position_default = transform.position;
		scale_default = transform.localScale;
	}
	
	// Update is called once per frame
	void Update () {
		if (!checkroom_in) {
			runRoute ();
		} else {
			checkRoom ();
		}
	}

	void runRoute(){
		if (InputManager.Instance.isClick ()) {
			Vector3 oldposition = transform.position;
			Vector3 pos = transform.position;
			Vector2 touchpos = InputManager.Instance.getClickPosition();
			Vector3 raypos = new Vector3 (touchpos.x,touchpos.y,1f);
			Ray ray = camera_object.ScreenPointToRay (raypos);
			pos = camera_object.gameObject.transform.position;
			float mult = (pos.y-position_default.y) / ray.direction.y;
			pos += ray.direction * -mult;

			RouteMarker newrm = JailerRouteManager.Instance.getNearMarker (pos);
			//transform.position = newrm.transform.position;
			RouteMarker oldrm = JailerRouteManager.Instance.getNearMarker (oldposition);
			JailerRouteManager.Instance.setRouteEdge (oldrm, newrm, pos);

			nextmarker = JailerRouteManager.Instance.getNextMarker ();
		}

		nextmarker = JailerRouteManager.Instance.getNextMarker ();
		if (nextmarker != null) {
			animator.SetBool ("walk", true);
			if (transform.position.x > nextmarker.transform.position.x) {
				transform.localScale = scale_default;
			} else if (transform.position.x < nextmarker.transform.position.x) {
				transform.localScale = new Vector3 (-scale_default.x, scale_default.y, scale_default.z);
			}
			if (transform.position.z > nextmarker.transform.position.z) {
				animator.SetBool ("front", true);
			} else if (transform.position.z < nextmarker.transform.position.z) {
				animator.SetBool ("front", false);
			}
			transform.position += (nextmarker.transform.position - transform.position).normalized * movespeed;
			float dist = Vector3.Distance (transform.position, nextmarker.transform.position);
			if (dist < movespeed) {
				JailerRouteManager.Instance.goNextMarker ();
				transform.position = nextmarker.transform.position;
			}
		} else {
			animator.SetBool ("walk", false);
			// ルームチェックするかどうか
			if (JailerRouteManager.Instance.isCheckRoom ()) {
				checkroom_in = true;
				checkroom_room = JailerRouteManager.Instance.getCheckRoom ();
				JailerRouteManager.Instance.resetRoomCheck ();
			}
		}
	}

	void checkRoom(){
		switch (checkroom_move) {
		case 0:
			checkroom_oldpos = transform.position;
			checkroom_move = 1;
			break;
		case 1:
			checkRoom_Jump ();
			break;
		default:
			checkroom_in = false;
			transform.position = checkroom_oldpos;
			checkroom_move = 0;
			break;
		}

	}
	void checkRoom_Jump(){
		Vector3 pos = new Vector3(checkroom_room.transform.position.x,transform.position.y,checkroom_room.transform.position.z);
		checkroom_time += Time.deltaTime;
		if (checkroom_time > checkroom_time_max) {
			checkroom_time = 0;
			checkroom_move = 2;
			if (checkroom_room.isBroken ()) {
				SoundManager.GetInstance ().PlaySE ("keihou",false);
				crane_object.enterExecution (checkroom_room.getPrisoner ());
			}
		}
		transform.position = pos;
	}
}
