using System;
using System.Collections.Generic;

namespace ScrewsMaster
{
    public class ConsumeSystem : BaseSystem
    {
        public ConsumeSystem(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)
        {
            ConsumeVal consumeVal = (ConsumeVal)obj;

            int 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 <= GameHelper.Get102())
                        {
                            consumeSuccessCount++;
                        }
                        else
                        {
                            GameHelper.ShowTips("No enough", true);
                        }

                        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 <= GameHelper.Get102())
                                {
                                    PreferencesMgr.Instance.Currency102 -= 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)
        {
            ConsumeVal 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)
        {
            ConsumeVal 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)
        {
            ConsumeVal consumeVal = ConsumeVal.Get();
            consumeVal.onfinish = onCompleted;
            for (int 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)
        {
            ConsumeVal consumeVal = ConsumeVal.Get();
            consumeVal.onfinish = onCompleted;
            for (int 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 ObjectPool<ConsumeVal> _pool = new ObjectPool<ConsumeVal>();

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

        public static ConsumeVal Get(int id, decimal 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);
        }
    }
}