﻿using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class PauseMenu : MonoBehaviour {

	public KeyCode pauseKey;

	private bool paused;
	private int menuNb=0;
	private string[] menus = { "RESUME","SAVE","QUIT" };


	public KeyCode enterKey;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
		if (Input.GetKeyDown(pauseKey)) {

			paused =!paused;


			GameObject[] persos = GameObject.FindGameObjectsWithTag("Ennemy");


			if (paused) {


			
			for(int i = 0 ; i < persos.Length ; i++){
				
				
				SetPause pause = (SetPause)persos[i].GetComponent (typeof(SetPause));
				pause.setPaused(true);
				
				
				
			}

			}else{

				
				for(int i = 0 ; i < persos.Length ; i++){
					
					
					SetPause pause = (SetPause)persos[i].GetComponent (typeof(SetPause));
					pause.setPaused(false);
					
					
					
				}


			}






		}


		if (paused) {

			Time.timeScale = 0;
		
			if(Input.GetKeyDown (KeyCode.DownArrow)){

				menuNb++;
				menuNb%= menus.Length;

			}

			if(Input.GetKeyDown (KeyCode.UpArrow)){
				if(menuNb>0){menuNb--;}else{ menuNb = menus.Length - 1; }
				menuNb%= menus.Length;
			}


			if(Input.GetKeyDown (enterKey)){


				if(menuNb % menus.Length == 0)paused = false;
				if(menuNb % menus.Length == 1){

					// SAVE GAME
					saveGame();
				}

				if(menuNb % menus.Length == 2)Application.Quit();

				


			}







		} else {

			Time.timeScale = 1;
		}

	}

	void OnGUI(){

		//if (paused) {

			if (paused) {
		
			GUI.Label (new Rect (Screen.width/2, Screen.height/2, Screen.width, Screen.height), "GamePaused");
			DrawQuad (new Rect(0, 0, Screen.width, Screen.height), new Color(0.7f,0.1f,0.5f,0.3f));


			for(int i = 0 ; i < 3 ; i++){
				if(i== menuNb){
					GUI.color = new Color(1.0f,0,0);}else{
					GUI.color = new Color(1.0f,1.0f,1.0f);
				}

				GUI.Button (new Rect( Screen.width/4 , Screen.height/4 + i * 100 , 200, 50 ) , menus [i] );
			}


		}
		//}

	}

	void OnApplicationPause(bool pauseStatus){
		paused = pauseStatus;

	}


	public bool getPaused(){
		return paused;

	}


	
	
	void DrawQuad(Rect position, Color color) {
		Texture2D texture = new Texture2D(1, 1);
		texture.SetPixel(0,0,color);
		texture.Apply();
		GUI.skin.box.normal.background = texture;
		GUI.Box(position, GUIContent.none);
	}



	void saveGame(){

		TextWriter tw = new StreamWriter ("Assets/SaveGameFolder/save.txt");

	


		GameObject[] persos = GameObject.FindGameObjectsWithTag("Ennemy");
			
			for (int i = 0; i < persos.Length; i++) {

			tw.WriteLine("TYPE");
			tw.WriteLine("Perso");
			tw.WriteLine("NAME");
			tw.WriteLine(persos[i].name);
			tw.WriteLine ("POSITION");
			tw.WriteLine (persos[i].transform.position);
			tw.WriteLine ("ROTATION");
			tw.WriteLine (persos[i].transform.rotation);
			tw.WriteLine ("ACTION");
			ActionScript actionScript = (ActionScript)persos[i].GetComponent(typeof(ActionScript));

			tw.WriteLine (""+actionScript.returnActionSetter());

			tw.WriteLine("MOVING");
			MoveToSimpler moveScript = (MoveToSimpler)persos[i].GetComponent(typeof(MoveToSimpler));

			tw.WriteLine(""+moveScript.returnTypeOfMoving());
			string myPlace="";
			if(moveScript.returnPlace()!=null) myPlace = moveScript.returnPlace().name;
			tw.WriteLine(myPlace);
			tw.WriteLine("COLOR");
			GetAColor colorScript = (GetAColor) persos[i].GetComponent(typeof(GetAColor));


			tw.WriteLine (""+colorScript.getMyColor());



		}

		tw.Close ();

		//SystemFile sr = File.CreateText ();
		//System.IO.WriteAllText("eeeeee","




	}




}
