using UnityEngine;
using System.Collections;
using TMPro;

public class UI_Main : TM_UI_Base 
{

    public TextMeshProUGUI textTimer;
    public TextMeshProUGUI textScore;

    public TextMeshProUGUI textShape;
    public TextMeshProUGUI textWisdom;

    public TextMeshProUGUI textTimerAdjust;

    public GameManager theGame;

    [HideInInspector]
    public float fPostTutorialLastTipTimer;

    float fTimeDisplay;

    void Awake()
    {
        theGame = GameObject.Find("GameManager").GetComponent<GameManager>();
    }

	// Use this for initialization
	public override void Start () 
    {
	
	}
	
    public void SetDisplayTime(float fTime)
    {
        fTimeDisplay = fTime;
    }

	// Update is called once per frame
	public override void Update () 
    {
	    if (bFirstUpdate)
	    {
	        bFirstUpdate = false;
            TurnOff();
	        return;
	    }

        float xSize = Screen.width;
        float ySize = Screen.height;

        int iDangerQuotient = theGame.GetGameDangerQuotient();

        //tick down our clock.
        fTimeDisplay -= Time.deltaTime;
	    textTimerAdjust.enabled = false;

        //if we aren't close to the actual time, tick down HARDER
        float fActualGameTime = theGame.TimeRemaining();
        float fDelta = fTimeDisplay - fActualGameTime;

        bool bSetTimerShapeBasedOnDanger = true;
        //textTimer.transform.localPosition = new Vector3(1f, 1f, 1f);
        //we are behind, and need to add time
        if( fDelta < 0.0f )
        {
            fTimeDisplay += Time.deltaTime * 6.0f;
            if (fTimeDisplay >= fActualGameTime)
                fTimeDisplay = fActualGameTime;
            else
            {
                textTimer.color = Color.cyan;
                bSetTimerShapeBasedOnDanger = false;
                textTimerAdjust.enabled = true;
                textTimerAdjust.color = Color.cyan;

            }
        }
        //we are above, and need to tick down
        if (fDelta > 0.0f)
        {
            fTimeDisplay -= Time.deltaTime * 4.0f;
            if (fTimeDisplay <= fActualGameTime + 0.1f)
                fTimeDisplay = fActualGameTime;
            else
            {
                textTimer.color = Color.red;
                bSetTimerShapeBasedOnDanger = false;
                textTimerAdjust.enabled = true;
                textTimerAdjust.color = Color.red;
                //textTimer.transform.localPosition = new Vector3(Random.Range(0.95f, 1.05f), Random.Range(0.95f, 1.05f), 1f);
            }
        }

        textTimer.enabled = !theGame.bTutorialMode;
        textTimer.text = fTimeDisplay.ToString("0.00");

        if (bSetTimerShapeBasedOnDanger)
        {
            switch (iDangerQuotient)
            {
                case 1:
                    textTimer.color = Color.white;
                    break;
                case 2:
                    textTimer.color = Color.yellow;
                    break;
                case 3:
                    textTimer.color = new Color(255, 165, 0);
                    break;
                case 4:
                    textTimer.color = Color.red;
                    break;
            }

            if (fTimeDisplay <= 15.0f)
            {
                float fScale = 1.25f + (0.25f * Mathf.Sin(Time.time * 6.28f));
                textTimer.transform.localScale = new Vector3(fScale, fScale, fScale);
            }
            else
            {
                textTimer.transform.localScale = new Vector3(1f, 1f, 1f);
            }
        }


        textScore.text = "Score\n" + theGame.Score();
        //textTimer.transform.position = new Vector3( xSize * 0.5f, ySize * 0.92f,0);

        if( theGame.bTutorialMode)
        {
            //if we're being assembled...
            if (theGame.bStartGameAssembly)
            {
                textShape.text = "COLLECT SHAPES";
                textWisdom.text = "Shift from ground to air forms";
            }
            //otherwise talk about our shape
            else
            {
                switch (theGame.thePlayer.currentForm)
                {
                    case EShipForm.triangle:
                        textShape.text = "JET";
                        textWisdom.text = theGame.GetJetWisdom();
                        break;
                    case EShipForm.square:
                        textShape.text = "TANK";
                        textWisdom.text = theGame.GetTankWisdom();
                        break;
                }
            }
        }
        else if( fPostTutorialLastTipTimer > 0.0f )
        {
            textShape.text = "CHASE LOOT";
            textWisdom.text = "Fight for points\nWatch the clock\nChange forms to hit foes";
            fPostTutorialLastTipTimer -= Time.deltaTime;
        }
        else
        {
            textShape.text = "";
            textWisdom.text = "";
        }

        if( fTimeDisplay <= 0.0f )
            theGame.GameOver();
	}

    public void SetTimerAdjustText(string strText)
    {
        textTimerAdjust.text = strText;
    }
}
