using System.Collections;
using UnityEngine;

namespace ChillConnect
{
    public static class CrazyAsyKit
    {
        private static AsyncKitUnityBehaviour sAsyncKitUnityBehaviour;


        private static readonly object SynClock = new object();

        private static AsyncKitUnityBehaviour mAsyncKitUnityBehaviour
        {
            get
            {
                if (sAsyncKitUnityBehaviour != null)
                {
                    return sAsyncKitUnityBehaviour;
                }

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


                    sAsyncKitUnityBehaviour = AsyncKitUnityBehaviour.Instance;
                    if (sAsyncKitUnityBehaviour != null)
                    {
                        sAsyncKitUnityBehaviour.name = "[ CrazyAsyKit ]";
                    }
                }

                return sAsyncKitUnityBehaviour;
            }
        }

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

        public static void StopCoroutine(Coroutine enumerator)
        {
            mAsyncKitUnityBehaviour.StopCoroutine(enumerator);
        }

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


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