using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CPU : MonoBehaviour {
	public GameObject Enemy1;
	public GameObject Enemy2;
	public GameObject imageENM;
	public int ENM1;
	public int ENM2;
	public Transform pos1;
	public Transform pos2;
	public Transform pos3;
	public Transform pos4;
	public bool pos11;
	public bool pos12;
	public bool pos13;
	public bool pos14;
	public Animator anim;
	bool a;
	public GameObject planGameOver;
	public GameObject TANKP;
	public Transform postank;
	public GameObject TANK;
	public int Health;
	public Text healthtext;
	// Use this for initialization
	void Start () {


	
	}
	
	// Update is called once per frame
	void Update () {

		if (TANK ==null && Health > 0) {
			Instantiate (TANKP, postank.position, postank.rotation);

			Health -=1;
			TANK = GameObject.Find ("PICSELART--TANK1(Clone)");
			healthtext.text=Health.ToString();
		}

		imageENM = GameObject.FindWithTag ("image");


		if (GameObject.FindGameObjectsWithTag ("image").Length != ENM1+ ENM2) {

			Destroy(imageENM);
		}
		  
		if (pos11 == true && pos12 && pos13 && pos14) {

			pos11 =false;
			pos12 =false;
			pos13 =false;
			pos14 =false;
		}


		if (GameObject.FindGameObjectsWithTag ("ENM").Length < 2 && !pos11 && ENM1 > 0) {

			Instantiate (Enemy1,pos1.position,pos1.rotation);
			pos11=true;
			ENM1 -=1;
		}

		if (GameObject.FindGameObjectsWithTag ("ENM").Length < 2 && !pos12 && ENM1 > 0) {
			
			Instantiate (Enemy1,pos2.position,pos2.rotation);
			pos12=true;
			ENM1 -=1;
		}

		
		if (GameObject.FindGameObjectsWithTag ("ENM").Length < 2 && !pos13 && ENM1 > 0) {
			
			Instantiate (Enemy1,pos3.position,pos3.rotation);
			pos13=true;
			ENM1 -=1;
		}

		if (GameObject.FindGameObjectsWithTag ("ENM").Length < 2 && !pos14 && ENM1 > 0  ) {
			
			Instantiate (Enemy1,pos4.position,pos4.rotation);
			pos14=true;
			ENM1 -=1;
		}





		if (GameObject.FindGameObjectsWithTag ("ENM2").Length < 2  && !pos11  && ENM2 > 0 ) {
			
			Instantiate (Enemy2, pos1.position, pos1.rotation);
			pos11=true;
			ENM2 -= 1;
		}

		if (GameObject.FindGameObjectsWithTag ("ENM2").Length < 2 && pos12== false && ENM2 > 0  ) {
			
			Instantiate (Enemy2, pos2.position, pos2.rotation);
			pos12=true;
			ENM2 -= 1;
		}
		if (GameObject.FindGameObjectsWithTag ("ENM2").Length < 2 && pos13== false && ENM2 > 0 ) {
			
			Instantiate (Enemy2, pos3.position, pos3.rotation);
			pos13=true;
			ENM2 -= 1;
		}
		if (GameObject.FindGameObjectsWithTag ("ENM2").Length < 2&& pos14== false  && ENM2 > 0) {
			
			Instantiate (Enemy2, pos4.position, pos4.rotation);
			pos14=true;
			ENM2 -= 1;
		}






		
		
		if (ENM1 == 0 && ENM2 > 0 && GameObject.FindGameObjectsWithTag ("ENM2").Length + GameObject.FindGameObjectsWithTag ("ENM").Length < 4 && pos11 == false) {
			Instantiate (Enemy2, pos1.position, pos1.rotation);
			pos11=true;
			ENM2 -= 1;
		}
		
		if (ENM1 == 0 && ENM2 > 0 && GameObject.FindGameObjectsWithTag ("ENM2").Length + GameObject.FindGameObjectsWithTag ("ENM").Length < 4 && pos12 == false) {
			Instantiate (Enemy2, pos2.position, pos2.rotation);
			pos12=true;
			ENM2 -= 1;
		}
		
		if (ENM1 == 0 && ENM2 > 0 && GameObject.FindGameObjectsWithTag ("ENM2").Length + GameObject.FindGameObjectsWithTag ("ENM").Length < 4 && pos13 == false) {
			Instantiate (Enemy2, pos3.position, pos3.rotation);
			pos13=true;
			ENM2 -= 1;
		}
		if (ENM1 == 0 && ENM2 > 0 && GameObject.FindGameObjectsWithTag ("ENM2").Length + GameObject.FindGameObjectsWithTag ("ENM").Length < 4 && pos14 == false) {
			Instantiate (Enemy2, pos4.position, pos4.rotation);
			pos14=true;
			ENM2 -= 1;
		}
		
		
		if (ENM2 == 0 && ENM1 > 0 && GameObject.FindGameObjectsWithTag ("ENM2").Length + GameObject.FindGameObjectsWithTag ("ENM").Length < 4 && pos11 == false) {
			
			Instantiate (Enemy1,pos1.position,pos1.rotation);
			pos11=true;
			ENM1 -=1;
		}
		if (ENM2 == 0 && ENM1 > 0 && GameObject.FindGameObjectsWithTag ("ENM2").Length + GameObject.FindGameObjectsWithTag ("ENM").Length < 4 && pos12 == false) {
			
			Instantiate (Enemy1,pos2.position,pos2.rotation);
			pos12=true;
			ENM1 -=1;
		}
		if (ENM2 == 0 && ENM1 > 0 && GameObject.FindGameObjectsWithTag ("ENM2").Length + GameObject.FindGameObjectsWithTag ("ENM").Length < 4 && pos13 == false) {
			Instantiate (Enemy2, pos3.position, pos3.rotation);
			pos13=true;
			ENM2 -= 1;
			
		}
		if (ENM2 == 0 && ENM1 > 0 && GameObject.FindGameObjectsWithTag ("ENM2").Length + GameObject.FindGameObjectsWithTag ("ENM").Length < 4 && pos14 == false) {
			Instantiate (Enemy1,pos4.position,pos4.rotation);
			pos14=true;
			ENM1 -=1;
			
		}
		
		
		if (ENM1 + ENM2 == 0 && GameObject.FindGameObjectsWithTag ("ENM2").Length + GameObject.FindGameObjectsWithTag ("ENM").Length == 0) {

			Invoke("next",3);

		}

       if (anim.enabled == true && !a || TANK==null ) {

			Invoke("gameover",1.5f);
			a=true;
		}




	}
	public void next(){

		PlayerPrefs.SetString ("stage2", "on");
		Application.LoadLevel("lave2");
	}

	public void gameover(){
		planGameOver.SetActive (true);
		Time.timeScale = 0;
	}
}