﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System;

public class UIControl : MonoBehaviour {

    [Range(60.0f, 240.0f), SerializeField]
    private float timeBonusLimit = 180.0f;

    [SerializeField]
    private GUISkin customSkin;

    [SerializeField]
    private Texture2D frameTexture;

    [SerializeField]
    private Texture2D playerTexture;
    [SerializeField]
    private Texture2D enemyTexture;
    [SerializeField]
    private Texture2D healthPlayerTexture;
    [SerializeField]
    private Texture2D healthEnemyTexture;

    private DateTime startTime;
    private TimeSpan elapsedTime;

    private bool IsDisplayingInventory;


    int timeScore = 10;


	// Use this for initialization
	void Start () {
        startTime = DateTime.Now;
        elapsedTime = TimeSpan.Zero;
	}
	
	// Update is called once per frame
	void Update () {
		if (!Managers.IsGameOver)
        {
            // Calculate time elapsed, setting to a minimum of zero

            //int timeElapsed = (int) Mathf.Max(0, Time.time - startTime);
            //elapsedTime = elapsedTime.Add(new TimeSpan(timeElapsed));
            elapsedTime = DateTime.Now - startTime;
        }

        if (Input.GetKey(KeyCode.H))
        {
            IsDisplayingInventory = !IsDisplayingInventory;
        }
	}

    private void OnGUI()
    {
        GUI.skin = customSkin;

        DisplayPlayerInfo();

        if (IsDisplayingInventory)
        {
            DisplayInventory();
        }
    }

    private void DisplayPlayerInfo()
    {
        int width = 200;
        int height = 25;
        int margin = 10;
        int leftX = margin;
        int rightX = Screen.width - width - margin;
        int posY = 5;
        int labelWidth = 80;
        int healthBarWidth = 156;
        int healthBarHeight = 21;

        // Draw current time from the right edge
        GUI.Box(new Rect(rightX, posY, width, height), "");
        GUI.Label(new Rect(rightX + margin, posY, labelWidth, height), "TIME: ");
        GUI.Label(new Rect(rightX + margin + labelWidth, posY,
            (width - labelWidth), height),
            string.Format("{0:00}:{1:00}:{2:00}",
            elapsedTime.Hours,
            elapsedTime.Minutes,
            elapsedTime.Seconds));

        if (IsDisplayingInventory == false)
        {
            // Draw player's score
            posY *= 2;
            GUI.Box(new Rect(rightX, posY + height, width, height), "");
            GUI.Label(new Rect(rightX + margin, posY + height, labelWidth, height), "SCORE: ");
            GUI.Label(new Rect(rightX + margin + labelWidth, posY + height,
                (width - labelWidth), height), Managers.Player.Score.ToString());

            //Debug.Log(Time.time.ToString() + "   " + elapsedTime.ToString());

            // Draw player's health
            // reset the label width
            labelWidth = 46;
            // Draw the HP (Health Player) texture label first
            GUI.DrawTexture(new Rect(leftX, posY, labelWidth, 32), playerTexture);

            // Reset the y position
            posY = 15;

            // Create outer box
            using (new GUI.GroupScope(new Rect(leftX + labelWidth + margin, posY,
                healthBarWidth, height)))
            {
                // Draw the plain white texture
                GUI.DrawTexture(new Rect(0, 0, healthBarWidth, healthBarHeight), frameTexture);

                // Create inner box based on current health percentage (/100)
                using (new GUI.GroupScope(new Rect(0, 0,
                    Managers.Player.Health / 100 * healthBarWidth, healthBarHeight)))
                {
                    // Draw the coloured texture representing the current health
                    GUI.DrawTexture(new Rect(0, 0, healthBarWidth, healthBarHeight), healthPlayerTexture);
                }
            }
        }
    }

    private void DisplayInventory()
    {
        int width = 150;
        int height = 25;
        int margin = 10;
        int leftX = margin;
        int rightX = Screen.width - 200 - margin;
        int posY = 5;
        Texture2D image;

        // Get the current inventory list from the Inventory Manager
        List<string> itemList = Managers.Inventory.GetItemList();

        // if there are no items, display no items.
        if (itemList.Count == 0)
        {
            GUI.Box(new Rect(leftX, posY, width, height), "No Items");
        }

        // Otherwise items exist, loop through each of the inventory items collected and display them
        foreach (string item in itemList)
        {
            // get the number of items collected for the current item
            int count = Managers.Inventory.GetItemCount(item);

            // Use the icon images to create a new texture
            // The assets to load must be stored in the Resources folder.
            image = Resources.Load<Texture2D>("Icons/" + item) as Texture2D;

            // Check if icon image exists, if it does use, otherwise use plain text
            if (image != null)
            {
                GUI.Box(new Rect(leftX, posY, width, height), 
                    new GUIContent("(" + count + ")", image));
            } else
            {
                GUI.Box(new Rect(leftX, posY, width, height), 
                    item + " : " + count);
            }

            // create a bit of space for the next button
            leftX += width + margin;
        }

        // Move y down for next row of buttons
        posY += height + margin;

        // Get the inventory item the player is currently equipped with
        string equipped = Managers.Inventory.equippedItem;

        // If equipped, display the item, player is currently equipped with
        if (equipped != null)
        {
            // Calculate where to display the items - starting at the right side of the screen.
            image = Resources.Load("Icons/" + equipped) as Texture2D;
            if (image != null)
            {
                GUI.Box(new Rect(rightX, posY, 200, height), 
                    new GUIContent("Equipped", image));
            }
            else
            {
                GUI.Box(new Rect(rightX, posY, 200, height), 
                    "Equipped: " + equipped);
            }
        }

        // reset position for next set of display
        leftX = margin;

        foreach (string item in itemList)
        {
            // Display a button for the user to click on to equip player character with item
            if (GUI.Button(new Rect(leftX, posY, width, height), "Equip " + item))
            {
                // Button clicked, equip/unequip player character with item
                Managers.Inventory.EquipItem(item);
            }

            //// Check if the item the player has equipped him/herself with is health
            //if (item == "health")
            //{
            //    // Create a button for the player to use the health equipped with
            //    if (GUI.Button(new Rect(leftX, posY + height + margin, width, height), "Use Health"))
            //    {
            //        // When player uses the health, reduce the player's health
            //        Managers.Inventory.ConsumeItem("health");
            //        Managers.Player.ChangeHealth(25);
            //    }
            //}

            // create a bit of space for the next image button
            leftX += width + margin;
        }
    }

}
