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.xiaomiAppId = initInfo.login.xiaomiminigame.xiaomiAppId;
            console.log("initInfo: " + JSON.stringify(initInfo));
            window.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);
            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));
            window.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);
                tyPayParams.extraParams = extraParams;
            }
            tyPayParams.prodId = payInfo.productId;
            tyPayParams.prodName = payInfo.productName;
            tyPayParams.prodPrice = payInfo.productPrice;
            console.log("tyPayParams: " + JSON.stringify(tyPayParams));
            window.TuyooSdk.Pay(tyPayParams, payCallback, this);
        } catch (e) {
            console.error("PayToBridge catch error: " + e);
        }
    },

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

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

    GAEventTrackToBridge: function (reportEventInfo) {
        try {
            var reportInfoFromCS = UTF8ToString(reportEventInfo);
            var reportInfo = JSON.parse(reportInfoFromCS);
            if (!reportInfo.typeName) {
                reportInfo.typeName = "track";
            }
            window.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);
            window.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);
        //     }
        //     window.TuyooSdk.getDeviceBenchmarkLevel(callback, this);
        // } catch (error) {
        //     console.error("GetDeviceLevelToBridge catch error: " + error);
        // }
        console.error("current channel not support GetDeviceLevelToBridge function");
    },

    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");
    }

}

mergeInto(LibraryManager.library, TuyooSdkBridge);