using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class NaZkouseni : MonoBehaviour {
	public GameObject cube1, cube2;
	public GameObject sphere;
	private Material initialMat;
	private Color initCol;
	// Use this for initialization

	private bool superCondition = false;

	void Start () {
		//string zk = "12(Clone)";
		//Debug.Log (zk.Substring (0, zk.IndexOf ("(")));
		string p = @Application.persistentDataPath;
		Debug.Log (p);
		//p = Directory.GetParent (p).FullName;
		//Debug.Log (p);
		p = Path.GetFullPath (Path.Combine (p, @"../../"));
		Debug.Log (p);

		float fa = 0.333332f;
		Debug.Log (fa.ToString ());
		Debug.Log (fa);
		Debug.Log (fa.ToString ("F6"));
		string fas = fa.ToString ("F6");
		Debug.Log (fas);
		float da = float.Parse (fas);
		Debug.Log (da.ToString ());
		Debug.Log (da);
		Debug.Log (da.ToString ("F6"));

		Vector3 vfa = new Vector3 (da, da, da);
		Debug.Log (vfa);
		Debug.Log (vfa.ToString ());
		Debug.Log (vfa.ToString ("F6"));


/*
		sphere.GetComponent<MeshRenderer> ().material = Resources.Load ("Models/Materials/RedGlassColor") as Material;
		initialMat = sphere.GetComponent<MeshRenderer> ().material;*/
		initCol = sphere.GetComponent<MeshRenderer> ().material.color;

		/*for(int i = 0, j = 1; i < 10; i++, j++){
			Debug.Log ("i = " + i + ", j = " + j);
			if (j == 5) {
				j = 0;
			}
		}*/
		//proved ();
	}
	private void proved(){
		StartCoroutine (performB ());
	}
	private IEnumerator performB(){
		Debug.Log ("*********************ZACINAM*********************");
		yield return new WaitUntil (() => bbbb ());
		Debug.Log ("*********************koncim*********************");
	}
	private bool bbbb(){
		superCondition = true;
		StartCoroutine (firstM ());

		return true;
	}
	private IEnumerator firstM(){
		for (int i = 0; i < 10; i++) {
			Debug.Log ("i = " + i);
			//yield return new WaitUntil (() => secondM (i));
			yield return StartCoroutine(secondM(i));
			Debug.Log ("End of cycle = " + i);
		}
		Debug.Log ("End of loop");
		superCondition = false;
	}
	private IEnumerator secondM(int i){
		/*for (int i = 0; i < 5; i++) {
			Debug.Log ("j = " + i);
		}*/
		if (i != 0) {
			StartCoroutine (thirdM (i));
		}
		yield return null;
		//return true;
	}
	private IEnumerator thirdM(int i){
		yield return new WaitForEndOfFrame ();
		Debug.Log ("end of frame , cyklus = " + i);
	}

	private void changeToInitMat(){
		sphere.GetComponent<MeshRenderer> ().material = initialMat;
	}
	private Material setInitMat(Material material){
		initialMat.SetFloat ("_Mode", material.GetFloat ("_Mode"));
		initialMat.SetInt ("_SrcBlend", material.GetInt ("_SrcBlend"));
		initialMat.SetInt ("_DstBlend", material.GetInt ("_DstBlend"));
		initialMat.SetInt ("_ZWrite", material.GetInt ("_ZWrite"));
		initialMat.DisableKeyword("_ALPHATEST_ON");
		initialMat.DisableKeyword("_ALPHABLEND_ON");
		if (material.IsKeywordEnabled ("_ALPHAPREMULTIPLY_ON")) {
			initialMat.EnableKeyword("_ALPHAPREMULTIPLY_ON");
		} else {
			initialMat.DisableKeyword("_ALPHAPREMULTIPLY_ON");
		}
		initialMat.renderQueue = material.renderQueue;
		initialMat.color = material.color;
		return initialMat;
	}
	private void changeMatOnTransp(){
		Material material = sphere.GetComponent<MeshRenderer> ().material;
		material.SetFloat("_Mode", 3);
		material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
		material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
		material.SetInt("_ZWrite", 0);
		material.DisableKeyword("_ALPHATEST_ON");
		material.DisableKeyword("_ALPHABLEND_ON");
		material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
		material.renderQueue = 3000;
		material.color = new Color (material.color.r, material.color.g, material.color.b, 0.2f);
		sphere.GetComponent<MeshRenderer> ().material = material;
	}
	private void changeMatOnBack(){
		Material material = sphere.GetComponent<MeshRenderer> ().material;
		material.SetFloat ("_Mode", initialMat.GetFloat ("_Mode"));
		material.SetInt ("_SrcBlend", initialMat.GetInt ("_SrcBlend"));
		material.SetInt ("_DstBlend", initialMat.GetInt ("_DstBlend"));
		material.SetInt ("_ZWrite", initialMat.GetInt ("_ZWrite"));
		material.DisableKeyword("_ALPHATEST_ON");
		material.DisableKeyword("_ALPHABLEND_ON");
		if (initialMat.IsKeywordEnabled ("_ALPHAPREMULTIPLY_ON")) {
			material.EnableKeyword("_ALPHAPREMULTIPLY_ON");
		} else {
			material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
		}
		material.renderQueue = initialMat.renderQueue;
		material.color = initialMat.color;
		sphere.GetComponent<MeshRenderer> ().material = material;
	}
	private void chanCol(Color c){
		Color a = sphere.GetComponent<MeshRenderer> ().material.color;
		sphere.GetComponent<MeshRenderer>().material.color = new Color (a.r, a.g, a.b, 0.25f);
	}
	private void chanBackCol(){
		Color a = sphere.GetComponent<MeshRenderer> ().material.color;
		sphere.GetComponent<MeshRenderer> ().material.color = new Color (a.r, a.g, a.b, 0.49609f);
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKeyDown (KeyCode.Q)) {
			chanCol (initCol);
			//changeMatOnTransp ();
			//zkusSoubor ();
		}
		if (Input.GetKeyDown (KeyCode.W)) {
			chanBackCol ();
			//changeToInitMat ();
			//changeMatOnBack();
			//changeMatOnOpaque ();
			//readS ("C:/Users/Andrejka/Desktop/Kaja/Model/zk.txt");
		}
	}
	private void zkusSoubor(){
		string path = "C:/Users/Andrejka/Desktop/Kaja/Model";
		if (!Directory.Exists (path)) {
			Directory.CreateDirectory (path);
		}
		path = path + "/zk.txt";
		string path2 = Application.persistentDataPath + "zk.txt";
		Debug.Log (Application.persistentDataPath);
		Debug.Log (path);
		StreamWriter sw = new StreamWriter (path, false);
		List<string> lines = new List<string> ();
		lines.Add (cube1.name + "#" + cube1.transform.position + "#" + cube1.transform.rotation + "#" + cube1.GetComponent<MeshRenderer> ().material.name);
		lines.Add (cube2.name + "#" + cube2.transform.position + "#" + cube2.transform.rotation + "#" + cube2.GetComponent<MeshRenderer> ().material.name);
		for (int i = 0; i < lines.Count; i++) {
			sw.WriteLine (lines [i]);
			//sw.Flush ();
		}
		sw.Close ();
	}
	private void readS(string path){
		string dir = path.Substring (0, path.LastIndexOf ("/"));
		if (Directory.Exists (dir) && File.Exists (path)) {
			StreamReader sr = new StreamReader (path);
			List<string> lines = new List<string> ();
			string l;
			while ((l = sr.ReadLine ()) != null) {
				lines.Add (l);
			}
			Debug.Log (lines.Count);
			for (int i = 0; i < lines.Count; i++) {
				//Debug.Log (lines [i]);
				transferStringInput (lines [i].Split (new char[]{ '#' }, 4));
			}
			Debug.Log ("konec");
			sr.Close ();
		} else {
			Debug.Log ("Adresar nebo soubor neexistuje!");
		}
	}
	private void transferStringInput(string[] parts){
		string name = parts [0];
		Vector3 pos = getVector3FromString (parts [1]);
		Quaternion rot = getQuaternionFromString (parts [2]);
		Material mat = Resources.Load ("Models/Materials/" + parts [3].Substring (0, parts [3].Length - 11)) as Material;
		Debug.Log (name + " " + name.GetType ().ToString ());
		Debug.Log (pos + " " + pos.GetType ().ToString ());
		Debug.Log (rot + " " + rot.GetType ().ToString ());
		Debug.Log (mat + " " + mat.GetType ().ToString ());
	}
	private Vector3 getVector3FromString(string s){
		s = s.Substring (1, s.Length - 2);	// orizne zavorky ... (0.0, 0.0, 0.0) na 0.0, 0.0, 0.0
		string[] splittedS = s.Split(new char[]{','}, 3);
		for (int i = 1; i < splittedS.Length; i++) {		// ze vsech krome prvniho
			splittedS [i] = splittedS [i].Substring (1);	// z kazdeho odebere prvni znak (vezme od druheho dal)
		}
		return new Vector3 (float.Parse (splittedS [0]), float.Parse (splittedS [1]), float.Parse (splittedS [2]));
	}
	private Quaternion getQuaternionFromString(string s){
		s = s.Substring (1, s.Length - 2);	// orizne zavorky ... (0.0, 0.0, 0.0, 0.0) na 0.0, 0.0, 0.0, 0.0
		string[] splittedS = s.Split(new char[]{','}, 4);
		for (int i = 1; i < splittedS.Length; i++) {		// ze vsech krome prvniho
			splittedS [i] = splittedS [i].Substring (1);	// z kazdeho odebere prvni znak (vezme od druheho dal)
		}
		return new Quaternion (float.Parse (splittedS [0]), float.Parse (splittedS [1]), float.Parse (splittedS [2]), float.Parse (splittedS [3]));
	}
		private void zkouska(){
				
				Bounds g = new Bounds ();
				Debug.Log (g.center);
				Debug.Log(g.size);

				Bounds b1 = new Bounds (Vector3.zero, Vector3.one);
				//Bounds b2 = new Bounds (new Vector3 (1.0f, 0.0f, 0.0f), Vector3.one);	// tohle se protne
				Bounds b2 = new Bounds (new Vector3 (1.001f, 0.0f, 0.0f), Vector3.one);	// tohle ne


				Bounds b4 = new Bounds (Vector3.zero, new Vector3 (0.2f, 1.0f, 0.8f));
				Debug.Log ("b4.min = " + b4.min + " , b4.max = " + b4.max);
				Bounds b5 = new Bounds (b4.center, new Vector3 (b4.size.z, b4.size.y, b4.size.x));
				Debug.Log ("b5.min = " + b5.min + " , b5.max = " + b5.max);



				string s = "1";
				Debug.Log (float.Parse (s));
				Debug.Log (1.0f);

				Bounds b = new Bounds (Vector3.zero, new Vector3 (64.0f, 64.0f, 64.0f));
				Vector3 newPosition = new Vector3 (b.center.x - (b.extents.x / 2.0f), b.center.y + (b.extents.y / 2.0f), b.center.z + (b.extents.z / 2.0f));
				Bounds LUF = new Bounds (newPosition, b.extents);
				Debug.Log (LUF.center);
				Debug.Log (LUF.max);



				Debug.Log (b1.Intersects (b2));
				int[] arr = new int[]{0, 1, 2, 4, 8, 16, 32, 64, 128};
				for (int i = 0; i < arr.Length; i++) {
						Debug.Log (arr[i] + " = " + System.Convert.ToByte (arr [i]));
				}
				/**
				 * 0000 0000	=	0
				 * 0000 0001	=	1
				 * 0000 0010	=	2
				 * 0000 0100	=	4
				 * 0000 1000	=	8
				 * 0001 0000	=  16
				 * 0010 0000	=  32
				 * 0100 0000	=  64
				 * 1000 0000	= 128
				 */
		}
		private int getM(int a, int b){
				if ((a % b) > (b / 2)) {
						return a + b - (a % b);
				} else {
						return a - (a % b);
				}
		}
}
