var TuyooSdkBridge = {

    loadJsScriptToBridge: function (channelType) {
        var channelTypeFromCS = UTF8ToString(channelType);
        console.log("channelTypeFromCS:" + channelTypeFromCS);
    },

    InitToBridge: function (initEventInfo) {
        try {
            var initCallback = function (code, result, error) {
                var callbackString = JSON.stringify({ code, result, error });
                SendMessage("NativeCallBack", "InitResult", callbackString);
            }
            var initInfoFromCS = UTF8ToString(initEventInfo);
            var initInfo = JSON.parse(initInfoFromCS);
            var tyInitParams = {};
            tyInitParams.appId = initInfo.tuyooParam.appId;
            tyInitParams.gameId = initInfo.tuyooParam.gameId;
            tyInitParams.pay_version = "hall37";
            tyInitParams.baseurl = initInfo.tuyooParam.serverUrl.endsWith("/") ? initInfo.tuyooParam.serverUrl : initInfo.tuyooParam.serverUrl + "/";
            tyInitParams.bi_base_url = "https://cbi.tuyoo.com/";
            tyInitParams.cloudId = initInfo.tuyooParam.cloudId;
            tyInitParams.packageName = initInfo.package;
            tyInitParams.clientId = initInfo.tuyooParam.clientId;
            tyInitParams.jingdongAppId = initInfo.login.jingdongminigame.jingdongAppId;
            tyInitParams.debug = String(initInfo.debug).toLowerCase() === "true" || false;
            console.log("initInfo: " + JSON.stringify(initInfo));
            if (initInfo.login.jingdongminigame.packageType === "wasmsplit" && GameGlobal.tywx) {
                GameGlobal.tywx.StateInfo.debugMode = initInfo.debug.toLowerCase() === "true";
                GameGlobal.tywx.SystemInfo.loginUrl = initInfo.tuyooParam.serverUrl.endsWith("/") ? initInfo.tuyooParam.serverUrl : initInfo.tuyooParam.serverUrl + "/";
                GameGlobal.tywx.SystemInfo.clientId = initInfo.tuyooParam.clientId;
                GameGlobal.tywx.SystemInfo.cloudId = initInfo.tuyooParam.cloudId;
                GameGlobal.tywx.SystemInfo.appId = initInfo.tuyooParam.appId;
                GameGlobal.tywx.SystemInfo.gameId = initInfo.tuyooParam.gameId;
                GameGlobal.tywx.SystemInfo.wxAppId = initInfo.login.tywx.wx_appId;
                GameGlobal.tywx.SystemInfo.packageName = initInfo.package;
            }
            GameGlobal.TuyooSdk.OnInit(initInfo.tuyooParam.channelType, tyInitParams, initCallback, this);
        } catch (e) {
            console.error("InitToBridge catch error: " + e);
        }
    },

    // 切换SDK域名
    UpdateServerUrlToBridge: function (serverUrl) {
        try {
            var serverUrlFromCS = UTF8ToString(serverUrl);
            window.TuyooSdk.baseuri = (serverUrlFromCS.substring(serverUrlFromCS.length - 1) === '/') ? serverUrlFromCS.substring(0, serverUrlFromCS.length - 1) : serverUrlFromCS;
            console.log("update serverUrl to: " + window.TuyooSdk.baseuri);
        } catch (e) {
            console.error("UpdateServerUrlToBridge catch error: " + e);
        }
    },

    LoginToBridge: function (loginEventInfo) {
        try {
            var loginCallback = function (code, result, error) {
                var callbackResult = {
                    code: code,
                    result: result && result.logindata ? result.logindata : result,
                    error: error
                };
                var callbackString = JSON.stringify(callbackResult);
                SendMessage("NativeCallBack", "LoginResult", callbackString);
            }
            var loginInfoFromCS = UTF8ToString(loginEventInfo);
            var loginInfo = JSON.parse(loginInfoFromCS);
            if (loginInfo.loginType === "wx.cn.minigame" && GameGlobal.tywx) {
                GameGlobal.tywx.TuyooSDK.login(loginCallback);
                return;
            }
            var tyLoginParams = {};
            if (loginInfo.loginType) {
                tyLoginParams.ChannelType = loginInfo.loginType;
            }
            if (loginInfo.extraParams) {
                var extraParams = JSON.parse(loginInfo.extraParams);
                tyLoginParams.extraParams = extraParams;
            }
            console.log("tyLoginParams: " + JSON.stringify(tyLoginParams));
            GameGlobal.TuyooSdk.Login(tyLoginParams, loginCallback, this);
        } catch (e) {
            console.error("LoginToBridge catch error: " + e);
        }
    },

    PayToBridge: function (payEventInfo) {
        try {
            var payCallback = function (code, result, error) {
                var callbackString = JSON.stringify({ code, result, error });
                SendMessage("NativeCallBack", "PayResult", callbackString);
            }
            var payInfoFromCS = UTF8ToString(payEventInfo);
            var payInfo = JSON.parse(payInfoFromCS);
            var tyPayParams = {};
            tyPayParams.prodCount = 1;
            tyPayParams.appInfo = "";
            if (payInfo.chargeService) {
                tyPayParams.ChargeType = payInfo.chargeService;
            }
            if (payInfo.productCount) {
                tyPayParams.prodCount = payInfo.productCount;
            }
            if (payInfo.serverAppInfo) {
                tyPayParams.appInfo = payInfo.serverAppInfo;
            }
            if (payInfo.gameOrderId) {
                tyPayParams.prodOrderId = payInfo.gameOrderId;
            }
            if (payInfo.extraParams) {
                var extraParams = JSON.parse(payInfo.extraParams);
                if(extraParams.jingdongCurrency) {
                    tyPayParams.jingdongCurrency = extraParams.jingdongCurrency;
                    delete extraParams.jingdongCurrency;
                }
                tyPayParams.extraParams = extraParams;
            }
            tyPayParams.prodId = payInfo.productId;
            tyPayParams.prodName = payInfo.productName;
            tyPayParams.prodPrice = payInfo.productPrice;
            console.log("tyPayParams: " + JSON.stringify(tyPayParams));
            GameGlobal.TuyooSdk.Pay(tyPayParams, payCallback, this);
        } catch (e) {
            console.error("PayToBridge catch error: " + e);
        }
    },

    SetShareInfoToBridge: function (shareEventInfo) {
        try {
            var shareCallback = function (code, result, error) {
                var callbackString = JSON.stringify({ code, result, error });
                SendMessage("NativeCallBack", "ShareResult", callbackString);
            }
            var shareInfoFromCS = UTF8ToString(shareEventInfo);
            var shareInfo = JSON.parse(shareInfoFromCS);
            var shareInfoToJs = shareInfo.resource;
            var tyShareParams = {};
            shareInfoToJs.title && (tyShareParams.title = shareInfoToJs.title);
            shareInfoToJs.imageUrl && (tyShareParams.imageUrl = shareInfoToJs.imageUrl);
            shareInfoToJs.desc && (tyShareParams.desc = shareInfoToJs.desc);
            shareInfoToJs.type && (tyShareParams.type = shareInfoToJs.type);
            if(shareInfoToJs.extraParams) {
                var extraParams = JSON.parse(shareInfoToJs.extraParams);
                if(extraParams.channel) {
                    tyShareParams.channel = extraParams.channel;
                    delete extraParams.channel;
                }
                if(extraParams.url) {
                    tyShareParams.url = extraParams.url;
                    delete extraParams.url;
                }
                if(extraParams.keyShareChannel) {
                    tyShareParams.keyShareChannel = extraParams.keyShareChannel;
                    delete extraParams.keyShareChannel;
                }
                if(extraParams.localImageUrl) {
                    tyShareParams.localImageUrl = extraParams.localImageUrl;
                    delete extraParams.localImageUrl;
                }
                if(extraParams.onlineImageUrl) {
                    tyShareParams.onlineImageUrl = extraParams.onlineImageUrl;
                    delete extraParams.onlineImageUrl;
                }
                tyShareParams.extraParams = extraParams;
            }
            tyShareParams.callback = shareCallback;
            console.log("tyShareParams: " + JSON.stringify(tyShareParams));
            GameGlobal.TuyooSdk.SetDefaultShareInfo(tyShareParams);
        } catch (e) {
            console.error("SetShareInfoToBridge catch error: " + e);
        }
    },

    ShareToBridge: function (shareEventInfo) {
        try {
            var shareCallback = function (code, result, error) {
                var callbackString = JSON.stringify({ code, result, error });
                SendMessage("NativeCallBack", "ShareResult", callbackString);
            }
            var shareInfoFromCS = UTF8ToString(shareEventInfo);
            var shareInfo = JSON.parse(shareInfoFromCS);
            var shareInfoToJs = shareInfo.resource;
            var tyShareParams = {};
            shareInfoToJs.title && (tyShareParams.title = shareInfoToJs.title);
            shareInfoToJs.imageUrl && (tyShareParams.imageUrl = shareInfoToJs.imageUrl);
            shareInfoToJs.desc && (tyShareParams.desc = shareInfoToJs.desc);
            shareInfoToJs.type && (tyShareParams.type = shareInfoToJs.type);
            if(shareInfoToJs.extraParams) {
                var extraParams = JSON.parse(shareInfoToJs.extraParams);
                if(extraParams.channel) {
                    tyShareParams.channel = extraParams.channel;
                    delete extraParams.channel;
                }
                if(extraParams.url) {
                    tyShareParams.url = extraParams.url;
                    delete extraParams.url;
                }
                if(extraParams.keyShareChannel) {
                    tyShareParams.keyShareChannel = extraParams.keyShareChannel;
                    delete extraParams.keyShareChannel;
                }
                if(extraParams.localImageUrl) {
                    tyShareParams.localImageUrl = extraParams.localImageUrl;
                    delete extraParams.localImageUrl;
                }
                if(extraParams.onlineImageUrl) {
                    tyShareParams.onlineImageUrl = extraParams.onlineImageUrl;
                    delete extraParams.onlineImageUrl;
                }
                tyShareParams.extraParams = extraParams;
            }
            console.log("tyShareParams: " + JSON.stringify(tyShareParams));
            GameGlobal.TuyooSdk.Share(tyShareParams, shareCallback);
        } catch (e) {
            console.error("ShareToBridge catch error: " + e);
        }
    },

    GAEventTrackToBridge: function (reportEventInfo) {
        try {
            var reportInfoFromCS = UTF8ToString(reportEventInfo);
            var reportInfo = JSON.parse(reportInfoFromCS);
            if (!reportInfo.typeName) {
                reportInfo.typeName = "track";
            }
            GameGlobal.TuyooSdk.bi.trackWithType(reportInfo.typeName, reportInfo.eventName, reportInfo.reportEvent);
        } catch (e) {
            console.error("GAEventTrackToBridge catch error: " + e);
        }
    },

    GAProfileTrackToBridge: function (reportEventInfo) {
        try {
            var reportInfoFromCS = UTF8ToString(reportEventInfo);
            var reportInfo = JSON.parse(reportInfoFromCS);
            GameGlobal.TuyooSdk.bi.profileTrack(reportInfo.profileType, reportInfo.profileSubjectType, reportInfo.profileSubjectPrimary, reportInfo.reportEvent);
        } catch (e) {
            console.error("GAProfileTrackToBridge catch error: " + e);
        }
    },

    GetShareInfoBridge: function (shareEventInfo) {
        console.error("current channel not support GetShareInfo function");
    },

    GetAdInfoBridge: function () {
        console.error("current channel not support GetAdInfo function");
    },

    TxAdSDKReportToBridge: function (txAdReportInfo) {
        console.error("current channel not support TxAdSDKReport function");
    },

    CheckBindToBridge: function (bindEventInfo) {
        console.error("current channel not support CheckBind function");
    },

    SendSmsCodeToBridge: function (loginEventInfo) {
        console.error("current channel not support SendSmsCode function");
    },

    BindToBridge: function (bindEventInfo) {
        console.error("current channel not support Bind function");
    },

    SetWxUserInfoToBridge: function (type, userInfo) {
        console.error("current channel not support SetWxUserInfo function");
    },

    GetDeviceLevelToBridge: function() {
        try {
            var callback = function (code, result, error) {
                var callbackString = JSON.stringify({ code, result, error });
                SendMessage("NativeCallBack", "GetDeviceLevelResult", callbackString);
            }
            GameGlobal.TuyooSdk.getDeviceBenchmarkLevel(callback, this);
        } catch (error) {
            console.error("GetDeviceLevelToBridge catch error: " + error);
        }
    },

    CreatGridGamePanelToBridge: function(gridGameModule) {
        console.error("current channel not support CreatGridGamePanel function");
    },

    ShowGridGamePanelToBridge: function(panelId) {
        console.error("current channel not support ShowGridGamePanel function");
    },

    HideGridGamePanelToBridge: function(panelId) {
        console.error("current channel not support HideGridGamePanel function");
    },

    DestroyGridGamePanelToBridge: function(panelId) {
        console.error("current channel not support DestroyGridGamePanel function");
    },

    NavigateToMiniProgramToBridge: function(navigateEventInfo) {
        try {
            var navigateCallback = function (code, result, error) {
                var callbackString = JSON.stringify({ code, result, error });
                SendMessage("NativeCallBack", "NavigateToMiniProgramResult", callbackString);
            }
            var navigateInfoFromCS = UTF8ToString(navigateEventInfo);
            var navigateInfo = JSON.parse(navigateInfoFromCS);
            var tyNavigateParams = {};
            if (navigateInfo.targetAppId) {
                tyNavigateParams.targetAppId = navigateInfo.targetAppId;
            }
            if (navigateInfo.extraParams) {
                tyNavigateParams.extraParams = JSON.parse(navigateInfo.extraParams);
            }
            if (navigateInfo.envType) {
                tyNavigateParams.env = navigateInfo.envType == "trial" ? "202" : "201";
            } else {
                tyNavigateParams.env = "201";
            }
            console.log("tyNavigateParams: " + JSON.stringify(tyNavigateParams));
            GameGlobal.TuyooSdk.navigateToMiniProgram(tyNavigateParams, navigateCallback, this);
        } catch (e) {
            console.error("NavigateToMiniProgramToBridge catch error: " + e);
        }
    }

}

mergeInto(LibraryManager.library, TuyooSdkBridge);