using UnityEngine;
using System.Collections;

public class CristalPainter : MonoBehaviour {
	public GameObject mAppManager;
	public int[] CristalEnemies = new int[4];
	public int  sum = 0;
	public int nofyellow = 0;
	public int nofred = 0;
	public int nofblue = 0;
	public int nofgreen = 0;
	//public int test =0;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		Painter (1);
	}

	public void Painter (int nruni)
	{
		Mesh mesh = GetComponent<MeshFilter>().mesh;
		Vector3[] vertices = mesh.vertices;
		//	test =vertices.Length;
		CristalEnemies [0] = mAppManager.GetComponent<AnomalyCounter> ().Enemies [nruni] [0];
		CristalEnemies [1] = mAppManager.GetComponent<AnomalyCounter> ().Enemies [nruni] [1];
		CristalEnemies [2] = mAppManager.GetComponent<AnomalyCounter> ().Enemies [nruni] [2];
		CristalEnemies [3] = mAppManager.GetComponent<AnomalyCounter> ().Enemies [nruni] [3];
		sum = CristalEnemies [0] + CristalEnemies [1] + CristalEnemies [2] + CristalEnemies [3];
		nofyellow = CristalEnemies [0] * vertices.Length/sum;
		nofred = CristalEnemies [1] * vertices.Length/sum;
		nofblue = CristalEnemies [2] * vertices.Length/sum;
		nofgreen = CristalEnemies [3] * vertices.Length/sum;
		
		int y = 0;
		int r = 0;
		int b = 0;
		int g = 0;
		
		
		
		Color[] colors = new Color[vertices.Length];
		int i = 0;
		
		while (i < vertices.Length) {
			
			int colornumber  = Mathf.RoundToInt(Random.value * 3 + 1);
			if(( colornumber == 1)&&(y < nofyellow)){
				colors[i] = new Color(1.0F,0.92F,0.016F);
				y += 1;
				
			}
			if(( colornumber == 2)&&(r < nofred)){
				colors[i] = new Color(1.0F,0.0F,0.0F);
				r += 1;
				
			}
			if(( colornumber == 3)&&(b < nofblue)){
				colors[i] = new Color(0.0F,0.0F,1.0F);
				b += 1;
				
			}
			if(( colornumber == 4)&&(g < nofgreen)){
				colors[i] = new Color(0.0F,1.0F,0.0F);
				g += 1;
				
			}

			i++;
		}
		mesh.colors = colors;
	}
}
