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

public class Shopping : MonoBehaviour
{
    private bool playerInRange;

    private bool isBuy = false;
    public Item sellItem = null;
    //public GameObject[] itemForSale = new GameObject[2];
    public List<GameObject> itemForSale = new List<GameObject>();

    public GameObject panelEbutton;
    public GameObject Ebutton;
    public GameObject textEbutton;

    public GameObject textEbuttonObject;



    // Start is called before the first frame update
    void Start()
    {
        panelEbutton.GetComponent<CanvasRenderer>().SetAlpha(0f);
        Ebutton.GetComponent<CanvasRenderer>().SetAlpha(0f);
        textEbutton.GetComponent<CanvasRenderer>().SetAlpha(0f);
        textEbuttonObject.GetComponent<CanvasRenderer>().SetAlpha(0f);

    }

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

        if (playerInRange)
        {
            switch (sellItem.id)
            {
                case 1:
                    textEbuttonObject.GetComponent<Text>().text = "Chaussures de marche";
                    break;
                case 2:
                    textEbuttonObject.GetComponent<Text>().text = "Bâton de marche";
                    break;
                case 3:
                    textEbuttonObject.GetComponent<Text>().text = "Réchaud";
                    break;
                case 4:
                    textEbuttonObject.GetComponent<Text>().text = "Tente";
                    break;
            }
        }

        if ((Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown("joystick button 1")) && playerInRange && sellItem && itemForSale.Count > 0)
        {
            PlayerNature.instance.LooseNature(25);
            switch (sellItem.id)
            {
                case 1:
                    Dialog.instance.setSituation("boutiqueChaussure");
                    isBuy = true;
                    break;
                case 2:
                    Dialog.instance.setSituation("boutiqueBaton");
                    isBuy = true;
                    break;
                case 3:
                    Dialog.instance.setSituation("boutiqueRechaud");
                    isBuy = true;
                    break;
                case 4:
                    Dialog.instance.setSituation("boutiqueTente");
                    isBuy = true;
                    break;
            }
            panelEbutton.GetComponent<CanvasRenderer>().SetAlpha(0f);
            Ebutton.GetComponent<CanvasRenderer>().SetAlpha(0f);
            textEbutton.GetComponent<CanvasRenderer>().SetAlpha(0f);
            textEbuttonObject.GetComponent<CanvasRenderer>().SetAlpha(0f);
            Inventory.instance.addItem(sellItem);
            Debug.Log(sellItem.name.ToString() + " a bien ete ajouté a l'inventaire");
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Player"))
        {
            playerInRange = true;
            if (isBuy == false)
            {
                panelEbutton.GetComponent<CanvasRenderer>().SetAlpha(1f);
                Ebutton.GetComponent<CanvasRenderer>().SetAlpha(1f);
                textEbutton.GetComponent<CanvasRenderer>().SetAlpha(1f);
                textEbuttonObject.GetComponent<CanvasRenderer>().SetAlpha(1f);
            }

        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        playerInRange = false;
        panelEbutton.GetComponent<CanvasRenderer>().SetAlpha(0f);
        Ebutton.GetComponent<CanvasRenderer>().SetAlpha(0f);
        textEbutton.GetComponent<CanvasRenderer>().SetAlpha(0f);
        textEbuttonObject.GetComponent<CanvasRenderer>().SetAlpha(0f);
    }
}