using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Touch_move : MonoBehaviour {
	public GameObject player;
	public Vector3 pos;
	public float speed;
	public GameObject object1;
	public GameObject text_frame;
	public string st;
	public Text txt;
	public bool a;
	public bool pause;
	public item1 Item1;
	public select SELECT;
	// Use this for initialization
	void Start () {
		pos = player.transform.position;
		}


	IEnumerator PlayText(){

		foreach (char c in st) {

			txt.text += c;
			yield return new WaitForSeconds (0.2f);
		}

		if (txt.text.Length == st.Length) {
			pause = false;
			if (object1.name == "hand") {
				Destroy (object1);
				for (int i = 0; i < 4; i++) {
					if (Item1.item [i].enabled == false) {
						Item1.item [i].enabled = true;
						Item1.item [i].sprite = Item1.item_sprite [0];
						break;
					}


				}
			}else if (object1.name == "hud_keyBlue") {
				Destroy (object1);
				for (int i = 0; i < 4; i++) {
					if (Item1.item [i].enabled == false) {
						Item1.item [i].enabled = true;
						Item1.item [i].sprite = Item1.item_sprite [3];
						break;
					}


				}
			}else if (object1.name == "planeRed3") {
				Destroy (object1);
				for (int i = 0; i < 4; i++) {
					if (Item1.item [i].enabled == false) {
						Item1.item [i].enabled = true;
						Item1.item [i].sprite = Item1.item_sprite [1];
						break;
					}


				}
			}else if (object1.name == "playerShip1_red") {
				Destroy (object1);
				for (int i = 0; i < 4; i++) {
					if (Item1.item [i].enabled == false) {
						Item1.item [i].enabled = true;
						Item1.item [i].sprite = Item1.item_sprite [2];
						break;
					}


				}
			}else if (object1.name == "painting") {
				if (SELECT.select_item [SELECT.number].enabled == true && Item1.item [SELECT.number].sprite.name == "hand") {

					object1.GetComponent<SpriteRenderer> ().sprite = Item1.item [SELECT.number].sprite;
					Item1.item [SELECT.number].sprite = null;

					if (SELECT.number == 0) {
						Item1.item [0].sprite = Item1.item [0 + 1].sprite;
						Item1.item [1].sprite = Item1.item [0 + 2].sprite;
						Item1.item [2].sprite = Item1.item [0 +3].sprite;
						Item1.item [3].sprite = null;

					}else 	if (SELECT.number ==1) {
						Item1.item [1].sprite = Item1.item [1 + 1].sprite;
						Item1.item [2].sprite = Item1.item [1 + 2].sprite;

						Item1.item [3].sprite = null;

					}else 	if (SELECT.number ==2) {
						Item1.item [2].sprite = Item1.item [3].sprite;


						Item1.item [3].sprite = null;

					}else 	if (SELECT.number ==3) {
						


					}

					for (int i = 0; i < 4; i++) {
						if (Item1.item [i].sprite == null) {
							Item1.item [i].enabled = false;
						}
						SELECT.select_item [i].enabled = false;

					}
				}
				

			}

			Invoke ("delete_text", 3);

		}

		print ("morteza");
	}


	void delete_text(){
		a = false;

		object1 = null;
		txt.text = "";
		text_frame.SetActive (false);

	}

	void FixedUpdate(){
		if (object1) {
			if (!a) {
				if (object1.name == "zombie_frame") {
					st = "hello morteza";
					txt.text = "";
					if (player.transform.position.x == pos.x) {

						text_frame.SetActive (true);

						StartCoroutine ("PlayText");
						a = true;
						pause = true;
					}


				} else if (object1.name == "hand") {

					st = "this is a one hand photo";
					txt.text = "";
					if (player.transform.position.x == pos.x) {

						text_frame.SetActive (true);

						StartCoroutine ("PlayText");
						a = true;
						pause = true;
					}

				} else if (object1.name == "hud_keyBlue") {
					st = "this is a one key photo";
					txt.text = "";
					if (player.transform.position.x == pos.x) {

						text_frame.SetActive (true);

						StartCoroutine ("PlayText");
						a = true;
						pause = true;
					}

				} else if (object1.name == "planeRed3") {
					st = "this is a one planeRed3 photo";
					txt.text = "";
					if (player.transform.position.x == pos.x) {

						text_frame.SetActive (true);

						StartCoroutine ("PlayText");
						a = true;
						pause = true;
					}

				} else if (object1.name == "playerShip1_red") {
					st = "this is a one playerShip1\n_red photo";
					txt.text = "";
					if (player.transform.position.x == pos.x) {

						text_frame.SetActive (true);

						StartCoroutine ("PlayText");
						a = true;
						pause = true;
					}

				}else if (object1.name == "painting") {
					st = "install the painting";
					txt.text = "";
					if (player.transform.position.x == pos.x) {

						text_frame.SetActive (true);

						StartCoroutine ("PlayText");
						a = true;
						pause = true;
					}

				}

			}
		}

		if (Input.touchCount == 1) {
			if (!pause) {

				if (Input.GetTouch (0).tapCount == 1) {
					speed = 0.06f;
					Ray ray;
					ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);

					RaycastHit hit;

					if (Physics.Raycast (ray, out hit)) {
						object1 = hit.collider.gameObject;
				

						if (hit.point.x < player.transform.position.x) {


							pos = new Vector3 (hit.point.x + 2, pos.y, pos.z);
						} else {

							pos = new Vector3 (hit.point.x - 2, pos.y, pos.z);
						}


						if (player.transform.position.x > pos.x) {

							player.transform.localScale = new Vector3 (-1.5970f, player.transform.localScale.y, player.transform.localScale.z);

						} else {

							player.transform.localScale = new Vector3 (1.5970f, player.transform.localScale.y, player.transform.localScale.z);
						}

					}



				}
				if (Input.GetTouch (0).tapCount == 2) {

					speed = 0.16f;

					Ray ray;
					ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);

					RaycastHit hit;

					if (Physics.Raycast (ray, out hit)) {

						print (hit.point);

						if (hit.point.x < player.transform.position.x) {


							pos = new Vector3 (hit.point.x + 2, pos.y, pos.z);
						} else {

							pos = new Vector3 (hit.point.x - 2, pos.y, pos.z);
						}


						if (player.transform.position.x > pos.x) {

							player.transform.localScale = new Vector3 (-1.5970f, player.transform.localScale.y, player.transform.localScale.z);

						} else {

							player.transform.localScale = new Vector3 (1.5970f, player.transform.localScale.y, player.transform.localScale.z);
						}

					}

				}
			}
		}

		player.transform.position = Vector3.MoveTowards (player.transform.position, pos, speed);

	}
	// Update is called once per frame

}
