using System;

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

    public static class IsfvKit
    {
        private static IsfvBehaviour _sIsfvBehaviour;


        private static readonly object SynClock = new object();

        private static IsfvBehaviour MIsfvBehaviour
        {
            get
            {
                if (_sIsfvBehaviour != null)
                {
                    return _sIsfvBehaviour;
                }

                lock (SynClock)
                {
                    if (_sIsfvBehaviour != null)
                    {
                        return _sIsfvBehaviour;
                    }


                    _sIsfvBehaviour = IsfvBehaviour.Instance;
                    if (_sIsfvBehaviour != null)
                    {
                        _sIsfvBehaviour.name = "[ AsyncKit ]";
                    }
                }

                return _sIsfvBehaviour;
            }
        }






        public static Coroutine StartCoroutine(IEnumerator enumerator)
        {
            return MIsfvBehaviour.StartCoroutine(enumerator);
        }





        public static void StopCoroutine(Coroutine methodName)
        {
            MIsfvBehaviour.StopCoroutine(methodName);
        }




        public static void StopAllCoroutine()
        {
            MIsfvBehaviour.StopAllCoroutines();
        }








        public static void StartAction(string key, IsfvData.DealMethod method, float delayTime, int repeat = 1)
        {
            var temp = new IsfvData { dealMethod = method, DelayTime = delayTime, RepeatCount = repeat };
            MIsfvBehaviour.AddAsyncData(key, temp);
        }





        public static void StopAction(string key)
        {
            MIsfvBehaviour.RemoveAsyncData(key);
        }

        public static void StopAllAction()
        {
            MIsfvBehaviour.RemoveAllAction();
        }







        public static void StartUpdateAction(string key, IsfvData.DealMethod method, float time)
        {
            var temp = new IsfvData { dealMethod = method, DelayTime = time, RepeatCount = -1 };
            MIsfvBehaviour.AddUpdateAction(key, temp);
        }





        public static void StopUpdateAction(string key)
        {
            MIsfvBehaviour.RemoveUpdateAction(key);
        }

        public static void DelayedAction(float delayTime, IsfvData.DealMethod method, int repeat = 1,
            string key = null)
        {
            key ??= $"{nameof(DelayedAction)}{DateTime.Now.Millisecond}";
            var temp = new IsfvData
            {
                dealMethod = () =>
                {
                    method?.Invoke();
                    ClearDelayedAction(key);
                },
                DelayTime = delayTime, RepeatCount = repeat
            };
            MIsfvBehaviour.AddAsyncTempData(key, temp);
        }

        public static void ClearDelayedAction(string key)
        {
            MIsfvBehaviour.RemoveAsyncTempData(key);
        }
    }
}