using System;
using BingoBrain.Core;
using System.Collections.Generic;

namespace BingoBrain
{
    public class TuSystem : BaseSystem
    {
        public TuSystem(bool isAutoInit = true)
        {
            if (isAutoInit)
            {
                Init();
            }
        }

        public sealed override void Init()
        {
            base.Init();

            AddListener();
        }

        private void AddListener()
        {
            CtrlDispatcher.Instance.AddListener(CtrlMsg.ConsumeResources, OnConsumeResources);
        }

        private void OnConsumeResources(object obj)
        {
            var consumeVal = (ConsumeVal)obj;

            var consumeSuccessCount = 0;
            for (var index = 0; index < consumeVal.ItemId.Count; index++)
            {
                var item = consumeVal.ItemId[index];
                var itemValue = consumeVal.ItemVal[index];
                switch (item)
                {
                    case 101:
                        if (itemValue <= PreferencesMgr.Instance.Currency101)
                        {
                            consumeSuccessCount++;
                        }
                        else
                        {
                           // GameHelper.Toast("outTicket", true);
                        }

                        break;
                    case 102:
                        if (itemValue <= PreferencesMgr.Instance.Currency102)
                        {
                            consumeSuccessCount++;
                        }

                        break;
                    case 103:
                        if (itemValue <= PreferencesMgr.Instance.Active)
                        {
                            consumeSuccessCount++;
                        }

                        break;
                }
            }

            if (consumeVal.isConsume)
            {
                if (consumeSuccessCount == consumeVal.ItemId.Count)
                {
                    for (var index = 0; index < consumeVal.ItemId.Count; index++)
                    {
                        var item = consumeVal.ItemId[index];
                        var itemValue = consumeVal.ItemVal[index];
                        switch (item)
                        {
                            case 101:
                                if (itemValue <= PreferencesMgr.Instance.Currency101)
                                {
                                    PreferencesMgr.Instance.Currency101 -= (int)itemValue;
                                }

                                break;
                            case 102:
                                if (itemValue <= PreferencesMgr.Instance.Currency102)
                                {
                                    PreferencesMgr.Instance.Currency102 -= itemValue;
                                }

                                break;
                            case 103:
                                if (itemValue <= PreferencesMgr.Instance.Active)
                                {
                                    PreferencesMgr.Instance.Active -= (int)itemValue;
                                }

                                break;
                        }
                    }

                    consumeVal.onfinish?.Invoke(true);
                }
                else
                {
                    consumeVal.onfinish?.Invoke(false);
                }
            }
            else
            {
                consumeVal.onfinish?.Invoke(consumeSuccessCount == consumeVal.ItemId.Count);
            }

            consumeVal.Release();
        }

        #region 消耗资源

        public static void Consume(int itemId, int itemNum, Action<bool> onCompleted = null, bool isConsume = true)
        {
            var consumeVal = ConsumeVal.Get(itemId, itemNum, onCompleted);
            consumeVal.isConsume = isConsume;
            CtrlDispatcher.Instance.Dispatch(CtrlMsg.ConsumeResources, consumeVal);
        }

        public static void Consume(int itemId, decimal itemNum, Action<bool> onCompleted = null, bool isConsume = true)
        {
            var consumeVal = ConsumeVal.Get(itemId, itemNum, onCompleted);
            consumeVal.isConsume = isConsume;
            CtrlDispatcher.Instance.Dispatch(CtrlMsg.ConsumeResources, consumeVal);
        }


        public static void Consume(int[] itemIds, int[] itemNums, Action<bool> onCompleted = null,
            bool isConsume = true)
        {
            var consumeVal = ConsumeVal.Get();
            consumeVal.onfinish = onCompleted;
            for (var i = 0; i < itemIds.Length; i++)
            {
                consumeVal.AddConsume(itemIds[i], itemNums[i]);
            }

            consumeVal.isConsume = isConsume;
            CtrlDispatcher.Instance.Dispatch(CtrlMsg.ConsumeResources, consumeVal);
        }

        public static void Consume(int[] itemIds, decimal[] itemNums, Action<bool> onCompleted = null,
            bool isConsume = true)
        {
            var consumeVal = ConsumeVal.Get();
            consumeVal.onfinish = onCompleted;
            for (var i = 0; i < itemIds.Length; i++)
            {
                consumeVal.AddConsume(itemIds[i], itemNums[i]);
            }

            consumeVal.isConsume = isConsume;
            CtrlDispatcher.Instance.Dispatch(CtrlMsg.ConsumeResources, consumeVal);
        }

        #endregion

        public static void CheckItem(int itemId, int itemNum, Action<bool> onCompleted = null)
        {
            Consume(itemId, itemNum, onCompleted, false);
        }

        public static void CheckItem(int itemId, decimal itemNum, Action<bool> onCompleted = null)
        {
            Consume(itemId, itemNum, onCompleted, false);
        }

        public static void CheckItem(int[] itemId, int[] itemNum, Action<bool> onCompleted = null)
        {
            Consume(itemId, itemNum, onCompleted, false);
        }

        public static void CheckItem(int[] itemId, decimal[] itemNum, Action<bool> onCompleted = null)
        {
            Consume(itemId, itemNum, onCompleted, false);
        }
    }


    public class ConsumeVal
    {
        private static Jcna<ConsumeVal> _pool = new Jcna<ConsumeVal>();

        public static ConsumeVal Get()
        {
            ConsumeVal val = _pool.Get();
            return val;
        }

        public static ConsumeVal Get(int id, float sum)
        {
            return Get(id, sum);
        }

        public static ConsumeVal Get(int id, decimal sum, Action<bool> finish = null)
        {
            ConsumeVal val = _pool.Get();
            val.AddConsume(id, sum);
            val.onfinish = finish;
            return val;
        }


        public void Release()
        {
            ItemId.Clear();
            ItemVal.Clear();
            isConsume = true;
            onfinish = null;
            _pool.Release(this);
        }

        public ConsumeVal()
        {
        }

        public Action<bool> onfinish;

        public List<int> ItemId = new List<int>();
        public List<decimal> ItemVal = new List<decimal>();

        public bool isConsume = true;

        public void AddConsume(int id, decimal sum)
        {
            ItemId.Add(id);
            ItemVal.Add(sum);
        }
    }
}