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

public class CharacterRandomiser : MonoBehaviour {
	
	[SerializeField]
	private TextAsset BoysFileLocation;
	[SerializeField]
	private TextAsset GirlsFileLocation;
	[SerializeField]
	private TextAsset LastFileLocation;
	[SerializeField]
	private TextAsset OriginFileLocation;
	[SerializeField]
	private TextAsset BloodFileLocation;
	
	
	
	//Text Assets for Character Generation
	string boysFile;
	string girlsFile;
	string lastFile;
	string originFile;
	string bloodFile;

	//Global name arrays
	List <string> girlsArr;
	List <string> boysArr;
	List <string> lastArr;
	List <string> originArr;
	List <string> bloodArr;

	public Text UICharacterDescription = null;
	[SerializeField]
	private float PopUpLength = 4.0f; //Time pop up lasts
	private float PopUpStart = 0.0f; //When in time did the pop up pop up

	private NewCharacter Person = null;

	void Start()
	{
		boysFile = BoysFileLocation.text;
		girlsFile = GirlsFileLocation.text;
		lastFile = LastFileLocation.text;
		originFile = OriginFileLocation.text;
		bloodFile = BloodFileLocation.text;
		
		boysArr = new List<string>();
		girlsArr = new List<string>();
		lastArr = new List<string>();
		bloodArr = new List<string>();
		originArr = new List<string>();
		
		char returnChar = '\n';
		//Boys
		string[] lines  = boysFile.Split(returnChar);
		foreach(string line in lines)
		{
			boysArr.Add(line);
		}
		
		//Girls
		lines = girlsFile.Split(returnChar);
		foreach(string line in lines)
		{
			girlsArr.Add(line);
		}
		
		//Last Name
		lines = lastFile.Split(returnChar);
		foreach(string line in lines)
		{
			lastArr.Add(line);
		}
		
		//Blood Type
		lines = bloodFile.Split(returnChar);
		foreach(string line in lines)
		{
			bloodArr.Add(line);
		}
		
		//Origin
		lines = originFile.Split(returnChar);
		foreach(string line in lines)
		{
			originArr.Add(line);
		}

		CreateNewPlayer ();
	}
	
	public void CreateNewPlayer()
	{
		Person = new NewCharacter(boysArr, girlsArr, lastArr, bloodArr, originArr);
		UICharacterDescription.text = Person.GetCharacter();
		UICharacterDescription.transform.parent.gameObject.SetActive(true);
		PopUpStart = Time.time;
	}

	void Update()
	{
		if (PopUpStart + PopUpLength < Time.time) 
		{
			UICharacterDescription.transform.parent.gameObject.SetActive(false);
		}

	}

	public NewCharacter GetPerson()
	{
		return Person;
	}


}

public class NewCharacter
{
	string gender;
	string first;
	string last;
	string blood;
	string age;
	string origin;

	float sizeMod;
	float speedMod;

	public NewCharacter(List<string> boysArr, List<string> girlsArr, List<string> lastArr, List<string> bloodArr, List<string> originArr)
	{		
		System.Random NumGen = new System.Random();
		int GenderDecider = NumGen.Next(0,2);
		if (GenderDecider == 0)
		{
			this.gender = "M";
			this.first = boysArr[NumGen.Next(0, boysArr.Count)];
		}
		else
		{
			this.gender = "F";
			this.first = girlsArr[NumGen.Next(0, girlsArr.Count)];
		}
		
		this.last = lastArr[NumGen.Next(0, lastArr.Count)];
		this.blood = bloodArr[NumGen.Next(0, bloodArr.Count)];
		this.origin = originArr[NumGen.Next(0, originArr.Count)];
		this.age = NumGen.Next(18,50).ToString();

		this.sizeMod = Random.Range (0.9f, 1.5f);
		this.speedMod = Random.Range (0.84f, 1.15f);  

		string OutData = string.Format("Character generated: {0}, {1}, {2}, {3}, {4}, {5}", first, last, gender, age, origin, blood);
		Debug.Log(OutData);
		string[] characterData = {first, last,gender, age, origin, blood};
	}

	public string GetCharacter()
	{
		string NewDescription = this.last + ", " + 
				this.first + System.Environment.NewLine + System.Environment.NewLine + 
				"Gender:   " + this.gender + System.Environment.NewLine +
				"Age:   " + this.age + System.Environment.NewLine +
				"Origin:   " + this.origin + System.Environment.NewLine +
				"Blood:   " + this.blood;
		return NewDescription;
	}

	public float GetSizeMod()
	{
		return sizeMod;
	}

	public float GetSpeedMod()
	{
		return speedMod;
	}
}
