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

public class Controller : MonoBehaviour {
    public GameObject gameoverCanvas;

	public bool onPot = false;
	public Stir stir;
    public List<string> ings = new List<string>();
	public Transform items;

	public float RED_TIMER = 0f;
	public float GREEN_TIMER = 0f;
	public float YELLOW_TIMER = 0f;
	public float DARKGREEN_TIMER = 0f;
	public float MAGENTA_TIMER = 0f;
	public float MOUSTARD_TIMER = 0f;
	public float ORANGE_TIMER = 0f;
	public float BLUE_TIMER = 0f;
	public float CYAN_TIMER = 0f;
	public float PURPLE_TIMER = 0f;

	public enum GameState {
		WIN, PLAYING, GAMEOVER
	}

	public GameState gameState = GameState.PLAYING;

	private static Controller instance;

	public static Controller Instance {
		get {
			return instance;
		}
	}

	void Awake() {
		instance = this;
	}

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
        CheckForOpposites();
		CheckForNoIngredients();
        if (gameState == GameState.GAMEOVER)
        {
            gameoverCanvas.GetComponent<Canvas>().enabled = true;
            gameoverCanvas.GetComponentsInChildren<Text>()[0].text = "Game Over";
            gameoverCanvas.GetComponentsInChildren<Text>()[1].text = "Restart";
        }
        else if (gameState == GameState.WIN)
        {
            gameoverCanvas.GetComponent<Canvas>().enabled = true;
            gameoverCanvas.GetComponentsInChildren<Text>()[0].text = "Win";
            gameoverCanvas.GetComponentsInChildren<Text>()[1].text = "Next level";
        }
	}

    public void GameButtonClicked()
    {
        string text = gameoverCanvas.GetComponentsInChildren<Text>()[1].text;
        if (text.Equals("Restart")) {
            Application.LoadLevel(Application.loadedLevel);
        }
        else {
            Application.LoadLevel(Application.loadedLevel + 1);
        }
    }

	public void DropItem(GameObject item) {
		if (onPot) {
            ings.Insert(0,item.GetComponent<DragAndDrop>().ingredientName);
			Destroy(item);
			onPot = false;
		}
	}

	public void LeftStir() {

		stir.LeftStir();
        ings.Insert(0, "SL");
	}

    public void RightStir() {
        stir.RightStir();
        ings.Insert(0, "SR");
    }

	void CheckForNoIngredients() {
		if (items.childCount == 0) {
			gameState = GameState.GAMEOVER;
		}
	}

    void CheckForOpposites() {
        if (ings.Count > 1) {
            if (AreOpposites(ings[0], ings[1])) {
                ings.RemoveAt(0);
                ings.RemoveAt(0);
            }
        }
    }

    bool AreOpposites(string a, string b) {
        if (a == "Bone Dust" && b == "Dragon's Blood" || b == "Bone Dust" && a == "Dragon's Blood")
            return true;
        else if (a == "Poppy Seeds" && b == "Troll Snot" || b == "Poppy Seeds" && a == "Troll Snot")
            return true;
        else if (a == "SR" && b == "SL" || b == "SR" && a == "SL")
            return true;
        else if (a == "Stardust" && b == "Lava" || b == "Stardust" && a == "Lava")
            return true;
        else if (a == "Mermaid Findust" && b == "Kraken Ink" || b == "Mermaid Findust" && a == "Kraken Ink")
            return true;
        else if (a == "Grave Dirt" && b == "Unicorn Blood" || b == "Grave Dirt" && a == "Unicorn Blood")
            return true;
        else if (a == "Eye of The Tiger" && b == "Dragon's Claw" || b == "Eye of The Tiger" && a == "Dragon's Claw")
            return true;
        else if (a == "Flies" && b == "Wings of a Bat" || b == "Flies" && a == "Wings of a Bat")
            return true;
        return false;
    }
}
