using BingoBrain;

namespace BingoBrain.Core
{
    using System;
    using UnityEngine;
    using System.Collections.Generic;

    internal class IsfvBehaviour : SingletonUnity<IsfvBehaviour>
    {
        private readonly Dictionary<string, IsfvData> _asyncDataDict = new Dictionary<string, IsfvData>();

        private readonly Dictionary<string, IsfvData> _asyncTmpDataDict = new Dictionary<string, IsfvData>();

        private readonly Dictionary<string, IsfvData> _updateActionDict = new Dictionary<string, IsfvData>();

        private readonly List<string> _keyClean = new List<string>();

        private void Update()
        {
            _asyncTmpDataDict.ForEachSafe((s, data) => data.PassTime(Time.deltaTime));
            _asyncDataDict.ForEachSafe((s, data) => data.PassTime(Time.deltaTime));

            try
            {
                _updateActionDict.ForEachSafe((k, v) => { v?.dealMethod?.Invoke(); });
            }
            catch (Exception)
            {
            }

            ClearTempFinished();


            ClearFinished();


            ClearUpdateActionFinished();
        }


        private void ClearFinished()
        {
            _keyClean.Clear();
            foreach (var dealData in _asyncDataDict)
            {
                if (dealData.Value.IsComplete)
                {
                    _keyClean.Add(dealData.Key);
                }
            }

            foreach (var key in _keyClean)
            {
                _asyncDataDict.Remove(key);
            }
        }

        private void ClearTempFinished()
        {
            _keyClean.Clear();
            foreach (var dealData in _asyncTmpDataDict)
            {
                if (dealData.Value.IsComplete)
                {
                    _keyClean.Add(dealData.Key);
                }
            }

            foreach (var key in _keyClean)
            {
                _asyncTmpDataDict.Remove(key);
            }
        }


        public void AddAsyncData(string key, IsfvData isfvData)
        {
            RemoveAsyncData(key);
            _asyncDataDict.Add(key, isfvData);
        }

        public void AddAsyncTempData(string key, IsfvData isfvData)
        {
            RemoveAsyncTempData(key);
            _asyncTmpDataDict.Add(key, isfvData);
        }

        public void RemoveAsyncTempData(string key)
        {
            if (_asyncTmpDataDict.ContainsKey(key))
            {
                _asyncTmpDataDict.Remove(key);
            }
        }


        public void RemoveAsyncData(string key)
        {
            if (_asyncDataDict.ContainsKey(key))
            {
                _asyncDataDict.Remove(key);
            }
        }


        public void AddUpdateAction(string key, IsfvData isfvData)
        {
            RemoveAsyncData(key);
            _updateActionDict.Add(key, isfvData);
        }


        public void RemoveUpdateAction(string key)
        {
            if (_updateActionDict.ContainsKey(key))
            {
                _updateActionDict.Remove(key);
            }
        }


        public void ClearUpdateActionFinished()
        {
            _keyClean.Clear();
            foreach (var dealData in _updateActionDict)
            {
                if (dealData.Value.IsComplete)
                {
                    _keyClean.Add(dealData.Key);
                }
            }

            foreach (var key in _keyClean)
            {
                _updateActionDict.Remove(key);
            }
        }

        public void RemoveAllAction()
        {
            _updateActionDict.Clear();
            _asyncDataDict.Clear();
            _asyncTmpDataDict.Clear();
        }
    }
}