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

public class PizzaMakerManager : MonoBehaviour {

	//public GameObject PizzaTable;
	public Transform PizzaLocation;

	public GameObject scoreManager;
	public Text gameOverText;
	public Text recipeText;
	public Text pizzaStatus;
	
	private GameObject pizzaContainer;

	private int TOPPING_BASE = 0;
	private int TOPPING_CHEESE = 1;
	private int TOPPING_PEPPERONI = 2;
	private int TOPPING_PEPPER = 3;
	private int TOPPING_OLIVE = 4;
	private int TOPPING_MUSHROOM = 5;
	private int TOPPING_PINEAPPLE = 6;
	private int TOPPING_HAM = 7;
	private int TOPPING_ONION = 8;

	private int TOPPING_COUNT = 9;

	public string[] TOPPING_NAME;
	public float[] TOPPING_ENABLED;
	public float[] TOPPING_DAMAGE;

	private float pizzaValue;

	private int PIZZA_NONE = 0;
	private int PIZZA_BASE = 1;
	private int PIZZA_THROWN = 2;
	private int PIZZA_OVER = 99;

	public int pizzaState = 0;

	// current pizza info
	private int layersApplied;
	private int[] layerOn;

	private GameObject selectedBadGuy = null;

	// Use this for initialization
	void Start () {
		
		
		pizzaStatus.text = "";	
	
		gameOverText.enabled = false;
		
		TOPPING_ENABLED = new float[TOPPING_COUNT];
		TOPPING_DAMAGE = new float[TOPPING_COUNT];
		TOPPING_NAME = new string[TOPPING_COUNT];
		
		layerOn = new int[TOPPING_COUNT];
		
		pizzaValue = 0;
		
		for (int i = 0; i < (TOPPING_COUNT); i++)
		{
			TOPPING_ENABLED[i] = 1.0f;
		}

		TOPPING_DAMAGE[TOPPING_CHEESE] 		= 0.5f;
		TOPPING_DAMAGE[TOPPING_PEPPERONI] 	= 2.0f;
		TOPPING_DAMAGE[TOPPING_PEPPER] 		= 2.0f;
		TOPPING_DAMAGE[TOPPING_OLIVE]	 	= 1.0f;
		TOPPING_DAMAGE[TOPPING_MUSHROOM]	= 1.0f;
		TOPPING_DAMAGE[TOPPING_PINEAPPLE] 	= 2.0f;
		TOPPING_DAMAGE[TOPPING_HAM] 		= 0.5f;
		TOPPING_DAMAGE[TOPPING_ONION] 		= 1.5f;

		TOPPING_NAME[TOPPING_CHEESE] 		= "Cheese";
		TOPPING_NAME[TOPPING_PEPPERONI] 	= "Pepperoni";
		TOPPING_NAME[TOPPING_PEPPER] 		= "Pepper";
		TOPPING_NAME[TOPPING_OLIVE] 		= "Olive";
		TOPPING_NAME[TOPPING_MUSHROOM] 		= "Mushroom";
		TOPPING_NAME[TOPPING_PINEAPPLE] 	= "Pineapple";
		TOPPING_NAME[TOPPING_HAM] 			= "Ham";
		TOPPING_NAME[TOPPING_ONION] 		= "Onion";
		
		selectedBadGuy = null;
		
		NewPizza();
	
	}
	
	// Update is called once per frame
	void Update () {
	
		if (pizzaState == PIZZA_THROWN)
		{
			//transform.Translate (0,0, 10 * Time.deltaTime);
			
			PizzaContainer pc = pizzaContainer.GetComponent<PizzaContainer>();
			if (selectedBadGuy != null)
			{
				Debug.Log ("pizzaTarget: " + selectedBadGuy.GetComponent<BadGuy>().pizzaTarget.position);
				pc.FlingIt(selectedBadGuy.GetComponent<BadGuy>().pizzaTarget);
			}
			else
				pc.FlingIt(null);		
	
	
			// off end of floor
			if (pc.transform.position.z > 16)	
			{
				DestroyObject (pizzaContainer);
				pizzaState = PIZZA_NONE;
				NewPizza();
			}
			// hit BadGuy
			else if (Vector3.Distance (pc.transform.position, selectedBadGuy.GetComponent<BadGuy>().pizzaTarget.position) < .5f )
			{
				selectedBadGuy.GetComponent<BadGuy>().badGuyManager.damage(selectedBadGuy, pizzaValue, layerOn);

				DestroyObject (pizzaContainer);
				pizzaState = PIZZA_NONE;
				NewPizza();
				
				//selectedBadGuy.GetComponent<BadGuy>().die();
				
			}
												
		}
	
	
	}
	
	
	
	public void PressedTopping(int topping)
	{
		if ( (TOPPING_ENABLED[topping] >= 0) && (layerOn[topping] == 0) && pizzaState != PIZZA_THROWN && pizzaState != PIZZA_OVER)
		{
			layersApplied++;
			AddToPizza(topping, layersApplied);
			layerOn[topping] = 1;
			pizzaValue += TOPPING_DAMAGE[topping];
			updateRecipeText();
		}
	}

	private void AddToPizza(int topping, int layeroffset)
	{
		Debug.Log ("Add to Pizza: " + TOPPING_NAME[topping]);
		
		GameObject go = GetPrefab (topping);
		
		if (go != null)
		{		
			Vector3 v = new Vector3(PizzaLocation.position.x, PizzaLocation.position.y + (layeroffset / 1000f), PizzaLocation.position.z);	
			go.transform.position = v;
			go.transform.SetParent(pizzaContainer.transform);
						

		}		
	}	
	
	private void updateRecipeText()
	{
		recipeText.text = "Pizza Damage: <color='RED'>" + pizzaValue + "</color>\n";
	
		if (selectedBadGuy != null)
		{
			string w = selectedBadGuy.GetComponent<BadGuy>().getWeakness();
			if (w  != "")
				recipeText.text += "This Zombie is weak against " + w + " specialty!\n";
		}
		
		for (int i = 0; i < (TOPPING_COUNT); i++)
		{
			if (layerOn[i] > 0)
				recipeText.text += TOPPING_NAME[i] + "\n";
		}
		recipeText.text += "Click a target,then click on Pizza to LAUNCH!";
	}
	
	private GameObject GetPrefab(int p)
	{
		string prefix = "Prefab/";
		string name = "";
		if (p == TOPPING_BASE)
			name = prefix + "PizzaLayerBase";
		if (p == TOPPING_CHEESE)
			name = prefix + "PizzaLayerCheese";
		if (p == TOPPING_PEPPERONI)
			name = prefix + "PizzaLayerPepperoni";
		if (p == TOPPING_PEPPER)
			name = prefix + "PizzaLayerPepper";
		if (p == TOPPING_OLIVE)
			name = prefix + "PizzaLayerOlive";
		if (p == TOPPING_MUSHROOM)
			name = prefix + "PizzaLayerMushroom";
		if (p == TOPPING_PINEAPPLE)
			name = prefix + "PizzaLayerPineapple";
		if (p == TOPPING_HAM)
			name = prefix + "PizzaLayerHam";
		if (p == TOPPING_ONION)
			name = prefix + "PizzaLayerOnion";
		
		
		
		
		if (name != "")
			return (GameObject)Instantiate(Resources.Load(name));
		else return null;
	
	}
	
	private void NewPizza()
	{
		recipeText.text = "";
		
		pizzaValue = 0;

		for(int i = 0; i < TOPPING_COUNT; i++)
		{
			layerOn[i] = 0;
		}
		
		pizzaContainer = new GameObject("PizzaContainer");
		pizzaContainer.transform.position = PizzaLocation.position;
		pizzaContainer.AddComponent<PizzaContainer>();
		
		layersApplied = 1;
		layerOn[TOPPING_BASE] = 1;
		AddToPizza(TOPPING_BASE,layersApplied);
		
		updateRecipeText();
		
		
		pizzaState = PIZZA_BASE;
	
	}
	
	public void ThrowPizza()
	{
		Debug.Log ("Pizza Thrown.");
				
		PizzaSpeciality ps = new PizzaSpeciality(layerOn);
		string sp = ps.CheckSpeciality();
		
		if (sp != "")			
			pizzaStatus.text = "You made a " + sp + " special!";		
		else
			pizzaStatus.text = "";	
		
		
		Vector3 adjdeg = new Vector3(-10,0,0);
		pizzaContainer.transform.Rotate (adjdeg);
		
		pizzaState = PIZZA_THROWN;
		
	}
	
	
	
	public void setCurrentBadGuy(GameObject bg)
	{
		pizzaStatus.text = "";
		selectedBadGuy = bg;
		updateRecipeText();
	}
	
	public void gameOver()
	{
		gameOverText.enabled = true;
		pizzaState = PIZZA_OVER;
	}
}
