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

public class DemonSpawner : MonoBehaviour {

	/*
	 * - Spawn demon every X seconds
	 * - Give players a warning Y seconds before demon spawns.  Actually SHOW a placeholder
	 * where the demon lord is trying to appear.  Maybe also an arrow?
	 * - Demon lifespan increases after every wave.
	 * - Also spawn demon minions!
	 * - Name demon lords after donators!
	 * 
	 */

	public GameObject[] demonLordPrefabs;

	float demonSpawnRate = 20f;
	float demonSpawnCooldown = 30f;

	GameObject currentDemonLord;

	public GameObject announcePanel;
	public Text txtDemonLord;
	public Text txtDemonShadow;

	public Text txtHP;
	public Text txtHPShadow;

	string nextDemonLordName;
	Vector2 nextDemonSpawnSpot;
	bool playedNextSound = false;

	public Text txtWaveNum;

	int nextWave = 0;

	GameObject portal;

	string[] demonNames = {
		"Akiss4luck", "Briarstone", "Brother_none", "Demonac10", "Emmaop12", "Essentia_Modica", "Evangeleigh", "Involution_", "Macedverji", "Moobot", "MS Carsonelle", "Niicks", "Pugsieandmoo", "Pyrotyger", "Animehero", "Zentrigger",
		"a_pox",
		"AeroEngiNerd",
		"Alavard_",
		"AlexParadax",
		"Androdi",
		"Anonymous",
		"Armok",
		"Baldricks_Cunning_Plan",
		"bedhead95",
		"Bezz247",
		"Bonejammin",
		"bosstone4242",
		"Captain_Brownpants",
		"Chan1254",
		"Chrisernja",
		"Ciclavex",
		"Cpt_Matt",
		"DarkwinterDreams",
		"DeadM8",
		"DeltaGeek",
		"doggyman10",
		"DoomDutch",
		"DougieM96",
		"Draugo",
		"DrTechnoBabel",
		"edwardyule",
		"Elfcirynn",
		"Faintspekaer",
		"FritiFirecaster",
		"Gamergazzan",
		"GardenofSinners2015",
		"Gizmore",
		"Gladitor117",
		"GlarionDnD",
		"glewellyn",
		"guard12202",
		"GurliGebis",
		"Hallurl",
		"Haza",
		"Haza290",
		"HendrikNL",
		"illinifan4249",
		"InfiniteG",
		"InfiniteG",
		"Insanity678",
		"IUPsohyda",
		"Jaholor",
		"JezaGaia",
		"Jouna",
		"krismoellegaard",
		"Lijpo123456",
		"Loooodvig",
		"Louie_the_fly",
		"lowercaserho",
		"Macailean",
		"MarcoPolochina",
		"mechpanties",
		"Ndbattlehunter",
		"NecroPlague",
		"nicondk",
		"Nirolosa",
		"Not So Silent Bob",
		"Nova_Kast",
		"noveau_barbier",
		"Olivier_Francois",
		"OranJay54",
		"Panzer1509",
		"PotatoAndTaco",
		"PrinceOfBrussels",
		"ProfessorPutricide2010",
		"QuiveringCactus",
		"Raikiir",
		"Raskolnikov1866",
		"Rawrmonger",
		"RhysGwynt",
		"RimePendragon",
		"RocMage",
		"Roulger",
		"ruabadfish",
		"Ruby_Rose",
		"Runalotski",
		"SafetyLast",
		"SaIIboT666",
		"Savi901",
		"SedativeMan",
		"Shanix",
		"sikrin",
		"Sintho1",
		"Sir Linder",
		"skullteria",
		"Sneaky",
		"sodacycle",
		"SomeAndyGuy",
		"SpoonkDK",
		"SugarSoap",
		"supernova1934",
		"Tabasco",
		"The_Avrowolf",
		"The_Avrowolf",
		"Themata075",
		"theRealFlobo",
		"thornation92",
		"TJ Scarrott",
		"TKxDestruction",
		"TracyBluthRIP",
		"ttocs19",
		"UMOarsman",
		"UnraveledMnd",
		"Urdnot_Scott",
		"Valiantcakefiend",
		"Valinor000",
		"warthurton",
		"Weegemonster5000",
		"WhyMe95",
		"wolfhoundlewis",
		"Wuddel79",
		"xDopeBoyMagik",
		"xthomassenx",
		"xtoxicsquierlx",
		"YukiHyou",
		"Zaraelys",
		"Zhatelier",
	};

	string[] demonTitle1 = {
		"The Destroyer",
		"The Consumer",
		"The Devourer",
		"CEO",
		"The Stackwiper",
		"The Eater",
		"Project Manager",
		"The Lawyer",
		"The Trumper",
		"The Bane",
		"The Doom",
		"The Annihilator",
		"The Deceiver",
		"The Harbinger",
		"The Harvester",
		"The Violator",
		"The Demise",
		"The Betrayer",
		"The Lord",
		"The King",
		"The Ruler",
		"The Patriarch",
		"The Matriarch",
		"The Count",
		"The Duke",
		"The Reaper",
		"The Defiler",
		"The Corruptor",
		"The Czar",
		"The Khan",
		"The Disemboweler",
		"The Old One",
		"The Elder",
		"The Binding",
		"The Banhammer",
	};

	string[] demonTitle2 = {
		"of Worlds",
		"of Souls",
		"of Puppies",
		"of Armies",
		"of EA",
		"of Ludum Dare",
		"of the Internet",
		"of Brussels",
		"of Sorrow",
		"of Terror",
		"of Anarchy",
		"of Sandstorms",
		"of Epochs",
		"of Centuries",
		"of Darkness",
		"of Light",
		"of Shadows",
		"of Conquest",
		"of Death",
		"of Sickness",
		"of Disease",
		"of Pain",
		"of Waffles",
		"of Repentance",
		"of Ancients",
		"of Carnage",
		"of Desolation",
		"of The Void",
		"of Blasphemy",
		"of Twitch Chat",
	};

	// Use this for initialization
	void Start () {
		portal = GameObject.Find ("Portal");
		GenerateNextDemonLord();
	}

	void GenerateNextDemonLord() {
		nextWave++;

		nextDemonLordName = demonNames[Random.Range(0, demonNames.Length)] + ", " + 
				demonTitle1[Random.Range(0, demonTitle1.Length)] + " " + 
				demonTitle2[Random.Range(0, demonTitle2.Length)];

		nextDemonSpawnSpot = (Vector3)Random.insideUnitCircle * 50f + portal.transform.position;
		playedNextSound = false;
	}

	int NextHPs() {
		return (int)Mathf.Min(1000000000, 50 * Mathf.Pow (1.35f, nextWave));
	}

	// Update is called once per frame
	void Update () {
		txtWaveNum.text = "Wave #" + (nextWave-1);

		if(currentDemonLord == null) {
			demonSpawnCooldown -= Time.deltaTime;

			if(Input.GetKeyDown(KeyCode.D) && Input.GetKey(KeyCode.LeftShift)) {
				demonSpawnCooldown = 0;
			}

			if(demonSpawnCooldown <= 0) {
				demonSpawnCooldown = demonSpawnRate;

				announcePanel.SetActive(false);

				currentDemonLord = (GameObject)Instantiate(
					demonLordPrefabs[Random.Range(0, demonLordPrefabs.Length)], nextDemonSpawnSpot, Quaternion.identity);

				currentDemonLord.name = nextDemonLordName;

				currentDemonLord.GetComponent<DemonLord>().initLifespan = NextHPs();


				GenerateNextDemonLord();
			}
			else if (demonSpawnCooldown <= 5f) {
				announcePanel.SetActive(true);
				txtDemonLord.text = nextDemonLordName;
				txtDemonShadow.text = nextDemonLordName;

				txtHP.text 		 = string.Format("{0:n0}", NextHPs()) + " HPs";
				txtHPShadow.text = string.Format("{0:n0}",  NextHPs()) + " HPs";

				if(playedNextSound == false) {
					SoundManager.current.PlaySound("Demon Lord Approaches");
					playedNextSound = true;
				}
			}
		}
	}
}
