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

public class TomPanel : MonoBehaviour {

    public Queue<string>[] systemError;
    public bool[] systemMessages;
    public float[] systemHealth;
    public int brokenCount = 0;
    public float rollTimer = 0;
    public float rollLimit = 5;
    public bool gameStarted = false;

    public UIValue dial;
    public UIValue[] rockers;
    public UIValue[] Sliders;
    public UIValue[] button;
    public UIValue hold;
    public UIValue breaker;
    public UIValue[] wires;

    public enum SystemType {
        Energy = 0,
        Life = 1,
        Water = 2,
        Communication = 3,
        Navigation = 4,
        Computer = 5
    }

    public enum TaskType {
        Wire = 0,
        Hold = 1,
        Toggle = 2,
        Press = 3,
        Slide = 4,
        Turn = 5
    }


    public GameObject MainUI;
    public Text mainText;

    // Use this for initialization
    void Start () {
        systemMessages = new bool[6];
        systemHealth = new float[6];
        for (int i = 0; i < 6; i++)
        {
            systemHealth[i] = 100;
            systemMessages[i] = false;
        }
        systemError = new Queue<string>[6];
        systemError[(int)SystemType.Energy] = new Queue<string>();
        systemError[(int)SystemType.Life] = new Queue<string>();
        systemError[(int)SystemType.Water] = new Queue<string>();
        systemError[(int)SystemType.Communication] = new Queue<string>();
        systemError[(int)SystemType.Navigation] = new Queue<string>();
        systemError[(int)SystemType.Computer] = new Queue<string>();

    }
	
	// Update is called once per frame
	void Update () {

        if (systemHealth[(int)SystemType.Energy] <= 0 || systemHealth[(int)SystemType.Life] <= 0 || systemHealth[(int)SystemType.Water] <= 0 || systemHealth[(int)SystemType.Communication] <= 0 || systemHealth[(int)SystemType.Navigation] <= 0 || systemHealth[(int)SystemType.Computer] <= 0)
        {
            SceneManager.LoadScene(4);
        }

        //breaking
        if (gameStarted  && brokenCount < 6 && rollTimer >= rollLimit)
        {


            rollTimer = 0;
            //roll for things breaking
            int roll = Random.Range(1, 19);
            if (roll > 10 + brokenCount)
            {
                //break something
                roll = Random.Range(0, 6);
                if (!systemMessages[roll])
                {
                    int tasks = Random.Range(2, 8);
                    int reason = Random.Range(0, CountMessages((SystemType)roll));
                    for (int i = 0; i < tasks; i++)
                    {

                        TaskType taskType = (TaskType)Random.Range(0, 6);
                        systemError[roll].Enqueue(SetTargets(taskType, reason));
                        
                    }
                    systemMessages[roll] = true;
                    brokenCount++;
                }
            }
        }
        else
        {
            rollTimer += Time.deltaTime;
        }

        //hp update
        for (int i = 0; i < 6; i++)
        {
            if (systemMessages[i])
            {
                if (CheckTask(systemError[i].Peek()))
                {
                    Debug.Log("Solved : " + systemError[i].Peek());
                    systemError[i].Dequeue();
                    if (systemError[i].Count == 0)
                    {
                        systemMessages[i] = false;
                    }
                }
                else
                {
                    systemHealth[i] -= Time.deltaTime;
                }
            }
        }

       
    }

    public int CountMessages(SystemType system)
    {
        switch (system)
        {
            case SystemType.Energy:
                return 4;
            case SystemType.Life:
                return 6;
            case SystemType.Water:
                return 3;
            case SystemType.Communication:
                return 2;
            case SystemType.Navigation:
                return 2;
            case SystemType.Computer:
                return 3;
        }
        return 0;
    }

    protected bool CheckTask(string task)
    {
        ///@todo this is where we check i the task is complete
        string[] tmp = task.Split("|"[0]);
        switch(tmp[0])
        {
            case "Ho":
                return hold.checkValue(float.Parse(tmp[2]));
                break;
            case "Pr":
                return button[int.Parse(tmp[1])].checkValue(int.Parse(tmp[2]));
                break;
            case "Sl":
                return Sliders[int.Parse(tmp[1])].checkValue(int.Parse(tmp[2]));
                break;
            case "To":
                return rockers[int.Parse(tmp[1])].checkValue(int.Parse(tmp[2]));
                break;
            case "To2":
                return breaker.checkValue(int.Parse(tmp[2]));
                break;
            case "Tu":
                return dial.checkValue(int.Parse(tmp[2]));
                break;
            case "Wi":
                return wires[0].checkValue(int.Parse(tmp[2]))|| wires[1].checkValue(int.Parse(tmp[2]))|| wires[2].checkValue(int.Parse(tmp[2])) || wires[3].checkValue(int.Parse(tmp[2]));
                break;
        }
        return false;
    }

    public string getError(SystemType system)
    {
        string outData = "";
        if (!systemMessages[(int)system])
        {
            outData = "0";
        }
        else
        {
            outData = "1|"+GetMessage(system);
        }
        return outData;
    }

    public string SetTargets(TaskType task, int messageID)
    {
        int startTarget;
        int valueTarget;
        float timeTarget;
        string outData = "";
        switch (task)
        {
            case TaskType.Hold:
                startTarget = 0;
                timeTarget = 1;
                outData = "Ho|" + startTarget + "|" + timeTarget+"|"+ messageID;
                break;
            case TaskType.Press:
                startTarget = Random.Range(0, 4);
                outData = "Pr|" + startTarget+"|"+1 + "|" + messageID;
                break;
            case TaskType.Slide:
                startTarget = Random.Range(0, 3);
                valueTarget = Random.Range(0, 10);
                outData = "Sl|" + startTarget + "|" + valueTarget + "|" + messageID;
                break;
            case TaskType.Toggle:
                startTarget = Random.Range(0, 3);
                valueTarget = Random.Range(0, 2);
                outData = "To|" + startTarget + "|" + valueTarget + "|" + messageID;
                break;
            case TaskType.Turn:
                startTarget = 0;
                valueTarget = Random.Range(0, 99);
                outData = "Tu|" + startTarget + "|" + valueTarget + "|" + messageID;
                break;
            case TaskType.Wire:
                startTarget = 0;
                valueTarget = Random.Range(0, 16);
                outData = "Wi|0|" + valueTarget + "|" + messageID;
                break;
        }
        return outData;
    }

    public string GetMessage(SystemType system)
    {
        string outData = systemError[(int)system].Peek();

        return outData;
    }
}
