using System.Xml.Serialization;
using TMPro;
using Unity.VisualScripting;
using Unity.VisualScripting.Antlr3.Runtime;
using UnityEngine;
using UnityEngine.Rendering.VirtualTexturing;
using UnityEngine.U2D;

public class ShopScript : MonoBehaviour
{
    [Header("Panels")]

    [SerializeField] GameObject shopPanel;
    [SerializeField] GameObject errorPanel;
    [SerializeField] GameObject backgroundPanel;

    [SerializeField] public bool paymentSystemActive = false;
    
    //TMP_Text textfeld;
    [SerializeField] TMP_Text textfeld;

    [Header("Scripte")]

    InventoryScript inventory;
    ChangeHealth healthScript;
    HandleEnergy battery;

    [Header("BuyValues")]

    private float moneten = 10.00f;      //als vorerstiger Ersatz für tatsächliches Geld

    private float gemsCost = 1.00f;         //1€
    private float manyGemsCost = 2.00f;     //2€
    private int updateCost = 100;

    private float duration = 4f;

    #region DevMode
    [Header("DevMode")]

    [SerializeField] GameObject devPanel;

    //TMP_Text textfeldA;                         //als textfeld
    //TMP_Text textfeldB;
    //TMP_Text textfeldC;

    [SerializeField] TMP_Text textfeldA;        //als text
    [SerializeField] TMP_Text textfeldB;
    [SerializeField] TMP_Text textfeldC;
    [SerializeField] TMP_Text errorText;

    [Header("ShopTextfieldCost")]

    [SerializeField] TMP_Text healthCost;
    [SerializeField] TMP_Text energyCost;
    [SerializeField] TMP_Text gemCost;
    [SerializeField] TMP_Text moreGemsCost;

    #endregion
    private void Awake()
    {
        inventory = backgroundPanel.GetComponent<InventoryScript>();
        healthScript = backgroundPanel.GetComponent<ChangeHealth>();
        battery = backgroundPanel.GetComponent<HandleEnergy>();
        
    }
    void Start()
    {

        shopPanel.SetActive(false);
        errorPanel.SetActive(false);
        devPanel.SetActive(false);

        healthCost.text = updateCost.ToString();
        energyCost.text = updateCost.ToString();
        gemCost.text = gemsCost.ToString() + " €";
        moreGemsCost.text = manyGemsCost.ToString() + " €";

        //textfeld = this.text;
    }

    void Update()
    {

        textfeld.text = inventory.Gems.ToString();
        
        textfeldA.text = "Energy:   " + inventory.Energy.ToString();
        textfeldB.text = "Health:   " + inventory.Health.ToString();
        textfeldC.text = "Moneten:  " + moneten.ToString();

    }

    public void OpenShop()
    {
        shopPanel.SetActive(true);
        LeanTween.scale(shopPanel, transform.localScale * 0.5f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);

    }

    public void CloseShop()
    {
        LeanTween.scale(shopPanel, transform.localScale * 0f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);
        shopPanel.SetActive(false);
    }

    public void ClosePanel()
    {
        LeanTween.scale(errorPanel, transform.localScale * 0f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);
        errorPanel.SetActive(false);
    }

    public void AddLife()
    {
        
        if (inventory.Gems - updateCost < 0)
        {
            errorPanel.SetActive(true);
            errorText.text = "Not enough gems!";
            LeanTween.scale(errorPanel, transform.localScale * 0.5f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);
        }

        else
        {
            healthScript.standardHealth++;

            inventory.Gems -= updateCost;
        }
    }

    public void AddEnergy()
    {
        if (inventory.Gems - updateCost <0)
        {
            errorPanel.SetActive(true);
            errorText.text = "Not enough gems!";
            LeanTween.scale(errorPanel, transform.localScale * 0.5f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);
        }

        else
        {
            battery.standardEnergy++;

            inventory.Gems -= updateCost;
        }
    }

    public void BuyGems()
    {
        if (paymentSystemActive)
        {

            if (moneten - gemsCost < 0)
            {
                errorPanel.SetActive(true);
                errorText.text = "Your card is empty!";
                LeanTween.scale(errorPanel, transform.localScale * 0.5f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);
            }

            else
            {
                moneten -= gemsCost;
                Debug.Log("Ich mag Moeneten");
                inventory.Gems += 100;
            }
        }

        else
        {
            errorPanel.SetActive(true);
            errorText.text = "Payment System inactive!";
            LeanTween.scale(errorPanel, transform.localScale * 0.5f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);
        }
    }

    public void BuyMore()
    {

        if (paymentSystemActive)
        {
            if (moneten - manyGemsCost < 0)
            {
                errorPanel.SetActive(true);
                errorText.text = "Your card is empty!";
                LeanTween.scale(errorPanel, transform.localScale * 0.5f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);
            }

            else
            {
                moneten -= manyGemsCost;
                Debug.Log("Ich mag Moeneten");

                inventory.Gems += 200;
            }
        }
        else
        {
            errorPanel.SetActive(true);
            errorText.text = "Payment System inactive!";
            LeanTween.scale(errorPanel, transform.localScale * 0.5f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);
        }
    }

    public void ShowDevMode()
    {
        devPanel.SetActive(true);
        LeanTween.scale(devPanel, transform.localScale * 0.5f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);

        //textfeldA = textA;
        //textfeldB = textB;
        //textfeldC = textC;

        //textfeldA.text = inventory.Energy.ToString();
        //textfeldB.text = inventory.Health.ToString();
        //textfeldC.text = moneten.ToString();
    }

    public void ChangeActivation()
    {
        SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();

        if (!paymentSystemActive)
        {
            paymentSystemActive = true;
            spriteRenderer.sprite = Resources.Load<Sprite>("T_12_ok.png");

            errorPanel.SetActive(true);
            errorText.text = "PaymentSystem active";
            LeanTween.scale(errorPanel, transform.localScale * 0.5f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);

        }

        else
        {
            paymentSystemActive = false;

            spriteRenderer.sprite = Resources.Load<Sprite>("T_11_no.png");

            errorPanel.SetActive(true);
            errorText.text = "PaymentSystem inactive!";
            LeanTween.scale(errorPanel, transform.localScale * 0.5f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);

        }
    }

    public void CloseDevPanel()
    {
        LeanTween.scale(devPanel, transform.localScale * 0f, duration * Time.deltaTime).setDelay(.1f).setEase(LeanTweenType.easeOutBounce);
        devPanel.SetActive(false);

        ClosePanel();
    }
}
