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

public class StartingCalcs : MonoBehaviour {

    GameObject GLWealthVal, GLHealthVal, GLIndustryVal, GLDevVal;
    GameObject MoneyTextGL, BrainTextGL, FoodTextGL, FuelTextGL;
    [HideInInspector]public float GLWealthValue, GLHealthValue, GLIndustryValue, GLDevValue;
    [HideInInspector]public float MoneyValGL, BrainValGL, FoodValGL, FuelValGL;

    GameObject BBLWealthVal, BBLHealthVal, BBLIndustryVal, BBLDevVal;
    GameObject MoneyTextBBL, BrainTextBBL, FoodTextBBL, FuelTextBBL;
    float BBLWealthValue, BBLHealthValue, BBLIndustryValue, BBLDevValue;
    float MoneyValBBL, BrainValBBL, FoodValBBL, FuelValBBL;

    float RanNum1, RanNum2, RanNum3, RanNum4;

    public Text PercZion, PercArcadia, ResultText, ResultText2;
    string finText;

    private void Awake()
    {
        ResultText.text = "Share resources between countries and reach to absolute balance between them. \nFor absolute balance two countries need to be 100 in balance.";

        RanNum1 = Random.Range(3, 8)*10;
        RanNum2 = Random.Range(3, 8)*10;
        RanNum3 = Random.Range(3, 8)*10;
        RanNum4 = Random.Range(3, 8)*10;

        GLWealthVal = GameObject.Find("GLWealthValue");
        GLHealthVal = GameObject.Find("GLHealthValue");
        GLIndustryVal = GameObject.Find("GLIndustryValue");
        GLDevVal = GameObject.Find("GLDevValue");

        MoneyTextGL = GameObject.Find("MoneyValueTextGL");
        BrainTextGL = GameObject.Find("BrainValueTextGL");
        FoodTextGL = GameObject.Find("FoodValueTextGL");
        FuelTextGL = GameObject.Find("FuelValueTextGL");

        BBLWealthVal = GameObject.Find("BBLWealthValue");
        BBLHealthVal = GameObject.Find("BBLHealthValue");
        BBLIndustryVal = GameObject.Find("BBLIndustryValue");
        BBLDevVal = GameObject.Find("BBLDevValue");

        MoneyTextBBL = GameObject.Find("MoneyValueTextBBL");
        BrainTextBBL = GameObject.Find("BrainValueTextBBL");
        FoodTextBBL = GameObject.Find("FoodValueTextBBL");
        FuelTextBBL = GameObject.Find("FuelValueTextBBL");      
    }

    void Start()
    {
        //Level Beginning Random Values
        MoneyTextBBL.GetComponent<Text>().text = RanNum1.ToString();
        BrainTextBBL.GetComponent<Text>().text = RanNum2.ToString();
        FoodTextBBL.GetComponent<Text>().text = RanNum3.ToString();
        FuelTextBBL.GetComponent<Text>().text = RanNum4.ToString();
        
        MoneyTextGL.GetComponent<Text>().text = (110 - RanNum1).ToString();
        BrainTextGL.GetComponent<Text>().text = (110 - RanNum2).ToString();
        FoodTextGL.GetComponent<Text>().text = (110 - RanNum3).ToString();
        FuelTextGL.GetComponent<Text>().text = (110 - RanNum4).ToString();

        //GL Values
        MoneyValGL = int.Parse(MoneyTextGL.GetComponent<Text>().text);
        BrainValGL = int.Parse(BrainTextGL.GetComponent<Text>().text);
        FoodValGL = int.Parse(FoodTextGL.GetComponent<Text>().text);
        FuelValGL = int.Parse(FuelTextGL.GetComponent<Text>().text);

        GLWealthValue = (MoneyValGL / 2) + (FoodValGL / 2);
        GLHealthValue = (BrainValGL / 2) + (FoodValGL / 2);
        GLIndustryValue = (MoneyValGL / 2) + (FuelValGL / 2);
        GLDevValue = (BrainValGL / 2) + (FuelValGL / 2);

        GLWealthVal.GetComponent<Text>().text = GLWealthValue.ToString();
        GLHealthVal.GetComponent<Text>().text = GLHealthValue.ToString();
        GLIndustryVal.GetComponent<Text>().text = GLIndustryValue.ToString();
        GLDevVal.GetComponent<Text>().text = GLDevValue.ToString();

        //BBL Values
        MoneyValBBL = int.Parse(MoneyTextBBL.GetComponent<Text>().text);
        BrainValBBL = int.Parse(BrainTextBBL.GetComponent<Text>().text);
        FoodValBBL = int.Parse(FoodTextBBL.GetComponent<Text>().text);
        FuelValBBL = int.Parse(FuelTextBBL.GetComponent<Text>().text);

        BBLWealthValue = (MoneyValBBL / 2) + (FoodValBBL / 2);
        BBLHealthValue = (BrainValBBL / 2) + (FoodValBBL / 2);
        BBLIndustryValue = (MoneyValBBL / 2) + (FuelValBBL / 2);
        BBLDevValue = (BrainValBBL / 2) + (FuelValBBL / 2);

        BBLWealthVal.GetComponent<Text>().text = BBLWealthValue.ToString();
        BBLHealthVal.GetComponent<Text>().text = BBLHealthValue.ToString();
        BBLIndustryVal.GetComponent<Text>().text = BBLIndustryValue.ToString();
        BBLDevVal.GetComponent<Text>().text = BBLDevValue.ToString();

        PercantageCalculations();
    }

    public void GLReCalculate()
    {
        GLWealthValue = (MoneyValGL / 2) + (FoodValGL / 2);
        GLHealthValue = (BrainValGL / 2) + (FoodValGL / 2);
        GLIndustryValue = (MoneyValGL / 2) + (FuelValGL / 2);
        GLDevValue = (BrainValGL / 2) + (FuelValGL / 2);

        GLWealthVal.GetComponent<Text>().text = GLWealthValue.ToString();
        GLHealthVal.GetComponent<Text>().text = GLHealthValue.ToString();
        GLIndustryVal.GetComponent<Text>().text = GLIndustryValue.ToString();
        GLDevVal.GetComponent<Text>().text = GLDevValue.ToString();

        MoneyTextGL.GetComponent<Text>().text = MoneyValGL.ToString();
        BrainTextGL.GetComponent<Text>().text = BrainValGL.ToString();
        FoodTextGL.GetComponent<Text>().text = FoodValGL.ToString();
        FuelTextGL.GetComponent<Text>().text = FuelValGL.ToString();

        if (MoneyValGL == 0 || BrainValGL == 0 || FoodValGL == 0 || FuelValGL == 0)
        {
            finText = "Arcadia's sources are finished. Poeple died. Yor are doomed. Game Over!!";
            FinishingTexts(finText);
            Time.timeScale = 0;
        }
    }

    public void BBLReCalculate()
    { 
        BBLWealthValue = (MoneyValBBL / 2) + (FoodValBBL / 2);
        BBLHealthValue = (BrainValBBL / 2) + (FoodValBBL / 2);
        BBLIndustryValue = (MoneyValBBL / 2) + (FuelValBBL / 2);
        BBLDevValue = (BrainValBBL / 2) + (FuelValBBL / 2);

        BBLWealthVal.GetComponent<Text>().text = BBLWealthValue.ToString();
        BBLHealthVal.GetComponent<Text>().text = BBLHealthValue.ToString();
        BBLIndustryVal.GetComponent<Text>().text = BBLIndustryValue.ToString();
        BBLDevVal.GetComponent<Text>().text = BBLDevValue.ToString();

        MoneyTextBBL.GetComponent<Text>().text = MoneyValBBL.ToString();
        BrainTextBBL.GetComponent<Text>().text = BrainValBBL.ToString();
        FoodTextBBL.GetComponent<Text>().text = FoodValBBL.ToString();
        FuelTextBBL.GetComponent<Text>().text = FuelValBBL.ToString();

        if(MoneyValBBL == 0 || BrainValBBL == 0 || FoodValBBL == 0 || FuelValBBL == 0)
        {
            finText = "Zion's sources are finished. Poeple died. Yor are doomed. Game Over!!";
            FinishingTexts(finText);
        }
    }

    public void PercantageCalculations()
    {
        float x1 = 1, x2 = 1, y1 = 1, y2 = 1;

        float[] Arcadia = { GLWealthValue, GLHealthValue, GLIndustryValue, GLDevValue };
        float[] Zion = { BBLWealthValue, BBLHealthValue, BBLIndustryValue, BBLDevValue };
        System.Array.Sort(Zion);
        System.Array.Sort(Arcadia);

        float ArcadiaTotal = Arcadia[0] + Arcadia[1] + Arcadia[2] + Arcadia[3];
        float ZionTotal = Zion[0] + Zion[1] + Zion[2] + Zion[3];

        if((Arcadia[3] / Arcadia[0]) < 2 && Arcadia[3] > 50 && Arcadia[3] <= 60)
        {
            x1 = 1.5f;
        }
        else if ((Arcadia[3] / Arcadia[0]) < 2 && Arcadia[3] > 60 && Arcadia[3] <= 70)
        {
            x1 = 2f;
        }
        else if ((Arcadia[3] / Arcadia[0]) < 2 && Arcadia[3] > 70 && Arcadia[3] <= 80)
        {
            x1 = 2.5f;
        }
        else if ((Arcadia[3] / Arcadia[0]) < 2 && Arcadia[3] > 80 && Arcadia[3] <= 90)
        {
            x1 = 3f;
        }
        else if ((Arcadia[3] / Arcadia[0]) < 2 && Arcadia[3] > 90 && Arcadia[3] <= 100)
        {
            x1 = 3.5f;
        }
        if ((Arcadia[2] / Arcadia[1]) < 2 && Arcadia[3] > 50 && Arcadia[3] <= 60)
        {
            x2 = 1.5f;
        }
        else if ((Arcadia[2] / Arcadia[1]) < 2 && Arcadia[3] > 60 && Arcadia[3] <= 70)
        {
            x2 = 2f;
        }
        else if ((Arcadia[2] / Arcadia[1]) < 2 && Arcadia[3] > 70 && Arcadia[3] <= 80)
        {
            x2 = 2.5f;
        }
        else if ((Arcadia[2] / Arcadia[1]) < 2 && Arcadia[3] > 80 && Arcadia[3] <= 90)
        {
            x2 = 3f;
        }
        else if ((Arcadia[2] / Arcadia[1]) < 2 && Arcadia[3] > 90 && Arcadia[3] <= 100)
        {
            x2 = 3.5f;
        }
        //Zion
        if ((Zion[3] / Zion[0]) < 2 && Zion[3] > 50 && Zion[3] <= 60)
        {
            y1 = 1.5f;
        }
        else if ((Zion[3] / Zion[0]) < 2 && Zion[3] > 60 && Zion[3] <= 70)
        {
            y1 = 2f;
        }
        else if ((Zion[3] / Zion[0]) < 2 && Zion[3] > 70 && Zion[3] <= 80)
        {
            y1 = 2.5f;
        }
        else if ((Zion[3] / Zion[0]) < 2 && Zion[3] > 80 && Zion[3] <= 90)
        {
            y1 = 3f;
        }
        else if ((Zion[3] / Zion[0]) < 2 && Zion[3] > 90 && Zion[3] <= 100)
        {
            y1 = 3.5f;
        }
        if ((Zion[2] / Zion[1]) < 2 && Zion[3] > 50 && Zion[3] <= 60)
        {
            y2 = 1.5f;
        }
        else if ((Zion[2] / Zion[1]) < 2 && Zion[3] > 60 && Zion[3] <= 70)
        {
            y2 = 2f;
        }
        else if ((Zion[2] / Zion[1]) < 2 && Zion[3] > 70 && Zion[3] <= 80)
        {
            y2 = 2.5f;
        }
        else if ((Zion[2] / Zion[1]) < 2 && Zion[3] > 80 && Zion[3] <= 90)
        {
            y2 = 3f;
        }
        else if ((Zion[2] / Zion[1]) < 2 && Zion[3] > 90 && Zion[3] <= 100)
        {
            y2 = 3.5f;
        }
        float ArcadiaResult = ArcadiaTotal / ((((Arcadia[3] / Arcadia[0])*x1) + ((Arcadia[2] / Arcadia[1])*x2)));
        float ZionResult = ZionTotal / ((((Zion[3] / Zion[0])*y1) + ((Zion[2] / Zion[1])*y2)));

        PercArcadia.text = Mathf.RoundToInt(ArcadiaResult).ToString();
        PercZion.text = Mathf.RoundToInt(ZionResult).ToString();

        if (Mathf.RoundToInt(ArcadiaResult) == Mathf.RoundToInt(ZionResult) && Mathf.RoundToInt(ArcadiaResult) == 50f)
        {
            string ResText = "You make an absolute balance in the world.";
            FinishingTexts(ResText);
        }
        else if (Mathf.RoundToInt(ArcadiaResult) == Mathf.RoundToInt(ZionResult) && Mathf.RoundToInt(ArcadiaResult) > 50f)
        {
            string ResText = "You bring balance to the world. Although it is not an absolute balance.";
            FinishingTexts(ResText);
        }
        else if (Mathf.RoundToInt(ArcadiaResult) == Mathf.RoundToInt(ZionResult) && Mathf.RoundToInt(ArcadiaResult) < 50f)
        {
            string ResText = "You bring balance to the world. Although it is not an absolute balance.";
            FinishingTexts(ResText);
        }
    }

    public void FinishingTexts(string Result)
    {
        ResultText2.text = Result;
    }

    IEnumerator FinishingTempText(string myText)
    {
        ResultText.text = myText;
        yield return new WaitForSeconds(1f);
    }

    IEnumerator TransmissionText(string ResText)
    {
        ResultText.text = ResText;
        yield return new WaitForSeconds(1);
    }

    public void TransmitMoneyFromBBL()
    {
        string transText;
        transText = "Money Transmission from Zion to Arcadia";
        StartCoroutine(TransmissionText(transText));        

        if (GLWealthValue > 0 && GLWealthValue < 45)
        {
            MoneyValGL = MoneyValGL + 5;
            finText = "Zion make bitcoin help to Arcadia but some of coins went to somebody's pocket. Shame!";
        }
        else if (GLWealthValue >= 45 && GLWealthValue <= 55)
        {
            MoneyValGL = MoneyValGL + 10;
            finText = "Zion make bitcoin help to Arcadia and people of Arcadia are thankful to Zion.";
        }
        else if (GLWealthValue > 55 && GLWealthValue < 100)
        {
            MoneyValGL = MoneyValGL + 15;
            finText = "Zion make bitcoin help to Arcadia and Arcadia made even profit from it. Zion is a bit jealous.";
        }

        MoneyValBBL = MoneyValBBL - 10;
        //GL Values
        GLReCalculate();
        //BBL Values
        BBLReCalculate();
        //WR Values
        PercantageCalculations();
        //
        StartCoroutine(FinishingTempText(finText));
    }

    public void TransmitMoneyFromGL()
    {
        string transText;
        transText = "Money Transmission from Arcadia to Zion";
        StartCoroutine(TransmissionText(transText));

        if (BBLWealthValue > 0 && BBLWealthValue < 45)
        {
            MoneyValBBL = MoneyValBBL + 5;
            finText = "Arcadia make bitcoin help to Zion but some of coins went to somebody's pocket. Shame!";
        }
        else if (BBLWealthValue >= 45 && BBLWealthValue <= 55)
        {
            MoneyValBBL = MoneyValBBL + 10;
            finText = "Arcadia make bitcoin help to Zion and people of Zion are thankful to Arcadia.";
        }
        else if (BBLWealthValue > 55 && BBLWealthValue < 100)
        {
            MoneyValBBL = MoneyValBBL + 15;
            finText = "Arcadia make bitcoin help to Zion and Zion made even profit from it. Arcadia is a bit jealous.";
        }
        MoneyValGL = MoneyValGL - 10;
        //GL Values
        GLReCalculate();
        //BBL Values
        BBLReCalculate();
        //WR Values
        PercantageCalculations();
        //
        StartCoroutine(FinishingTempText(finText));
    }

    public void TransmitBrainFromBBL()
    {
        string transText;
        transText = "Brain Power Transmission from Zion to Arcadia";
        StartCoroutine(TransmissionText(transText));

        if (GLHealthValue > 0 && GLHealthValue < 45)
        {
            BrainValGL = BrainValGL + 5;
            finText = "Zion sent some Brain Power to Arcadia. But they died cause of sickness. Sick country!";
        }
        else if (GLHealthValue >= 45 && GLHealthValue <= 55)
        {
            BrainValGL = BrainValGL + 10;
            finText = "Zion sent some Brain Power to Arcadia.";
        }
        else if (GLHealthValue > 55 && GLHealthValue < 100)
        {
            BrainValGL = BrainValGL + 15;
            finText = "Zion sent some Brain Power to Arcadia. Arcadia feels smarter!";
        }
        BrainValBBL = BrainValBBL - 10;
        //GL Values
        GLReCalculate();
        //BBL Values
        BBLReCalculate();
        //WR Values
        PercantageCalculations();
        //
        StartCoroutine(FinishingTempText(finText));
    }

    public void TransmitBrainFromGL()
    {
        string transText;
        transText = "Brain Power Transmission from Arcadia to Zion";
        StartCoroutine(TransmissionText(transText));

        if (BBLHealthValue > 0 && BBLHealthValue < 45)
        {
            BrainValBBL = BrainValBBL + 5;
            finText = "Arcadia sent some Brain Power to Zion. But they died cause of sickness. Sick country!";
        }
        else if (BBLHealthValue >= 45 && BBLHealthValue <= 55)
        {
            BrainValBBL = BrainValBBL + 10;
            finText = "Arcadia sent some Brain Power to Zion.";
        }
        else if (BBLHealthValue > 55 && BBLHealthValue < 100)
        {
            BrainValBBL = BrainValBBL + 15;
            finText = "Arcadia sent some Brain Power to Zion. Zion feels smarter!";
        }
        BrainValGL = BrainValGL - 10;
        //GL Values
        GLReCalculate();
        //BBL Values
        BBLReCalculate();
        //WR Values
        PercantageCalculations();
        //
        StartCoroutine(FinishingTempText(finText));
    }

    public void TransmitFoodFromBBL()
    {
        string transText;
        transText = "Food Transmission from Zion to Arcadia";
        StartCoroutine(TransmissionText(transText));

        if (GLIndustryValue > 0 && GLIndustryValue < 45)
        {
            FoodValGL = FoodValGL + 5;
            finText = "Zion sent some Food to Arcadia. But they got rotten. Pfff!";
        }
        else if (GLIndustryValue >= 45 && GLIndustryValue <= 55)
        {
            FoodValGL = FoodValGL + 10;
            finText = "Zion sent some Food to Arcadia.";
        }
        else if (GLIndustryValue > 55 && GLIndustryValue < 100)
        {
            FoodValGL = FoodValGL + 15;
            finText = "Zion sent some Food to Arcadia. And Arcadia produced even more food. Hard workers!";
        }
        FoodValBBL = FoodValBBL - 10;
        //GL Values
        GLReCalculate();
        //BBL Values
        BBLReCalculate();
        //WR Values
        PercantageCalculations();
        //
        StartCoroutine(FinishingTempText(finText));
    }

    public void TransmitFoodFromGL()
    {
        string transText;
        transText = "Food Transmission from Arcadia to Zion";
        StartCoroutine(TransmissionText(transText));

        if (BBLIndustryValue > 0 && BBLIndustryValue < 45)
        {
            FoodValBBL = FoodValBBL + 5;
            finText = "Arcadia sent some Food to Zion. But they got rotten. Pfff!";
        }
        else if (BBLIndustryValue >= 45 && BBLIndustryValue <= 55)
        {
            FoodValBBL = FoodValBBL + 10;
            finText = "Arcadia sent some Food to Zion.";
        }
        else if (BBLIndustryValue > 55 && BBLIndustryValue < 100)
        {
            FoodValBBL = FoodValBBL + 15;
            finText = "Arcadia sent some Food to Zion. And Zion produced even more food. Hard workers!";
        }
        FoodValGL = FoodValGL - 10;
        //GL Values
        GLReCalculate();
        //BBL Values
        BBLReCalculate();
        //WR Values
        PercantageCalculations();
        //
        StartCoroutine(FinishingTempText(finText));
    }

    public void TransmitFuelFromBBL()
    {
        string transText;
        transText = "Fuel Transmission from Zion to Arcadia";
        StartCoroutine(TransmissionText(transText));

        if (GLDevValue > 0 && GLDevValue < 45)
        {
            FuelValGL = FuelValGL + 5;
            finText = "Zion sent some Fuel to Arcadia. They went to black market. Dark!";
        }
        else if (GLDevValue >= 45 && GLDevValue <= 55)
        {
            FuelValGL = FuelValGL + 10;
            finText = "Zion sent some Fuel to Arcadia.";
        }
        else if (GLDevValue > 55 && GLDevValue < 100)
        {
            FuelValGL = FuelValGL + 15;
            finText = "Zion sent some Fuel to Arcadia. They made a rocket. Wow!";
        }
        FuelValBBL = FuelValBBL - 10;
        //GL Values
        GLReCalculate();
        //BBL Values
        BBLReCalculate();
        //WR Values
        PercantageCalculations();
        //
        StartCoroutine(FinishingTempText(finText));
    }

    public void TransmitFuelFromGL()
    {
        string transText;
        transText = "Fuel Transmission from Arcadia to Zion";
        StartCoroutine(TransmissionText(transText));

        if (BBLDevValue > 0 && BBLDevValue < 45)
        {
            FuelValBBL = FuelValBBL + 5;
            finText = "Arcadia sent some Fuel to Zion. They went to black market. Dark!";
        }
        else if (BBLDevValue >= 45 && BBLDevValue <= 55)
        {
            FuelValBBL = FuelValBBL + 10;
            finText = "Arcadia sent some Fuel to Zion.";
        }
        else if (BBLDevValue > 55 && BBLDevValue < 100)
        {
            FuelValBBL = FuelValBBL + 15;
            finText = "Arcadia sent some Fuel to Zion. They made a rocket. Wow!";
        }
        FuelValGL = FuelValGL - 10;
        //GL Values
        GLReCalculate();
        //BBL Values
        BBLReCalculate();
        //WR Values
        PercantageCalculations();
        //
        StartCoroutine(FinishingTempText(finText));
    }

}
