using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static CurrencyManager.Wallet;

namespace CurrencyManager
{
    public class WalletDemo : MonoBehaviour
    {
        private Wallet wallet;

        [SerializeField] private Currency currency1;
        [SerializeField] private Currency currency2;
        [SerializeField] private Currency currency3;
        [SerializeField] private Text currency1AmountText;
        [SerializeField] private Text currency2AmountText;
        [SerializeField] private Text currency3AmountText;

        [SerializeField] private InputField amount1Input;
        [SerializeField] private InputField amount3Input;

        [SerializeField] private InputField exchangeCurrency1xchangeInput;
        [SerializeField] private Text exchangeCurrency2OutputText;
        [SerializeField] private Button exchangeButton;

        private void Start()
        {
            wallet = Wallet.Instance;

            // Add currency1, currency2 and currency3 to the wallet.
            wallet.AddCurrency(currency1, 400);
            wallet.AddCurrency(currency2, 100);
            wallet.AddCurrency(currency3, 200);

            // Log the current balances.
            LogCurrentBalances();

            // Display the current balances.
            currency1AmountText.text = wallet.GetCurrencyBalance(currency1).ToString();
            currency2AmountText.text = wallet.GetCurrencyBalance(currency2).ToString();
            currency3AmountText.text = wallet.GetCurrencyBalance(currency3).ToString();
        }

        public void AddCurrency1Amount(float amount)
        {
            wallet.AddCurrency(currency1, amount);
            currency1AmountText.text = wallet.GetCurrencyBalance(currency1).ToString();
        }

        public void AddCurrency2Amount(float amount)
        {
            wallet.AddCurrency(currency2, amount);
            currency2AmountText.text = wallet.GetCurrencyBalance(currency2).ToString();
        }

        public void AddCurrency3Amount(float amount)
        {
            wallet.AddCurrency(currency3, amount);
            currency3AmountText.text = wallet.GetCurrencyBalance(currency3).ToString();
        }

        public void PayWithCurrency2(float amount)
        {
            CurrencyAmount currencyAmount = new CurrencyAmount { currency = currency2, amount = amount };

            if (wallet.GetInsufficientCurrencies(new List<CurrencyAmount> { currencyAmount }).Count == 0)
            {
                wallet.SubtractCurrency(currency2, amount);
                currency2AmountText.text = wallet.GetCurrencyBalance(currency2).ToString();
                Debug.Log($"Paid {amount} {currency2.Name}");
            }
            else
            {
                Debug.Log($"Insufficient {currency2.Name}");
            }
        }

        public void PayWithCurrency1And3()
        {
            float amount1 = float.Parse(!string.IsNullOrEmpty(amount1Input.text) ? amount1Input.text : "0");
            float amount3 = float.Parse(!string.IsNullOrEmpty(amount3Input.text) ? amount3Input.text : "0");

            if (amount1 == 0 && amount3 == 0) return;

            List<CurrencyAmount> currencyAmounts = new List<CurrencyAmount>
            {
                new CurrencyAmount { currency = currency1, amount = amount1 },
                new CurrencyAmount { currency = currency3, amount = amount3 }
            };

            if (wallet.GetInsufficientCurrencies(currencyAmounts).Count == 0)
            {
                wallet.SubtractCurrencies(currencyAmounts);
                currency1AmountText.text = wallet.GetCurrencyBalance(currency1).ToString();
                currency3AmountText.text = wallet.GetCurrencyBalance(currency3).ToString();
                Debug.Log($"Paid {amount1} {currency1.Name} and {amount3} {currency3.Name}");
            }
            else
            {
                Debug.Log($"Insufficient {currency1.Name} or {currency3.Name}");
            }
        }

        public void ExchangeCurrency()
        {
            float amount1 = float.Parse(!string.IsNullOrEmpty(exchangeCurrency1xchangeInput.text) ? exchangeCurrency1xchangeInput.text : "0");
            float amount2 = amount1 * currency1.ExchangeRateToBaseCurrency / currency2.ExchangeRateToBaseCurrency;

            if (wallet.GetCurrencyBalance(currency1) < amount1)
            {
                Debug.Log($"Insufficient {currency1.Name}");
                return;
            }

            exchangeCurrency2OutputText.text = amount2.ToString();
            exchangeButton.interactable = true;
        }

        public void ConfirmExchange()
        {
            float amount1 = float.Parse(!string.IsNullOrEmpty(exchangeCurrency1xchangeInput.text) ? exchangeCurrency1xchangeInput.text : "0");
            float amount2 = float.Parse(!string.IsNullOrEmpty(exchangeCurrency2OutputText.text) ? exchangeCurrency2OutputText.text : "0");

            if (wallet.ExchangeCurrency(currency1, currency2, amount1))
            {
                currency1AmountText.text = wallet.GetCurrencyBalance(currency1).ToString();
                currency2AmountText.text = wallet.GetCurrencyBalance(currency2).ToString();
                exchangeCurrency1xchangeInput.text = "0";
                exchangeCurrency2OutputText.text = "0";
                exchangeButton.interactable = false;
                Debug.Log($"Exchanged {amount1} {currency1.Name} to {amount2} {currency2.Name}");
            }
            else
            {
                Debug.Log($"Insufficient {currency1.Name} or {currency2.Name}");
            }
        }

        private void LogCurrentBalances()
        {
            List<Currency> currencies = new List<Currency> { currency1, currency2, currency3 };
            List<CurrencyAmount> currentBalances = wallet.GetCurrencyBalances(currencies);

            foreach (CurrencyAmount currencyAmount in currentBalances)
            {
                Debug.Log($"Currency: {currencyAmount.currency.Name}, Amount: {currencyAmount.amount}");
            }

            Debug.Log("-----");
        }
    }
}
