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

public class InventorySlotRenderer : MonoBehaviour {

    public Image itemPreviewImage;
    public Text IndexText; 

    private int index;
    private int itemId = 0;

    private RectTransform rectTransform; 
    public RectTransform RectTransform
    {
        get
        {
            if(this.rectTransform == null)
            {
                this.rectTransform = this.GetComponent<RectTransform>();
            }
            return this.rectTransform; 
        }
    }
    
    public void SetIndex(int index)
    {
        this.index = index;
        IndexText.text = "" + ( index + 1 ); 
        this.RectTransform.localPosition = new Vector3(10 + index * 100, -10, 0);
        this.RectTransform.localScale = Vector3.one; 
    }

    public void SetItem(int newItemId)
    {
        Debug.Log("InventoryRenderer SetItem " + newItemId); 
        if (itemId == 0)
        {
            itemPreviewImage.sprite = null;
            itemPreviewImage.color = new Color(0, 0, 0, 0); 
        }

        if(newItemId != 0)
        {
            Item i = ItemManager.Instance.SpawnWeapon(newItemId); 
            itemPreviewImage.sprite = i.gameObject.GetComponent<SpriteRenderer>().sprite;
            itemPreviewImage.color = new Color(1, 1, 1, 1);
            GameObject.Destroy(i.gameObject);
        }
        
    }

}
