using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SellMenu : MonoBehaviour
{
    [Header("Labels")]
    public TMP_Text usernameTxt;
    public TMP_Text coinsTxt;
    public TMP_Text gemsTxt;

    [Header("Content")]
    public Transform inventoryParent;
    public Sprite emptySlotImage;

    public Image selectedItemImg;
    public string selectedItem;

    public Button sellBtn;
    [Header("Your Request Material Setter")]
    public GameObject yourReqPopup;
    public TMP_InputField coinsInput;
    public TMP_InputField gemsInput;
    public TMP_InputField oxygenInput;
    public TMP_InputField metalInput;
    public int coinsAmount;
    public int gemsAmount;
    public int oxygenAmount;
    public int metalAmount;
    public Sprite coinsIcon;
    public Sprite gemsIcon;
    public Sprite metalsIcon;
    public Sprite oxygenIcon;
    public Sprite addIcon;
    public Transform resourceIconsParent;

    void Start()
    {
        Refresh();
    }

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


    public void Refresh(){
        usernameTxt.text = DBmanager.username;
        coinsTxt.text = DBmanager.Coins.ToString();
        gemsTxt.text = DBmanager.Gems.ToString();

        Button[] inventorySlots = inventoryParent.GetComponentsInChildren<Button>();
        if(DBmanager.Inventory == null){Debug.Log("DBmanager isn't inited yet");return;}
        if(inventorySlots.Length < DBmanager.Inventory.Count){
            Debug.LogError("NEED MORE SLOTS!!!");
            return;
        }        

        for(int i =0; i < inventorySlots.Length; i++){
            if(i < DBmanager.Inventory.Count){
                InventoryItem itemData = Inventory.GetInventoryItem(DBmanager.Inventory[i].Item);
                if(itemData==null){
                    Debug.LogError("Couldn't find data for " + DBmanager.Inventory[i].Item);
                }
                inventorySlots[i].transform.GetChild(0).GetComponent<Image>().sprite = itemData.image;
                inventorySlots[i].interactable=true;
                inventorySlots[i].onClick.RemoveAllListeners();
                inventorySlots[i].onClick.AddListener(()=>{selectInventoryItem(itemData);});
            }else{
                inventorySlots[i].transform.GetChild(0).GetComponent<Image>().sprite = emptySlotImage;
                inventorySlots[i].interactable=false;
            }
        }

        if(resourceIconsParent.childCount < 4){
            Debug.LogError("Not enough buttons for your req section!");
            return;
        }
        int btnIndex = 0;
        if(coinsAmount > 0){
            SetResourceSlot(btnIndex, coinsIcon, coinsAmount.ToString());
            btnIndex++;
        }else{
            SetResourceSlot(btnIndex, addIcon, "");
        }
        if(gemsAmount > 0){
            SetResourceSlot(btnIndex, gemsIcon, gemsAmount.ToString());
            btnIndex++;
        }else{
            SetResourceSlot(btnIndex, addIcon, "");
        }
        if(metalAmount > 0){
            SetResourceSlot(btnIndex, metalsIcon, metalAmount.ToString());
            btnIndex++;
        }else{
            SetResourceSlot(btnIndex, addIcon, "");
        }
        if(oxygenAmount > 0){
            SetResourceSlot(btnIndex, oxygenIcon, oxygenAmount.ToString());
            btnIndex++;
        }else{
            SetResourceSlot(btnIndex, addIcon, "");
        }


        for(int i = btnIndex; i < 4; i++){
            SetResourceSlot(i, addIcon, "");
        }

        sellBtn.interactable  = (btnIndex > 0 && selectedItemImg.sprite != addIcon);
    }

    void SetResourceSlot(int btnIndex,Sprite icon, string text){
        
        resourceIconsParent.GetChild(btnIndex).GetChild(0).GetComponent<Image>().sprite = icon;
        resourceIconsParent.GetChild(btnIndex).GetComponentInChildren<TMP_Text>(true).text = text;
     
        resourceIconsParent.GetChild(btnIndex).GetComponentInChildren<TMP_Text>(true).gameObject.SetActive((text.Length > 0));
        
    }

    public void ShowMaterialAmountSetter(){

     //   Clear();

        metalInput.text = metalAmount.ToString();
        oxygenInput.text = oxygenAmount.ToString();
        coinsInput.text = coinsAmount.ToString();
        gemsInput.text = gemsAmount.ToString();

        yourReqPopup.gameObject.SetActive(true);
    }

    public void OnSetClicked(){
        try{
            metalAmount = int.Parse(metalInput.text);
            oxygenAmount= int.Parse(oxygenInput.text);
            coinsAmount = int.Parse(coinsInput.text);
            gemsAmount = int.Parse(gemsInput.text);
        }catch{

        }

        yourReqPopup.gameObject.SetActive(false);
        Refresh();
    }

    public void OpenSellMenu(){
        Clear();
        gameObject.SetActive(true);
    }

    public void CloseSellMenu(){
        gameObject.SetActive(false);
        Clear();
    }


    public void Clear(){
        metalAmount = coinsAmount = oxygenAmount = gemsAmount = 0;
        selectedItemImg.sprite = addIcon;
        selectedItem = "";
       // selected
        Refresh();
    }
    public void selectInventoryItem(InventoryItem item){
        selectedItemImg.sprite = item.image;
        selectedItem = item.itemName;
        Refresh();
    }


    public async void Sell(){
        await DBmanager.SellItem(selectedItem, coinsAmount, gemsAmount, metalAmount, oxygenAmount);
        Clear();
    }
}
