#if UNITY_WEBGL

using System;
using System.Runtime.InteropServices;
using LitJson;
using EngineSdkConverter.Unity.Plugins.WebGL;
using UnityEngine;


namespace EngineSdkConverter.Unity.SDK
{
    public class WebGLInterface : ISDKInterface
    {

        [DllImport("__Internal")]
        private static extern void loadJsScriptToBridge(string channelType);

        [DllImport("__Internal")]
        private static extern void InitToBridge(string initEventInfo);

        [DllImport("__Internal")]
        private static extern void LoginToBridge(string loginEventInfo);

        [DllImport("__Internal")]
        private static extern void PayToBridge(string payEventInfo);

        [DllImport("__Internal")]
        private static extern void SetShareInfoToBridge(string shareEventInfo);

        [DllImport("__Internal")]
        private static extern void ShareToBridge(string shareEventInfo);

        [DllImport("__Internal")]
        private static extern void GetShareInfoBridge(string shareEventInfo);

        [DllImport("__Internal")]
        private static extern void GetAdInfoBridge();

        [DllImport("__Internal")]
        private static extern void GAEventTrackToBridge(string reportEventInfo);

        [DllImport("__Internal")]
        private static extern void GAProfileTrackToBridge(string reportEventInfo);

        [DllImport("__Internal")]
        private static extern void TxAdSDKReportToBridge(string txAdSDKReportEventInfo);

        [DllImport("__Internal")]
        private static extern void CheckBindToBridge(string bindEventInfo);

        [DllImport("__Internal")]
        private static extern void SendSmsCodeToBridge(string loginEventInfo);

        [DllImport("__Internal")]
        private static extern void BindToBridge(string bindEventInfo);

        [DllImport("__Internal")]
        private static extern void SetWxUserInfoToBridge(string type, string userInfo);

        [DllImport("__Internal")]
        private static extern void UpdateServerUrlToBridge(string serverUrl);

        [DllImport("__Internal")]
        private static extern string GetDeviceLevelToBridge();

        [DllImport("__Internal")]
        private static extern void CreatGridGamePanelToBridge(string gridGameModule);
        
        [DllImport("__Internal")]
        private static extern void ShowGridGamePanelToBridge(string gridGamePanelId);
        
        [DllImport("__Internal")]
        private static extern void HideGridGamePanelToBridge(string gridGamePanelId);
        
        [DllImport("__Internal")]
        private static extern void DestroyGridGamePanelToBridge(string gridGamePanelId);

        [DllImport("__Internal")]
        private static extern void MicroPaymentToBridge(string payEventInfo);

        [DllImport("__Internal")]
        private static extern void RequestUserRiskRankToBridge();

        [DllImport("__Internal")]
        private static extern void RequestMchTransferToBridge(string mchTransferEventInfo);

        public override void Init()
        {
            TYConfigManager.LoadConfig();
            loadJsScriptToBridge(TYConfigManager.GetChannelType());
        }

        public override void InitSDK(Action<string> callback)
        {
            InitEventBuilder.InitEvent initEvent = new InitEventBuilder.InitEvent();
            InitSDK(initEvent, callback);
        }

        public override void InitSDK(InitEventBuilder.InitEvent initEvent, Action<string> callback)
        {
            NativeCallBack.initResultCallback = callback;
            if (initEvent != null && !string.IsNullOrEmpty(initEvent.serverURL))
            {
                TYConfigManager.configData.tuyooParam.serverUrl = initEvent.serverURL;
            }
            InitToBridge(JsonUtility.ToJson(TYConfigManager.configData));
        }

        public override void UpdateServerUrl(string serverUrl)
        {
            UpdateServerUrlToBridge(serverUrl);
        }

        public override void Pay(PayEventBuilder.PayEvent payEvent, Action<string> callback)
        {
            NativeCallBack.payResultCallback = callback;
            string payEventInfo = JsonMapper.ToJson(payEvent);
            PayToBridge(payEventInfo);
        }

        public override void LoginByType(LoginEventBuilder.LoginEvent loginEvent, Action<string> callback)
        {
            NativeCallBack.loginResultCallback = callback;
            string loginEventInfo = JsonMapper.ToJson(loginEvent);
            LoginToBridge(loginEventInfo);
        }

        public override void GetLocalProductInfo(Action<string> callback)
        {
            throw new NotImplementedException();
        }

        public override void Login(Action<string> callback)
        {
            LoginEventBuilder.LoginEvent loginEvent = new LoginEventBuilder()
            .SetLoginType(TYConfigManager.GetChannelType())
            .Build();
            LoginByType(loginEvent, callback);
        }

        public override void SendCode(LoginEventBuilder.LoginEvent loginEvent, Action<string> callback)
        {
            NativeCallBack.sendCodeResultCallback = callback;
            string loginEventInfo = JsonMapper.ToJson(loginEvent);
            SendSmsCodeToBridge(loginEventInfo);
        }

        public override void Charge(PayEventBuilder.PayEvent payEvent, Action<string> callback)
        {

        }

        public override void SetPushResultHandler(Action<string> onReceive, Action<string> onClick)
        {

        }

        public override void AppsFlyerEventReport(ReportEventBuilder.ReportEvent reportEvent)
        {

        }

        public override void SilentLogin(Action<string> callback)
        {

        }

        public override void Logout(Action<string> callback)
        {

        }

        public override void LogoutChannel(LoginEventBuilder.LoginEvent logoutEvent, Action<string> callback)
        {

        }

        public override void ExitGame(Action<string> callback)
        {

        }

        public override void GetAppsFlyerDeviceId(Action<string> callback)
        {

        }

        public override void SetAppsFlyerCommonParam(ReportEventBuilder.ReportEvent reportEvent)
        {

        }

        public override void SetFirebaseUserProperty(ReportEventBuilder.ReportEvent reportEvent)
        {

        }

        public override void SetFirebaseCommonParam(ReportEventBuilder.ReportEvent reportEvent)
        {

        }

        public override void FirebaseEventReport(ReportEventBuilder.ReportEvent reportEvent)
        {

        }

        public override void Check(BindEventBuilder.BindEvent bindEvent, Action<string> callback)
        {
            NativeCallBack.checkResultCallback = callback;
            string bindEventInfo = JsonMapper.ToJson(bindEvent);
            CheckBindToBridge(bindEventInfo);
        }

        public override void Bind(BindEventBuilder.BindEvent bindEvent, Action<string> callback)
        {
            NativeCallBack.bindCallback = callback;
            string bindEventInfo = JsonMapper.ToJson(bindEvent);
            BindToBridge(bindEventInfo);
        }

        public override void CanBindList(Action<string> callback)
        {

        }

        public override void AlreadyBindList(Action<string> callback)
        {

        }

        public override void GetBindUserList(BindEventBuilder.BindEvent bindEvent, Action<string> callback)
        {

        }

        public override void GAProfileTrack(ReportEventBuilder.ReportEvent reportEvent)
        {
            string reportEventInfo = JsonMapper.ToJson(reportEvent);
            GAProfileTrackToBridge(reportEventInfo);
        }

        public override void GAEventTrack(ReportEventBuilder.ReportEvent reportEvent)
        {
            string reportEventInfo = JsonMapper.ToJson(reportEvent);
            GAEventTrackToBridge(reportEventInfo);
        }

        public override void SetGACommonParam(ReportEventBuilder.ReportEvent reportEvent)
        {

        }

        public override void SetLocalDeliverListener(Action<string, Action, Action<string, string>> callback)
        {

        }

        public override void RequestNotificationPermission()
        {

        }

        public override void SetCertificationListener(Action<string> callback)
        {

        }

        public override void Certificate(CertificationEventBuilder.CertificationEvent certificationEvent, Action<string> callback)
        {

        }

        public override void ReCertificate(CertificationEventBuilder.CertificationEvent certificationEvent, Action<string> callback)
        {

        }

        public override void RefreshCertification()
        {

        }

        public override void SetServerLanguage(string serverLanguage, Action<string> callback)
        {

        }

        public override void GetDeviceId(Action<string> callback)
        {

        }

        public override void LiveDetect(YiDunEventBuilder.YiDunEvent yiDunEvent, Action<string> callback)
        {

        }

        public override void RequestWXAuthorize(Action<string> callback)
        {

        }

        public override void GetWXAuthInfo(Action<string> callback)
        {

        }

        public override void GetYiDunCaptcha(YiDunEventBuilder.YiDunEvent yiDunEvent, Action<string> callback)
        {

        }

        public override void GetAPIVersion(Action<string> callback)
        {

        }

        public override void HasGeneratedGuest(Action<string> callback)
        {

        }

        public override void SetPrivacy(YiDunEventBuilder.YiDunEvent yiDunEvent, Action<string> callback)
        {

        }

        public override void Share(ShareEventBuilder.ShareEvent shareEvent, Action<string> callback)
        {
            NativeCallBack.shareResultCallback = callback;
            string shareEventInfo = JsonMapper.ToJson(shareEvent);
            ShareToBridge(shareEventInfo);
        }

        public override void ResetUserInfo(DYTrackEventBuilder.DYTrackEvent dYTrackEvent, Action<string> callback)
        {

        }

        public override void GetUserInfo(DYTrackEventBuilder.DYTrackEvent dYTrackEvent, Action<string> callback)
        {

        }

        public override void GetLiveInfo(Action<string> callback)
        {

        }

        public override void SetConfig(ConfigEventBuilder.ConfigEvent configEvent, Action<string> callback)
        {

        }

        public override void IsWxInstalled(Action<string> callback)
        {

        }

        public override void SetDefaultShareInfo(ShareEventBuilder.ShareEvent shareEvent, Action<string> callback)
        {
            NativeCallBack.shareResultCallback = callback;
            string shareEventInfo = JsonMapper.ToJson(shareEvent);
            SetShareInfoToBridge(shareEventInfo);
        }

        public override void GetShareInfoFromMarketService(Action<string> callback)
        {
            NativeCallBack.getShareInfoCallback = callback;
            string shareEventInfo = JsonMapper.ToJson(new ShareEventBuilder.ShareEvent());
            GetShareInfoBridge(shareEventInfo);
        }

        public override void GetSharePointInfoFromMarketService(ShareEventBuilder.ShareEvent shareEvent, Action<string> callback)
        {
            NativeCallBack.getShareInfoCallback = callback;
            string shareEventInfo = JsonMapper.ToJson(shareEvent);
            GetShareInfoBridge(shareEventInfo);
        }

        public override void GetAdInfoFromMarketService(Action<string> callback, Action<string> newCallback)
        {
            NativeCallBack.getIconInfoCallback = callback;
            NativeCallBack.getNewIconInfoCallback = newCallback;
            GetAdInfoBridge();
        }

        public override void TxAdSDKReport(TxAdSDKReportEventBuilder.TxAdSDKReportEvent txAdSDKReportEvent)
        {
            string reportInfo = JsonMapper.ToJson(txAdSDKReportEvent);
            TxAdSDKReportToBridge(reportInfo);
        }

        public override void SetWxUserInfo(SetWxUserInfoEventBuilder.SetWxUserInfoEvent setWxUserInfoEvent, Action<string> callback)
        {
            NativeCallBack.setWxUserInfoCallback = callback;
            string type = "0";
            if (setWxUserInfoEvent.wxUserInfo is SetWxUserInfo)
            {
                type = "1";
            }
            else if (setWxUserInfoEvent.wxUserInfo is SetWxEncData)
            {
                type = "2";
            }
            else if (setWxUserInfoEvent.wxUserInfo is SetThreeWxEncData)
            {
                type = "3";
            }
            string thirdExtendEventInfo = JsonMapper.ToJson(setWxUserInfoEvent);
            SetWxUserInfoToBridge(type, thirdExtendEventInfo);
        }

        public override string GetDeviceLevel(Action<string> callback)
        {
            NativeCallBack.getDeviceLevelCallback = callback;
            return GetDeviceLevelToBridge();
        }

        public override void CreateGridGamePanel(GridGameModuleBuilder.GridGameModule gridGameModule, Action<string, object> callback)
        {
            string key = "GridGamePanel_" + Guid.NewGuid().ToString("N");
            if(key != null){
                gridGameModule.gridGamePanelId = key;
                GridGamePanel gridGamePanel = new GridGamePanel().CreateGridGamePanel(gridGameModule, callback);
                Action<string> outCallback = (param) =>
                {
                    callback(param, gridGamePanel);
                };
                NativeCallBack.callbacks[key] = outCallback;
                string info = JsonMapper.ToJson(gridGameModule);
                CreatGridGamePanelToBridge(info);
            }
        }

        internal override void ShowGridGamePanel(string gridGamePanelId)
        {
            ShowGridGamePanelToBridge(gridGamePanelId);
        }

        internal override void HideGridGamePanel(string gridGamePanelId)
        {
            HideGridGamePanelToBridge(gridGamePanelId);
        }

        internal override void DestroyGridGamePanel(string gridGamePanelId)
        {
            DestroyGridGamePanelToBridge(gridGamePanelId);
        }

        public override void MicroPayment(PayEventBuilder.PayEvent payEvent, Action<string> callback)
        {
            NativeCallBack.payResultCallback = callback;
            string payEventInfo = JsonMapper.ToJson(payEvent);
            MicroPaymentToBridge(payEventInfo);
        }

        public override void RequestUserRiskRank(Action<string> callback)
        {
            NativeCallBack.userRiskRankCallback = callback;
            RequestUserRiskRankToBridge();
        }

        public override void RequestMerchantTransfer(MchTransferBuilder.MchTransferEvent mchTransferEvent, Action<string> callback)
        {
            NativeCallBack.mchTransferCallback = callback;
            string mchTransferEventInfo = JsonMapper.ToJson(mchTransferEvent);
            RequestMchTransferToBridge(mchTransferEventInfo);
        }

    }

}

#endif