using UnityEngine;
using System.Collections;

public class GUIManager : MonoBehaviour 
{
    float fWarningTimer = 0.0f;

    public PlayerMovement playerScript;

    public Texture blackTile;
    public GUIStyle myStyle;

    public Texture heartFull;
    public Texture heartHalf;
    public Texture heartEmpty;

    public Texture frustrationBox;
    public Texture frustrationFill;

    public Texture how2Play;


    public float fNewFloorTimer;
    public int iFloorNumber;

	// Use this for initialization
	void Start () 
    {
	
	}

    string GetProtip()
    {
        switch (iFloorNumber)
        {
            case 1:
                return "Advance to the end of the hall, dispense justice.";
            case 2:
                return "Hold up while jumping to reach higher floors";
            case 3:
                return "Beware Social Justice Paladins and their shields!";
            case 4:
                return "Soothing Social Justice Bards ease your frustration...";
            case 5:
                return "Harriers, Wizards, Sharpshooters! Fight on!";
            case 6:
                return "From on here out, true danger awaits!";
        }

        return "";
    }

    protected virtual bool GetPlayerScript()
    {
        GameObject go = GameObject.Find("PlayerMans");
        if (go != null)
        {
            playerScript = go.GetComponent<PlayerMovement>();
        }

        return playerScript != null;
    }

    void OnGUI()
    {
        myStyle.fontSize = 18;

        fWarningTimer += Time.smoothDeltaTime;
        if (fWarningTimer > 1.0f)
            fWarningTimer -= 1.0f;

        GUI.DrawTexture( new Rect(0,0,2000,64),blackTile);

        if( playerScript == null && !GetPlayerScript())
            return;

        if (fNewFloorTimer > 0.0f)
        {
            fNewFloorTimer -= Time.smoothDeltaTime;
            myStyle.fontSize = 32;
            DrawWithOutline(new Rect(8, 70, 600, 32), "Hall #" + iFloorNumber, myStyle, Color.black);
            myStyle.fontSize = 18;
            string strTip = GetProtip();
            if( strTip != null )
                DrawWithOutline(new Rect(8, 120, 600, 32), strTip, myStyle, Color.black);

        }

        GUI.Label(new Rect(4, 4, 100, 32), "Health", myStyle);
        int heartSize = 20;
        float fHealthRequirement = 100.0f;
        for (int y = 24; y < 24 + heartSize * 2; y += heartSize)
            for (int x = 4; x < 4 + heartSize * 5; x += heartSize)
            {
                if( playerScript.fHealth >= fHealthRequirement)
                    GUI.DrawTexture(new Rect(x,y,heartSize,heartSize), heartFull);
                else if (playerScript.fHealth > fHealthRequirement - 100.0f)
                    GUI.DrawTexture(new Rect(x, y, heartSize, heartSize), heartHalf);
                else
                    GUI.DrawTexture(new Rect(x, y, heartSize, heartSize), heartEmpty);

                fHealthRequirement += 100.0f;
            }

        GUI.Label(new Rect(200, 4, 100, 32), "Ethics:", myStyle);

        bool bChangeWasPositive = false;
        bool bShowWarning = false;
        float fChangeTimer = 0.0f;

        string strEthicsString = playerScript.GetEthicsStringFromScore( ref fChangeTimer, ref bChangeWasPositive, ref bShowWarning);

        Color drawColor = Color.white;
        if( bShowWarning )
        {
            if( fWarningTimer >= 0.5f )
                drawColor = Color.Lerp(Color.red, Color.yellow, (fChangeTimer - 0.5f) / 0.5f);  
            else
                drawColor = Color.Lerp(Color.yellow, Color.red, fChangeTimer / 0.5f);  

        }

        else if( fChangeTimer > 0.0f )
        {
            if( bChangeWasPositive )
                drawColor = Color.Lerp(Color.white, Color.cyan, fChangeTimer);  
            else
                drawColor = Color.Lerp(Color.white, Color.red, fChangeTimer);  
        }

        myStyle.normal.textColor = drawColor;
        GUI.Label(new Rect(200, 32, 100, 32), strEthicsString , myStyle);

        myStyle.normal.textColor = Color.white;

        GUI.Label(new Rect(480, 4, 100, 32), "Frustration", myStyle);

        GUI.DrawTexture(new Rect(480, 30, 200, 32), frustrationBox);

        float fFillDistance = (playerScript.fFrustration / 100.0f) * 180.0f;
        GUI.DrawTexture(new Rect(492, 34, fFillDistance, 24), frustrationFill);

        GUI.Label(new Rect(720, 4, 100, 32), "Score", myStyle);
        GUI.Label(new Rect(720, 32, 100, 32), playerScript.iScore.ToString(), myStyle);


    }

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

    public void UpdateTitleScreenGUI()
    {
        myStyle.fontSize = 48;
        GUI.Label(new Rect(16, 70, 300, 55), "WRECKBEARD", myStyle);
        myStyle.fontSize = 24;
        GUI.Label(new Rect(16, 120, 300, 55), "Men's Rights Arcanist", myStyle);
        GUI.Label(new Rect(16, 160, 300, 55), "Blood on the Con Floor:", myStyle);
        GUI.Label(new Rect(16, 200, 300, 55), "The Un-Convention Hall Weapon", myStyle);

        DrawWithOutline( new Rect(16, 300, 300, 55), "Gamepad A or Space to Alpha Up!", myStyle, Color.Lerp( Color.blue, Color.cyan, fWarningTimer));

        myStyle.fontSize = 18;
        GUI.Label(new Rect(16, 490, 300, 55), "Jim Shepard", myStyle);
        GUI.Label(new Rect(16, 510, 300, 55), "Twitter @madjackmcmad", myStyle);
        GUI.Label(new Rect(16, 530, 300, 55), "Ludum Dare #32 | 48 Hour Game Jam | 17-19 April 2015", myStyle);
        GUI.Label(new Rect(16, 550, 300, 55), "Theme: 'An Unconventional Weapon'", myStyle);


    }

    public void UpdateHowToPlayGUI()
    {
        GUI.DrawTexture(new Rect(64, 70, 800,600), how2Play );
    }

    public void UpdateGameOverGUI()
    {
        myStyle.fontSize = 48;
        DrawWithOutline(new Rect(16, 100, 300, 55), "GAME OVER", myStyle, Color.black);

        myStyle.fontSize = 24;
        GameManager gm = GameObject.Find("GameManager").GetComponent<GameManager>();
        DrawWithOutline(new Rect(16, 160, 300, 32), "Final Score " + gm.FinalScore, myStyle, Color.black);

        if (gm.bGameOverFromPlayerDeath)
        {
            DrawWithOutline(new Rect(16, 300, 600, 32), "Killed in the name of Men's Rights.", myStyle, Color.black);
            DrawWithOutline(new Rect(16, 340, 600, 32), "The legend will never die.", myStyle, Color.black);
            DrawWithOutline(new Rect(16, 380, 600, 32), "Collect Red Pills to regain health.", myStyle, Color.black);
        }
        else
        {
            DrawWithOutline(new Rect(16, 300, 600, 32), "Your Ethics have become INDEFENSIBLE.", myStyle, Color.black);
            DrawWithOutline(new Rect(16, 340, 600, 32), "A true Nice Guy would never hurt a lady!", myStyle, Color.black);
            DrawWithOutline(new Rect(16, 380, 600, 32), "Do not damage M'Ladies,", myStyle, Color.black);
            DrawWithOutline(new Rect(16, 420, 600, 32), "just touch them without asking.", myStyle, Color.black);
            DrawWithOutline(new Rect(16, 460, 600, 32), "Collect Corrupt Journalism", myStyle, Color.black);
            DrawWithOutline(new Rect(16, 500, 600, 32), "to restore your Ethics.", myStyle, Color.black);


        }
        myStyle.normal.textColor = Color.white;
    }

    public void DrawWithOutline (Rect position, string text, GUIStyle style, Color outColor)
    {
        GUIStyle backupStyle = style;
        var oldColor = style.normal.textColor;
        style.normal.textColor = outColor;
        position.x--;
        GUI.Label(position, text, style);
        position.x += 2;
        GUI.Label(position, text, style);
        position.x--;
        position.y--;
        GUI.Label(position, text, style);
        position.y += 2;
        GUI.Label(position, text, style);
        position.y--;
        style.normal.textColor = oldColor;
        GUI.Label(position, text, style);
        style = backupStyle;
    }

}
