using System;
using System.Collections.Generic;
using UnityEngine;
using LitJson;

namespace EngineSdkConverter.Unity.SDK
{
    public class NativeCallBack : SDKMonoBehaviour
    {
        public static Action<string> initResultCallback;
        public static Action<string> silentLoginResultCallback;
        public static Action<string> loginResultCallback;
        public static Action<string> sendCodeResultCallback;
        public static Action<string> logoutResultCallback;
        public static Action<string> logoutChannelResultCallback;
        public static Action<string> autoLogoutResultCallback;
        public static Action<string> exitGameResultCallback;
        public static Action<string> chargeResultCallback;
        public static Action<string> payResultCallback;
        public static Action<string> getLocalProductInfoCallback;
        public static Action<string> onPushResultReceiveCallback;
        public static Action<string> onPushResultClickCallback;
        public static Action<string> getAFDeviceIdCallback;
        public static Action<string> checkResultCallback;
        public static Action<string> bindCallback;
        public static Action<string> canBindListResultCallback;
        public static Action<string> alreadyBindListResultCallback;
        public static Action<string> userBindListResultCallback;
        // 设置发货回调
        public static Action<string, Action, Action<string, string>> setDeliverResultCallback;
        public static Action<string> getDeviceIdResultCallback;
        public static Action<string> liveDetectResultCallback;
        public static Action<string> requestWXAuthorizeResultCallback;
        public static Action<string> getWXAuthInfoResultCallback;
        public static Action<string> getYiDunCaptchaResultCallback;
        public static Action<string> getAPIVersionResultCallback;
        public static Action<string> hasGeneratedGuestResultCallback;
        public static Action<string> setPrivacyResultCallback;
        public static Action<string> shareResultCallback;
        public static Action<string> getShareInfoCallback;
        public static Action<string> getIconInfoCallback;
        public static Action<string> getNewIconInfoCallback;
        public static Action<string> setWxUserInfoCallback;
        public static Action<string> setCertificationListenerCallback;
        public static Action<string> certificationResultCallback;
        public static Action<string> reCertificationResultCallback;
        public static Action<string> resetUserInfoResultCallback;
        public static Action<string> getUserInfoResultCallback;
        public static Action<string> getLiveInfoResultCallback;
        public static Action<string> setConfigResultCallback;
        public static Action<string> isWxInstalledResultCallback;
        public static Action<string> getDeviceLevelCallback;
        public static Action<string> userRiskRankCallback;
        public static Action<string> mchTransferCallback;

        private static Dictionary<string, Action<string>> actionMap = new Dictionary<string, Action<string>>();

         // 统一回调入口
        public static Dictionary<string, object> callbacks = new Dictionary<string, object>();

        public void UnifiedNativeCallback(string result)
        {
            JsonData json = JsonMapper.ToObject(result);
            string action = json["action"].ToString();
            if (callbacks.ContainsKey(action))
            {
                ((Action<string>)callbacks[action])?.Invoke(result);
            }
        }

        // 切换到主线程回调
        private static readonly Queue<Action> _actions = new Queue<Action>();

        public static void Enqueue(Action action)
        {
            if (action == null) return;

            lock (_actions)
            {
                _actions.Enqueue(action);
            }
        }

        private void Update()
        {
            lock (_actions)
            {
                while (_actions.Count > 0)
                {
                    var action = _actions.Dequeue();
                    action?.Invoke();
                }
            }
        }

        // 添加或更新 key-value
        public static void Put(string key, Action<string> action)
        {
            actionMap[key] = action;
            
        }

        /*******************************************************************************************/

        private void Awake()
        {
            DontDestroyOnLoad(GameObject);
        }

        public void InitResult(string result)
        {
            Debug.Log("InitResult------>in! result=" + result);
            initResultCallback?.Invoke(result);
        }

        public void SilentLoginResult(string result)
        {
            Debug.Log("SilentLoginResult------>in! result=" + result);
            silentLoginResultCallback?.Invoke(result);
        }

        public void LoginResult(string result)
        {
            Debug.Log("LoginResult------>in! result=" + result);
            loginResultCallback?.Invoke(result);
        }

        public void SendCodeResult(string result)
        {
            Debug.Log("SendCodeResult------>in! result=" + result);
            sendCodeResultCallback?.Invoke(result);
        }

        public void LogoutResult(string result)
        {
            Debug.Log("LogoutResult------>in! result=" + result);
            logoutResultCallback?.Invoke(result);
        }

        public void LogoutChannelResult(string result)
        {
            Debug.Log("LogoutChannelResult------>in! result=" + result);
            logoutChannelResultCallback?.Invoke(result);
        }

        public void ExitGameResult(string result)
        {
            Debug.Log("ExitGameResult------>in! result=" + result);
            exitGameResultCallback?.Invoke(result);
        }

        public void ChargeResult(string result)
        {
            Debug.Log("ChargeResult------>in! result=" + result);
            chargeResultCallback?.Invoke(result);
        }

        public void PayResult(string result)
        {
            Debug.Log("PayResult------>in! result=" + result);
            payResultCallback?.Invoke(result);
        }

        public void GetLocalProductInfoResult(string result)
        {
            Debug.Log("GetLocalProductInfoResult------>in! result=" + result);
            getLocalProductInfoCallback?.Invoke(result);
        }

        public void onPushResultReceive(string result)
        {
            Debug.Log("onPushResultReceive------>in! result=" + result);
            onPushResultReceiveCallback?.Invoke(result);
        }

        public void onPushResultClick(string result)
        {
            Debug.Log("onPushResultClick------>in! result=" + result);
            onPushResultClickCallback?.Invoke(result);
        }

        public void GetAFDeviceId(string result)
        {
            Debug.Log("GetAFDeviceId------>in! result=" + result);
            getAFDeviceIdCallback?.Invoke(result);
        }

        public void CheckResult(string result)
        {
            Debug.Log("checkResult------>in! result=" + result);
            checkResultCallback?.Invoke(result);
        }

        public void onGetSnsInfoFail(string result)
        {
            Debug.Log("onGetSnsInfoFail------>in! result=" + result);
            bindCallback?.Invoke(result);
        }

        public void onCheckSnsInfoFail(string result)
        {
            Debug.Log("onCheckSnsInfoFail------>in! result=" + result);
            bindCallback?.Invoke(result);
        }

        public void onBindSnsInfoFail(string result)
        {
            Debug.Log("onBindSnsInfoFail------>in! result=" + result);
            bindCallback?.Invoke(result);
        }

        public void onBindSucc(string result)
        {
            Debug.Log("onBindSucc------>in! result=" + result);
            bindCallback?.Invoke(result);
        }

        public void CanBindListResult(string result)
        {
            Debug.Log("CanBindListResult------>in! result=" + result);
            canBindListResultCallback?.Invoke(result);
        }

        public void AlreadyBindListResult(string result)
        {
            Debug.Log("AlreadyBindListResult------>in! result=" + result);
            alreadyBindListResultCallback?.Invoke(result);
        }

        public void UserBindListResult(string result)
        {
            Debug.Log("UserBindListResult------>in! result=" + result);
            userBindListResultCallback?.Invoke(result);
        }

        public void GetDeviceIdResult(string result)
        {
            Debug.Log("GetDeviceIdResult------>in! result=" + result);
            getDeviceIdResultCallback?.Invoke(result);
        }

        public void LiveDetectResult(string result)
        {
            Debug.Log("LiveDetectResult------>in! result=" + result);
            liveDetectResultCallback?.Invoke(result);
        }

        public void RequestWXAuthorizeResult(string result)
        {
            Debug.Log("RequestWXAuthorizeResult------>in! result=" + result);
            requestWXAuthorizeResultCallback?.Invoke(result);
        }

        public void GetWXAuthInfoResult(string result)
        {
            Debug.Log("GetWXAuthInfoResult------>in! result=" + result);
            getWXAuthInfoResultCallback?.Invoke(result);
        }

        public void GetYiDunCaptchaResult(string result)
        {
            Debug.Log("GetYiDunCaptchaResult------>in! result=" + result);
            getYiDunCaptchaResultCallback?.Invoke(result);
        }

        public void GetAPIVersionResult(string result)
        {
            Debug.Log("GetAPIVersionResult------>in! result=" + result);
            getAPIVersionResultCallback?.Invoke(result);
        }

        public void HasGeneratedGuestResult(string result)
        {
            Debug.Log("HasGeneratedGuestResult------>in! result=" + result);
            hasGeneratedGuestResultCallback?.Invoke(result);
        }

        public void SetPrivacyResult(string result)
        {
            Debug.Log("SetPrivacyResult------>in! result=" + result);
            setPrivacyResultCallback?.Invoke(result);
        }

        public void ShareResult(string result)
        {
            Debug.Log("ShareResult------>in! result=" + result);
            shareResultCallback?.Invoke(result);
        }

        public void GetShareInfoResult(string result)
        {
            Debug.Log("GetShareInfoResult------>in! result=" + result);
            getShareInfoCallback?.Invoke(result);
        }

        public void GetIconInfoResult(string result)
        {
            Debug.Log("GetIconInfoResult------>in! result=" + result);
            getIconInfoCallback?.Invoke(result);
        }

        public void GetNewIconInfoResult(string result)
        {
            Debug.Log("GetNewIconInfoResult------>in! result=" + result);
            getNewIconInfoCallback?.Invoke(result);
        }

        public void SetWxInfoResult(string result)
        {
            Debug.Log("SetWxInfoResult------>in! result=" + result);
            setWxUserInfoCallback?.Invoke(result);
        }

        public void ResetUserInfoResult(string result)
        {
            Debug.Log("ResetUserInfoResult------>in! result=" + result);
            resetUserInfoResultCallback?.Invoke(result);
        }

        public void GetUserInfoResult(string result)
        {
            Debug.Log("GetUserInfoResult------>in! result=" + result);
            getUserInfoResultCallback?.Invoke(result);
        }

        public void GetLiveInfoResult(string result)
        {
            Debug.Log("GetLiveInfoResult------>in! result=" + result);
            getLiveInfoResultCallback?.Invoke(result);
        }

        public void AutoLogoutResult(string result)
        {
            Debug.Log("AutoLogoutResult------>in! result=" + result);
            autoLogoutResultCallback?.Invoke(result);
        }

        public void SetConfigResult(string result)
        {
            Debug.Log("SetConfigResult------>in! result=" + result);
            setConfigResultCallback?.Invoke(result);
        }

        public void IsWxInstalledResult(string result)
        {
            Debug.Log("IsWxInstalledResult------>in! result=" + result);
            isWxInstalledResultCallback?.Invoke(result);
        }

        public void onNativeCallback(string result)
        {
            string method = getMethodFromResult(result);
            bool contains = actionMap.ContainsKey(method);
            Debug.Log("onNativeCallback------>in! result=" + result + ", method=" + method + ", contains=" + contains);
            if (contains)
            {
                actionMap[method]?.Invoke(getDataFromResult(result));
                actionMap.Remove(method);
            }
        }

        private string getMethodFromResult(string result)
        {
            return result.Substring(0, result.IndexOf(';'));
        }

        private string getDataFromResult(string result)
        {
            return result.Substring(result.IndexOf(';') + 1);
        }

        public void SetCertificationListener(string result)
        {
            Debug.Log("SetCertificationListener------>in! result=" + result);
            setCertificationListenerCallback?.Invoke(result);
        }

        public void CertificationResult(string result)
        {
            Debug.Log("CertificationResult------>in! result=" + result);
            certificationResultCallback?.Invoke(result);
        }

        public void ReCertificationResult(string result)
        {
            Debug.Log("ReCertificationResult------>in! result=" + result);
            reCertificationResultCallback?.Invoke(result);
        }
        public void GetDeviceLevelResult(string result)
        {
            Debug.Log("GetDeviceLevelResult------>in! result=" + result);
            getDeviceLevelCallback?.Invoke(result);
        }
        public void UserRiskRankResult(string result)
        {
            Debug.Log("UserRiskRankResult------>in! result=" + result);
            userRiskRankCallback?.Invoke(result);
        }
        public void MchTransferResult(string result)
        {
            Debug.Log("MchTransferResult------>in! result=" + result);
            mchTransferCallback?.Invoke(result);
        }
    }
}