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

public class Store : MonoBehaviour
{
    /// <summary>
    /// what items are in stock
    /// </summary>
    public List<InventoryItem> items = new List<InventoryItem>();
    /// <summary>
    /// An Inventory instance
    /// </summary>
    public Inventory inventory;

    private void OnGUI()
    {
        foreach (var item in items)
        {
            if (item == null) continue;
            GUI.color = Color.white;
            GUILayout.BeginHorizontal();
            GUILayout.Label(item.name);
            GUILayout.BeginVertical();
            //draw the icon if its available
            if (item.icon != null)
            {
                Rect iconRect = GUILayoutUtility.GetRect(item.icon.width, item.icon.height);
                iconRect.width = item.icon.width;
                GUI.DrawTexture(iconRect, item.icon);
            }

            //Special behaviour if its a key item!
            if(item is InventoryKeyItem)
            {
                GUI.color = Color.yellow;
                GUILayout.Label(((InventoryKeyItem)item).enscription);
            }

            if (inventory != null)
            {
                //Don't let people buy Key Items if they already have one
                if(item is InventoryKeyItem && inventory.Count(item) > 0)
                {
                    GUILayout.Label("[Owned]");
                }
                else
                {
                    //buy the item!
                    if (GUILayout.Button($"Buy [{item.cost}]"))
                    {
                        inventory.AddItem(item);
                        //TODO:  Oh no where did I leave my wallet...
                    }
                }
            }

            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
        }
    }
}
