using UnityEngine;
using System.Collections;

public class GameControllerLevel01 : MonoBehaviour {
	int iRoundNum;
	public bool bMili;
	public bool bTechology;
	public bool bFood;
	public bool bEcon;
	public enum WinLose{Win,Lose,NotYet};
	public WinLose GameState;
	public GameObject gMili;
	public GameObject gTech;
	public GameObject gEcon;
	public GameObject gFood;
	 GameObject[] gWeapons =new GameObject[3] ;
	 public Transform tWeapon;
	public Transform tTarget;
	public Transform tArrivePos;
	public Transform tNotArrive;
	GameObject gNowWeapon;
	public GameObject gBullet;
	public GameObject gFadeEffect;
	GameObject gBackgorund ;

	// Use this for initialization
	void Start () {
	
	iRoundNum = 1;
	bMili=false;
	bTechology=false;
	bFood=false;
	bEcon=false;
	GameState = WinLose.NotYet;

	gMili = GameObject.Find ("military_animated");
	gTech = GameObject.Find ("technology_building");
	gEcon = GameObject.Find ("economy_building");
	gFood = GameObject.Find ("rations_building");
	
	tWeapon = GameObject.Find("Weapon").transform;
	gWeapons[0] = GameObject.Find ("laser_animated");
	gWeapons[1] = GameObject.Find ("rocket_animated");
	gWeapons[2] = GameObject.Find ("robot_animated");
	tTarget = GameObject.Find ("Target").transform;
	tArrivePos=GameObject.Find ("Arrive").transform;
	
	
	gBackgorund=GameObject.Find("BackGround_02");


	}
	
	// Update is called once per frame
	void Update () {
		//Debug
		//GameObject.Find ("DeBUG").GetComponent<UILabel>().text=""+GameState;
		GameObject.Find ("RoundNum").GetComponent<UILabel>().text="Round "+iRoundNum;
		if(GameState==WinLose.Win){
			tTarget.position = Vector3.Lerp(tTarget.position, tArrivePos.position, Time.deltaTime);
			tWeapon.LookAt (tTarget);
		}
//		else{
//			tWeapon.LookAt (tNotArrive);
//		}


	}
	//ME  F LASER  T BOBOT
	public void AnimationChk(){
		if(bMili){
			gMili.GetComponentInChildren<SkinnedMeshRenderer>().enabled=true;
			gMili.GetComponent<Animator>().SetBool("bMili",true);
			//gMili.animation.Play("phase2");
			//GameObject.Find("rationsmodel").GetComponent<SkinnedMeshRenderer>().enabled=true;
		}

		if(bTechology){
			gTech.GetComponentInChildren<SkinnedMeshRenderer>().enabled=true;
			gTech.GetComponent<Animator>().SetInteger("iTech",1);
			gMili.GetComponent<Animator>().SetBool("bTech",true);
		}
//
		if(bFood){
			gFood.GetComponentInChildren<SkinnedMeshRenderer>().enabled=true;
			gFood.GetComponent<Animator>().SetInteger("iFood",1);
			gMili.GetComponent<Animator>().SetBool("bFood",true);
		}
//
		if(bEcon){
			gEcon.GetComponentInChildren<SkinnedMeshRenderer>().enabled=true;
			gEcon.GetComponent<Animator>().SetInteger("iEcon",1);
			gMili.GetComponent<Animator>().SetBool("bEcon",true);
		}
//
		if(bMili && bTechology){

			gTech.GetComponent<Animator>().SetInteger("iTech",2);
		}
//
		if(bMili && bFood){
			gFood.GetComponent<Animator>().SetInteger("iFood",2);

		}
//
		if(bMili && bEcon){
			gEcon.GetComponent<Animator>().SetInteger("iEcon",2);
		}
//
//		if(bTechology && bEcon){
//			
//		}
//
//		if(bFood && bEcon){
//			
//		}
//
		if(bMili && bTechology && bFood ){
			gFood.GetComponent<Animator>().SetInteger("iFood",3);
			gTech.GetComponent<Animator>().SetInteger("iTech",3);
			if( iRoundNum==4){
				gWeapons[0].GetComponent<MeshRenderer>().enabled=true;
				gWeapons[0] .animation.Play ();
				gNowWeapon=gWeapons[0];
			}
		}
//
		if(bMili && bTechology && bEcon){
			gEcon.GetComponent<Animator>().SetInteger("iEcon",3);
			if( iRoundNum==4){
				gWeapons[2].GetComponent<MeshRenderer>().enabled=true;
				gWeapons[2] .animation.Play ();
				gNowWeapon=gWeapons[2];
			}
		}
//
		if(bMili && bFood && bEcon){
			gFood.GetComponent<Animator>().SetInteger("iFood",3);
			if( iRoundNum==4){
				gWeapons[1].GetComponent<MeshRenderer>().enabled=true;
				gWeapons[1] .animation.Play ();
				gNowWeapon=gWeapons[1];
			}
		}
//
//		if(bFood && bTechology && bEcon){
//
//		}
//
//		if(bMili && bTechology && bFood && bEcon){
//
//		}

	}

	public void WinLoseChk(){
		if(GameState!=WinLose.NotYet){
			GameObject.Find ("DeBUG").GetComponent<UILabel>().text=""+GameState;
		}
	}

	public void PassStageChk(){
		if(GameState==WinLose.NotYet){
		switch(iRoundNum){
		case 1:
			iRoundNum++;
			break;
			
		case 2:
			if(!(bTechology && bFood)){
				iRoundNum++;
			}else{
				GameState=WinLose.Lose;
					Instantiate (gFadeEffect,gFadeEffect.transform.position,gFadeEffect.transform.rotation);
					gBackgorund.SetActive (false);
			}
			break;

		case 3:
			if(!(bTechology && bFood && bEcon)){
				iRoundNum++;
			}else{
				GameState=WinLose.Lose;
					Instantiate (gFadeEffect,gFadeEffect.transform.position,gFadeEffect.transform.rotation);
					gBackgorund.SetActive (false);
			}
			break;

		case 4:
			iRoundNum++;
			if(bMili && bFood){
				GameState=WinLose.Win;
				Instantiate (gBullet ,gNowWeapon.transform.position,gBullet.transform.rotation);
					GameObject.Find ("alien").GetComponent<SpriteRenderer>().enabled=true;
					Invoke ("WinAnimation",2);



			}else{
				GameState=WinLose.Lose;
				Instantiate (gFadeEffect,gFadeEffect.transform.position,gFadeEffect.transform.rotation);
					gBackgorund.SetActive (false);
			}
			break;

		}
		}
	}
	void WinAnimation(){

		GameObject.Find ("end_picture_win").GetComponent<SpriteRenderer>().enabled=true;
		
		GameObject.Find ("earth").SetActive (false);
	}
}
