using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StartScript : MonoBehaviour {
	
	bool finishedAsteroids = false;
	public int startDistance = 100;
	public int asteroidCount = 1000;
	public float asteroidDistance = 200f;
	public float generateRadius = 1000;
	public int asteroidsBeforeYield = 10;
	public GameObject asteroidPrefab;
	public Vector3 minASize;
	public Vector3 maxASize;
	public GameObject helpCanvas;
	public Transform[] Planets;


	void Awake(){
		GlobalScript.controlEnabled = false;
		StartCoroutine("GenerateAsteroids");
		Cursor.lockState = CursorLockMode.Locked;
	}

	// Use this for initialization
	void Update () {
		if(!GlobalScript.controlEnabled && finishedAsteroids && !GlobalScript.planetaryMovementEnabled){
			GlobalScript.controlEnabled = true;
			GlobalScript.planetaryMovementEnabled = true;
			helpCanvas.SetActive(true);
			GlobalScript.FadeIn(2f);
		}
	}
	
	IEnumerator GenerateAsteroids(){
		for(int i = startDistance; i < asteroidCount; i++){
			if(i % asteroidsBeforeYield == 0)
				yield return null;

			bool inPlanetPath = false;

			foreach(Transform p in Planets){
				if(Mathf.Abs(((float)i * (generateRadius / asteroidCount)) - p.transform.position.magnitude) < asteroidDistance){
					inPlanetPath = true;
					break;
				}
			}
			if(inPlanetPath){
				continue;
			}
				
			Vector2 randPos = Random.insideUnitCircle.normalized;
			Vector3 pos = new Vector3(randPos.x * ((float)i * (generateRadius / asteroidCount)), 0, randPos.y * ((float)i * (generateRadius / asteroidCount)));
			GameObject go = Instantiate<GameObject>(asteroidPrefab, pos, Quaternion.identity, transform);
			go.name = System.Convert.ToChar(Random.Range(65, 91)).ToString() + System.Convert.ToChar(Random.Range(65, 91)).ToString() + i.ToString().PadLeft(4, '0');
			Vector3 size = Vector3.Lerp(minASize, maxASize, Random.value);
			go.transform.localScale = size;
			/*
			Mesh mesh = go.GetComponent<MeshFilter>().mesh;
			List<Vector3> vertices = new List<Vector3>();
			List<Vector3> doneVerts = new List<Vector3>();
			Vector3 center;

			float offset = Random.Range(0, 20);

			for (int s = 0; s < mesh.vertices.Length; s ++)
			{
				vertices.Add(mesh.vertices[s]);
			}

			center = go.GetComponent<Renderer>().bounds.center;

			for (int v = 0; v < vertices.Count; v++)
			{
				bool used = false;
				for (int k = 0; k < doneVerts.Count; k++)
				{
					if (doneVerts[k] == vertices[v])
					{
						used = true;
					}
				}
				if (!used)
				{
					Vector3 curVector = vertices[v];
					doneVerts.Add(curVector);
					int smoothing = Random.Range(4, 6);
					Vector3 changedVector = (curVector + ((curVector - center) * (Mathf.PerlinNoise((float) v / offset, (float)v / offset)/smoothing)));
					for (int s = 0; s < vertices.Count; s ++)
					{
						if (vertices[s] == curVector)
						{
							vertices[s] = changedVector;
						}
					}
				}
			}

			mesh.SetVertices(vertices);
			mesh.RecalculateBounds();
			mesh.RecalculateNormals();
			*/
		}
		finishedAsteroids = true;
	}
}
