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

public class RandomWord : MonoBehaviour {
	//This is for the Spawner to use.

	//Make a public string so we can fetch it with a other script later
	public string KeyWord = "";

	//Make this public and we can use other scripts to get this
	private TextMesh texter; //I Like using 3D text in game

	public GameObject SphereInstance;//This is where our game object is loaded in

	//This spawns 3 spheres when ever used.
	void SpawnSpheres(){
		string[] Words = { "great", "stage", "peak", "street", "please" };

		//First lets get the word for the round.
		int RandomNum = Random.Range(0,4);
		KeyWord = Words[RandomNum];

		//Set the blocks text to the key word so the user can see what word is needed.
		texter = this.transform.GetChild(0).transform.GetChild(0).GetComponent<TextMesh>();
		//To explain what I just did: Spawner has the Square as it's 0 child
		//the 3D text is also the 0 child of square. There are other ways to do this, this is just more reliable.
		texter.text = KeyWord;



		//Next we spawn 3 spheres 1 with the word and 2 dud words.
		GameObject WinSphere = Instantiate(SphereInstance,this.transform.localPosition,this.transform.localRotation,this.transform);//This adds a sphere to the spawner as a child
		WinSphere.transform.localPosition += new Vector3(Random.Range(-7,7),0,0);

		WinSphere.GetComponentInChildren<TextMesh> ().text = KeyWord;

		//make two duds to fool the player
		GameObject Dud1 = Instantiate(SphereInstance,this.transform.localPosition,this.transform.localRotation,this.transform);//This adds a sphere to the spawner as a child
		Dud1.transform.localPosition += new Vector3(Random.Range(-7,7),0,0);
		GameObject Dud2 = Instantiate(SphereInstance,this.transform.localPosition,this.transform.localRotation,this.transform);//This adds a sphere to the spawner as a child
		Dud2.transform.localPosition += new Vector3(Random.Range(-7,7),0,0);

		//Make a temp list to hold the fake words
		//int RandomDud = 

		Dud1.GetComponentInChildren<TextMesh> ().text = Words[RandomRangeButNot(RandomNum,0,4)];
		Dud2.GetComponentInChildren<TextMesh> ().text = Words[RandomRangeButNot(RandomNum,0,4)];
	}
	// Use this for initialization
	void Start () {
		SpawnSpheres ();
	}

	//This will give us a random number that isn't the random number used
	int RandomRangeButNot(int NumberNotToUse,int RangeA,int RangeB){
		int Output = NumberNotToUse;

		while (Output == NumberNotToUse) {
			Output = Random.Range (RangeA, RangeB);
		}
		return Output;
		//This function can take a long time with short ranges
	}

	//This will clean our scene and spawn 3 spheres again
	public void CleanupAndReset(){
		
		//Check every child to see if it is a sphere
		foreach(Transform Child in this.transform){
			if (Child != this.transform.GetChild (0)) {//Don't delete the firts one, it's the cube
				Destroy (Child.gameObject);
			}
				
		}
		SpawnSpheres ();
	}
		
	//This is how we stop the ran

}
