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

public class GroundPanel : MonoBehaviour {

    public bool[] systemMessages = new bool[6];
    public float[] systemHealth = new float[6];
    public string[] realMessage;
    public bool gameStarted = false;

    //UI connections
    public Text textLine; //text prefab
    public InputField textnput; //input UI
    public Text[] panels;
    public RectTransform[] bars;


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

    public GameObject MainUI;

    // Use this for initialization
    void Start()
    {
        systemMessages = new bool[6];
        systemHealth = new float[6];
        realMessage = new string[6];
        for (int i = 0; i < 6; i++)
        {
            systemHealth[i] = 1;
            systemMessages[i] = false;
            realMessage[i] = "";
        }
    }

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

    public void SendMessage()
    {
        if (textnput.text != "")
        {
            GetComponent<NetworkClient>().UDPSend("MSG:" + textnput.text);
        }
        textLine.text += "\n" + textnput.text;
    }

    public void SystemUpdate(float Energy, string EnergyError, float Life, string LifeError, float Water, string WaterError, float Comms, string CommsError, float Nav, string NavError, float CPU, string CPUError)
    {
        string[] tmp;
        systemHealth[(int)SystemType.Energy] = Energy;
        systemHealth[(int)SystemType.Life] = Life;
        systemHealth[(int)SystemType.Water] = Water;
        systemHealth[(int)SystemType.Communication] = Comms;
        systemHealth[(int)SystemType.Navigation] = Nav;
        systemHealth[(int)SystemType.Computer] = CPU;
        bars[(int)SystemType.Energy].localScale = new Vector3(Mathf.Max(Energy / 100,0), 1, 1);
        bars[(int)SystemType.Life].localScale = new Vector3(Mathf.Max(Life / 100, 0), 1, 1);
        bars[(int)SystemType.Water].localScale = new Vector3(Mathf.Max(Water / 100, 0), 1, 1);
        bars[(int)SystemType.Communication].localScale = new Vector3(Mathf.Max(Comms / 100, 0), 1, 1);
        bars[(int)SystemType.Navigation].localScale = new Vector3(Mathf.Max(Nav / 100, 0), 1, 1);
        bars[(int)SystemType.Computer].localScale = new Vector3(Mathf.Max(CPU / 100, 0), 1, 1);

        if(CPU <= 0 || Nav <= 0 || Comms <= 0 || Energy <= 0 || Life <= 0 || Water <= 0)
        {
            SceneManager.LoadScene(4);
        }

        if (EnergyError == "0")
        {
            systemMessages[(int)SystemType.Energy] = false;
            panels[(int)SystemType.Energy].text = "System Okay.";
        }
        else
        {
            tmp = EnergyError.Split("|"[0]);
            systemMessages[(int)SystemType.Energy] = true;
            realMessage[(int)SystemType.Energy] = Localizer.getText("energy", int.Parse(tmp[4])) + "\n" + GetCommandString(tmp[1], tmp[2], tmp[3]);
            panels[(int)SystemType.Energy].text = realMessage[(int)SystemType.Energy];
        }

        if (LifeError == "0")
        {
            systemMessages[(int)SystemType.Life] = false;
            panels[(int)SystemType.Life].text = "System Okay.";
        }
        else
        {
            tmp = LifeError.Split("|"[0]);
            systemMessages[(int)SystemType.Life] = true;
            realMessage[(int)SystemType.Life] =  Localizer.getText("life", int.Parse(tmp[4])) + "\n" + GetCommandString(tmp[1], tmp[2], tmp[3]);
            panels[(int)SystemType.Life].text = realMessage[(int)SystemType.Life];
        }

        if (WaterError == "0")
        {
            systemMessages[(int)SystemType.Water] = false;
            panels[(int)SystemType.Water].text = "System Okay.";
        }
        else
        {
            tmp = WaterError.Split("|"[0]);
            systemMessages[(int)SystemType.Water] = true;
            realMessage[(int)SystemType.Water] = Localizer.getText("water", int.Parse(tmp[4])) + "\n" + GetCommandString(tmp[1], tmp[2], tmp[3]);
            panels[(int)SystemType.Water].text = realMessage[(int)SystemType.Water];
        }


        if (CommsError == "0")
        {
            systemMessages[(int)SystemType.Communication] = false;
            panels[(int)SystemType.Communication].text = "System Okay.";
        }
        else
        {
            tmp = CommsError.Split("|"[0]);
            systemMessages[(int)SystemType.Communication] = true;
            realMessage[(int)SystemType.Communication] =  Localizer.getText("comm", int.Parse(tmp[4]))+"\n"+GetCommandString(tmp[1],tmp[2],tmp[3]);
            panels[(int)SystemType.Communication].text = realMessage[(int)SystemType.Communication];
        }


        if (NavError == "0")
        {
            systemMessages[(int)SystemType.Navigation] = false;
            panels[(int)SystemType.Navigation].text = "System Okay.";
        }
        else
        {
            tmp = NavError.Split("|"[0]);
            systemMessages[(int)SystemType.Navigation] = true;
            realMessage[(int)SystemType.Navigation] = Localizer.getText("nav", int.Parse(tmp[4])) + "\n" + GetCommandString(tmp[1], tmp[2], tmp[3]);
            panels[(int)SystemType.Navigation].text = realMessage[(int)SystemType.Navigation];
        }


        if (CPUError == "0")
        {
            systemMessages[(int)SystemType.Computer] = false;
            panels[(int)SystemType.Computer].text = "System Okay.";
        }
        else
        {
            tmp = CPUError.Split("|"[0]);
            systemMessages[(int)SystemType.Computer] = true;
            realMessage[(int)SystemType.Computer] = Localizer.getText("computer", int.Parse(tmp[4])) + "\n" + GetCommandString(tmp[1], tmp[2], tmp[3]);
            panels[(int)SystemType.Computer].text = realMessage[(int)SystemType.Computer];
        }
    }

    public string GetCommandString(string mode, string target, string param)
    {
        string outData = "";
        switch (mode)
        {
            case "Ho":
                outData = "0xb5 |=| " + param;
                break;
            case "Pr":
                outData = "0xb"+ target;
                break;
            case "Sl":
                switch (int.Parse(target)) {
                    case 0:
                        outData = "0xa1B";
                        break;
                    case 1:
                        outData = "0xa1G";
                        break;
                    case 2:
                        outData = "0xa1Y";
                        break;
                }
                outData += " = " + param;
                break;
            case "To":
                outData = "0xc"+target+" = "+param;
                break;
            case "Tu":
                outData = "0xd0 = " + param;
                break;
            case "Wi":
                
                outData += "->";
                switch (int.Parse(param))
                {
                    case 0:
                        outData = "0x0a";
                        break;
                    case 1:
                        outData = "0x1a";
                        break;
                    case 2:
                        outData = "0x2a";
                        break;
                    case 3:
                        outData = "0x3a";
                        break;
                    case 4:
                        outData = "0x0b";
                        break;
                    case 5:
                        outData = "0x1b";
                        break;
                    case 6:
                        outData = "0x2b";
                        break;
                    case 7:
                        outData = "0x3b";
                        break;
                    case 8:
                        outData = "0x0c";
                        break;
                    case 9:
                        outData = "0x1c";
                        break;
                    case 10:
                        outData = "0x2c";
                        break;
                    case 11:
                        outData = "0x3c";
                        break;
                    case 12:
                        outData = "0x0d";
                        break;
                    case 13:
                        outData = "0x1d";
                        break;
                    case 14:
                        outData = "0x2d";
                        break;
                    case 15:
                        outData = "0x3d";
                        break;
                }
                break;
        }
        return outData;
    }
}
