using UnityEngine;
using System.Collections;

public class planete : MonoBehaviour {
	
	
	public GameObject planette;
	public GameObject[] couches;
	public GameObject[] demi_couches;
	public GameObject dummy_camera;	
	public GameObject particleCreuse;
	public GameObject particleMinerais;
	public GameObject coeurPlanette;
	public GameObject mainCamera;
	private GameObject particleInstance;
	public GameObject explosionPrefab;
	
	public LayerMask destructibleLayer;
	
	// materiaux couches
	public Material texture_principale;
	public Material texture_secondaire;
	public Material texture_minerais;
	
	private Vector2 startMousePosition;
	private Vector3 offsetMousePos;
	
	
	private float speedRotation = 0.4f;
	private float screenWidth;
	private float screenHeight;	
	
	
	private string etat = "start";
	private string cacheEtat;
	private string cacheTriangle = " ";
	private string coucheEnCreation;

	
	// Use this for initialization
	void Start () {
		
		screenWidth = Screen.width;
		screenHeight = Screen.height;
		
		// texture la première couche de la terre
		texturingSurface();		
	}
	
	// Update is called once per frame
	void Update () {
		
		switch(etat){
			case("start") 			: UpdateStart(); 		break;
			case ("explode")		: UpdateExplose(); 		break;
			case ("init_creuse") 	: UpdateInitCreuse(); 	break;
			case ("creuse") 		: UpdateCreuse(); 		break;
			case ("pause") 			: UpdatePause(); 		break;
			case ("end")			: UpdateEnd(); 			break;
		}		
	}
	
	void OnGUI(){
		
		switch(etat){
			case "start" 		: GUIstart(); 		break;
			case "explode"		: GUIexplose();		break;
			case "init_creuse"	: GUIinitCreuse();	break;
			case "creuse" 		: GUIcreuse();		break;
			case "pause" 		: GUIpause(); 		break;
			case "end"			: GUIend(); 		break;
		}
		
		GUI.Label( new Rect(20,screenHeight -50,50,50), "Dev01");
	}
	
	
	
	void UpdateStart(){
	}
	
	void UpdateExplose(){

		// rotation de la planette
		activeRotatePlanette();
		Zoom();
		tirScript();
	}
	
	void UpdateInitCreuse(){
		
		instanceCouches();
		etat = "creuse";
	}
	
	void UpdateCreuse(){
		// rotation de la planette
		activeRotatePlanette();
		Zoom();
		// autorise la creusage de la planette
		creusePlanette();
		
		// script le coeur de la planette
		coeurPlanete();
	}
	
	void UpdatePause(){
	}
	
	void UpdateEnd(){
	
	}
	
	
	
	//------------ Bloc Texturing de la première couche de la planette 		----------------------------------------------------------------
	
	void texturingSurface(){

		// boucle parmi les couches de la planette
		for (int i = 0; i < 8; i++){
			
			// boucle pour les enfants de chaque couche de la planette 
			foreach ( Transform child in demi_couches[i].transform){
				
				//creation colider pour la couche de surface
				if(i <2){
					if(Random.Range(0,100)> 50){
						child.gameObject.renderer.material = texture_principale;
					}else{
						child.gameObject.renderer.material = texture_secondaire;
					}		
				}
				
				// assignation material couche 01
					if(i == 2 || i == 3){
					if(Random.Range(0,100) > 50){
						child.gameObject.renderer.material = texture_principale;
					}else{
						child.gameObject.renderer.material = texture_secondaire;
					}
					
					if(Random.Range(0,100) < 15){
						child.gameObject.renderer.material = texture_minerais;
					}						
				}
				//------------------
				
				// assignation material couche 02
				if(i == 4 || i == 5){
					if(Random.Range(0,100) > 50){
						child.gameObject.renderer.material = texture_principale;
					}else{
						child.gameObject.renderer.material = texture_secondaire;
					}
					
					if(Random.Range(0,100) < 40){
						child.gameObject.renderer.material = texture_minerais;
					}				
				}
				//------------------
				
				// assignation material couche 03
				if(i == 6 || i == 7){
					if(Random.Range(0,100) > 50){
						child.gameObject.renderer.material = texture_principale;
					}else{
						child.gameObject.renderer.material = texture_secondaire;
					}
					if(Random.Range(0,100) < 60){
						child.gameObject.renderer.material = texture_minerais;
					}
				}
				//------------------				
			}
		}		
	}
	
	//-----------------------------------------------------------------------------------------------------------------------------------------------
	
	
	
	//------------ Bloc action Tirer sur la planete 		-------------------------------------------------------------------------
	
	void tirScript(){
		
		if(Input.GetMouseButtonDown(0)){
			TirDown();
		}
		if(Input.GetMouseButtonUp(0)){
			TirUp();
		}			
	}
	
	
	void TirDown(){
		
		Ray ray = mainCamera.camera.ScreenPointToRay(Input.mousePosition);
   		RaycastHit hit;
    	if (Physics.Raycast(ray, out hit)){
			cacheTriangle = hit.transform.name;
		}		
		
		
	}
	
	void TirUp(){
		
		Ray ray = mainCamera.camera.ScreenPointToRay(Input.mousePosition);
   		RaycastHit hit;
    	if (Physics.Raycast(ray, out hit)){
			if(hit.transform.name == cacheTriangle && cacheTriangle != " " && hit.transform.gameObject.layer == 9){			
				
				Instantiate( explosionPrefab, hit.point , Quaternion.LookRotation(hit.point));
			}
			
		}
	}
	
	
	//------------------------------------------------------------------------------------------------------------------------------------------------
	
	
	//------------ Bloc creuse planette 		------------------------------------------------------------------------------------------------------
	
	
	void creusePlanette(){
		if (Input.touchCount == 1){
    		if (Input.GetTouch(0).phase == TouchPhase.Began){
				CreuseDown();
			}
			if (Input.GetTouch(0).phase == TouchPhase.Ended){
				CreuseUp();
			}
		}
		
		if(Input.GetMouseButtonDown(0)){
			CreuseDown();
		}
		if(Input.GetMouseButtonUp(0)){
			CreuseUp();
		}		
		
	}
	
	void CreuseDown()
	{
		Ray ray = mainCamera.camera.ScreenPointToRay(Input.mousePosition);
   		RaycastHit hit;
    	if (Physics.Raycast(ray, out hit)){
			cacheTriangle = hit.transform.name;
		}	
	}
	
	void CreuseUp(){
		Ray ray = mainCamera.camera.ScreenPointToRay(Input.mousePosition);
   		RaycastHit hit;
    	if (Physics.Raycast(ray, out hit)){
			if(hit.transform.name == cacheTriangle && cacheTriangle != " " && hit.transform.gameObject.layer == 9){	
				
				Color targetColor = hit.transform.gameObject.renderer.material.color;
				
				// intance des particules à la desturction d'un triangle
				
				if(hit.transform.gameObject.renderer.material.name == "triangle_principale (Instance)" || hit.transform.gameObject.renderer.material.name == "triangle_secondaire (Instance)"){
					particleInstance = particleCreuse;
				}
				else if(hit.transform.gameObject.renderer.material.name == "triangle_minerais (Instance)"){
					particleInstance = particleMinerais;
				}
				
				GameObject instanceParticle = (GameObject) Instantiate( particleInstance, (hit.point - (hit.point /10.0f)) , Quaternion.identity);
				instanceParticle.particleEmitter.localVelocity = hit.point * 0.4f;				
				
				//  --- A supprimer pour une version mobile
				
				if(particleInstance == particleCreuse){
					ParticleAnimator particleAnimator = instanceParticle.GetComponent<ParticleAnimator>();	
					Color[] modifiedColors = particleAnimator.colorAnimation;
	        		
					for (int i = 0; i < 5; i++){
						if( i == 1){
							targetColor.a = 1.0f;
						}
						else if( i == 2){
							targetColor.a = 0.8f;
						}
						else if( i == 3){
							targetColor.a = 0.5f;
						}
						else if( i == 4){
							targetColor.a = 0.2f;
						}	
						modifiedColors[i] = targetColor;
					}
	        		particleAnimator.colorAnimation = modifiedColors;
				}
				// ---------------------------
				
				Destroy(hit.transform.gameObject);
			}
		}
	}	
	
	//------------------------------------------------------------------------------------------------------------------------------------------------	

	
	
	
	
	//------------ Bloc rotation planette 		-------------------------------------------------------------------------------------------------------
	
	void activeRotatePlanette(){
		
    	if (Input.GetMouseButtonDown(0)){
			initRotationPlanette();
		}	
		if (Input.GetMouseButton(0)){
			rotatePlanette();
		}	
	}
	
	void initRotationPlanette(){
		startMousePosition = Input.mousePosition;
	}
	
	void rotatePlanette()
	{	
		// rotation de la camera
		offsetMousePos = new Vector3(-(Input.mousePosition.y - startMousePosition.y), Input.mousePosition.x - startMousePosition.x, 0); 			
		dummy_camera.transform.Rotate( offsetMousePos * speedRotation * Time.deltaTime); 
	}
	
	//------------------------------------------------------------------------------------------------------------------------------------------------	
	
	
	
	
	// bloc zoom camera --------------------------------------------------------------------------------------------------------------------
		
	void Zoom(){	
  		if ( Input.GetAxis("Mouse ScrollWheel") < 0.0f ){
   			ZoomOut();
  		}
  		else if ( Input.GetAxis("Mouse ScrollWheel") > 0.0f ){
   			ZoomIn();
  		}
	}
	
	void ZoomOut(){
		mainCamera.transform.Translate(Vector3.back * Time.deltaTime * 50.0f);
	}
	
	void ZoomIn(){
		mainCamera.transform.Translate(Vector3.forward * Time.deltaTime * 50.0f);
	}
	
	

	// -----------------------------------------------------------------------------------------------------------------------------------

	
	
	
	
	//------------ Création des couche de la planette 	----------------------------------------------------------------------------------
	
	void instanceCouches(){	
		
		Instantiate(couches[0], new Vector3(0,0,0), Quaternion.identity);
		Instantiate(couches[1], new Vector3(0,0,0), Quaternion.identity);
		Instantiate(couches[2], new Vector3(0,0,0), Quaternion.identity);		
	}
	
	//------------------------------------------------------------------------------------------------------------------------------------------------	
	
	
	
	//------------ Rotation du coeur de la planette  	-------------------------------------------------------------------------------------------------------
	
	void coeurPlanete(){
		
		Vector3 VrotateCoeur = new Vector3( 10.0f,10.0f,10.0f);
		coeurPlanette.transform.Rotate( VrotateCoeur * Time.deltaTime * 2.0f);
	}
	
	
	
	/////////////-- GUI	    //////////////////////////////////////////////////////////////////////////////
	
	
	void GUIstart(){
	
		if (GUI.Button(new Rect(screenWidth - 50, 10, 50, 50), "Menu")){
			cacheEtat = etat;
			etat = "pause";
		}
		
		
		if (GUI.Button(new Rect( screenWidth/2 - 40, screenHeight/2 - 25 , 80, 50), "start explosion")){
			etat = "explode";
		}		
		
	}
	
	
	void GUIexplose(){
		if (GUI.Button(new Rect(screenWidth - 50, 10, 50, 50), "Menu")){
			cacheEtat = etat;
			etat = "pause";
		}
		
		
		if (GUI.Button(new Rect( 10, 10, 50, 50), "Creuse")){
			etat = "init_creuse";
		}		
		
	}
	
	
	void GUIinitCreuse(){
		if (GUI.Button(new Rect(screenWidth - 50, 10, 50, 50), "Menu")){
			cacheEtat = etat;
			etat = "pause";
		}
	}
	
	
	void GUIcreuse(){
		if (GUI.Button(new Rect(screenWidth - 50, 10, 50, 50), "Menu")){
			cacheEtat = etat;
			etat = "pause";
		}
	}
	
	
	void GUIend(){

	}
	
	
	void GUIpause(){
		if (GUI.Button(new Rect( screenWidth/2 - 25, screenHeight/2 + 35 , 50, 50), "Quit")){
			Application.Quit();
		}
		
		if (GUI.Button(new Rect( screenWidth/2 - 25, screenHeight/2 - 35 , 50, 50), "Back")){
			etat = cacheEtat;
		}	
	}
	
	
}