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

public class EngineMinigameController : MonoBehaviour {


	public GameObject redCircle;
	private List<GameObject> redCircles = new List<GameObject> ();
	private List<Vector3> redCircleLocation = new List<Vector3>();
	private List<Vector3> redCircleGameLocation = new List<Vector3> ();
    private int damage = 3;

	public void populateRedCircles()
	{
		for (int i = 0; i < damage; i++) {
			GameObject tempCirc = Instantiate (redCircle) as GameObject;
			redCircles.Add (tempCirc);
			tempCirc.transform.SetParent (GameObject.FindGameObjectWithTag ("EngineImage").transform);
		}
	}

	public bool checkEngineStatus()
	{
        if (redCircles.Count != damage)
            return false;
       	for (int i = 0; i < redCircles.Count; i++) 
		{
			if (!redCircles [i].GetComponent<RedCircleLogic> ().isFinished ())
                
				return false;
		}

		return true;

		//return true;
	}

	public void populateLocations()
	{
		redCircleLocation.Add (new Vector3 (-100, 0, 0));
		redCircleLocation.Add (new Vector3 (-50, 0, 0));
		redCircleLocation.Add (new Vector3 (50, 0, 0));
		redCircleLocation.Add (new Vector3 (100, 0, 0));
		redCircleLocation.Add (new Vector3 (0, 0, 0));
		redCircleLocation.Add (new Vector3 (0, 50, 0));
		redCircleLocation.Add (new Vector3 (0, 100, 0));
		redCircleLocation.Add (new Vector3 (0, -50, 0));
		redCircleLocation.Add (new Vector3 (0, -100, 0));
	}

	public void assignSpots()
	{
		
		for (int i = 0; i < redCircles.Count; i++) {
			int x = Random.Range (0, redCircleLocation.Count);
			redCircleGameLocation.Add (redCircleLocation [x]);
			redCircleLocation.RemoveAt (x);
		}
	}

	public void distributeCircles()
	{
		for (int i = 0; i < redCircles.Count; i++)
		{
			redCircles [i].GetComponent<RectTransform> ().localPosition = redCircleGameLocation [i];

		}
	}

	public void assignCircleHits()
	{
		for (int i = 0; i < redCircles.Count; i++) {
            int num = Random.Range(1, 4);

            redCircles [i].GetComponentInChildren<Text> ().text = num.ToString ();
            redCircles[i].GetComponent<RedCircleLogic>().hits = num;
		}
	}

    public void resetEngineMiniGame()
    {
        redCircleGameLocation.Clear();
        populateLocations();        
        assignSpots();
        distributeCircles();
        assignCircleHits();
        

    }

    void Awake()
	{
        populateRedCircles();
        resetEngineMiniGame();
	}

}
