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.bytedanceAppId = initInfo.login.zijieminigame.bytedanceAppId;
            tyInitParams.debug = String(initInfo.debug).toLowerCase() === "true" || false;
            if (initInfo.login.zijieminigame.shareManagerUrl) {
                tyInitParams.shareManagerUrl = initInfo.login.zijieminigame.shareManagerUrl.endsWith("/") ? initInfo.login.zijieminigame.shareManagerUrl : initInfo.login.zijieminigame.shareManagerUrl + "/";
            }
            // 默认关闭拉取营销系统分享配置的开关
            tyInitParams.pullShareConfig = false;
            if(initInfo.login.zijieminigame.pullShareConfig === true) {
                tyInitParams.pullShareConfig = true;
            }
            console.log("initInfo: " + JSON.stringify(initInfo));
            GameGlobal.TuyooSdk.OnInit("bytedance.cn.iap", tyInitParams, initCallback, this);
        } catch (e) {
            console.error("InitToBridge catch error: " + e);
        }
    },

    // 切换SDK域名
    UpdateServerUrlToBridge: function (serverUrl) {
        try {
            var serverUrlFromCS = UTF8ToString(serverUrl);
            GameGlobal.TuyooSdk.baseuri = (serverUrlFromCS.substring(serverUrlFromCS.length - 1) === '/') ? serverUrlFromCS.substring(0, serverUrlFromCS.length - 1) : serverUrlFromCS;
            console.log("update serverUrl to: " + GameGlobal.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 = "bytedance.cn.iap";
            }
            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);
                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 resource = shareInfo.resource;
            var shareInfoToJs = {};
            var inviteInfo = {};
            var videoInfo = {};
            resource.content && (inviteInfo.shareContent = resource.content);
            resource.title && (inviteInfo.sharePointTitle = resource.title);
            resource.imageUrl && (inviteInfo.sharePicLocUrl = resource.imageUrl);
            resource.shareTemplateId && (inviteInfo.shareTemplateId = resource.shareTemplateId);

            resource.videoContent && (videoInfo.shareContent = resource.videoContent);
            resource.shareTopic && (videoInfo.shareTopic = resource.shareTopic);
            resource.videoShareTemplateId && (videoInfo.shareTemplateId = resource.videoShareTemplateId);
            resource.shareDefaultBgm && (videoInfo.shareDefaultBgm = resource.shareDefaultBgm);
            resource.shareCutTemplateId && (videoInfo.shareCutTemplateId = resource.shareCutTemplateId);
            resource.shareVideoPath && (videoInfo.shareVideoPath = resource.shareVideoPath);

            shareInfoToJs.callback = shareCallback;
            shareInfoToJs.invite = inviteInfo;
            shareInfoToJs.video = videoInfo;
            console.log("shareInfoToJs: " + JSON.stringify(shareInfoToJs.invite) + JSON.stringify(shareInfoToJs.video));
            GameGlobal.TuyooSdk.SetDefaultShareInfo(shareInfoToJs);
        } 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 resource = shareInfo.resource;
            var shareInfoToJs = {};
            shareInfoToJs.shareType = resource.type;
            resource.sharePointId && (shareInfoToJs.sharePointName = resource.sharePointId);
            resource.extraParams && (shareInfoToJs.extraInfo = JSON.parse(resource.extraParams));
            resource.useDefault && (shareInfoToJs.useDefault = resource.useDefault);

            if (resource.type == "video") {
                resource.videoShareTemplateId && (shareInfoToJs.shareTemplateId = resource.videoShareTemplateId);
                resource.shareDefaultBgm && (shareInfoToJs.shareDefaultBgm = resource.shareDefaultBgm);
                resource.shareCutTemplateId && (shareInfoToJs.shareCutTemplateId = resource.shareCutTemplateId);
                resource.shareVideoPath && (shareInfoToJs.shareVideoPath = resource.shareVideoPath);
                resource.videoContent && (shareInfoToJs.shareContent = resource.videoContent);
                resource.shareTopic && (shareInfoToJs.shareTopic = resource.shareTopic);
            } else {
                resource.shareTemplateId && (shareInfoToJs.shareTemplateId = resource.shareTemplateId);
                resource.content && (shareInfoToJs.shareContent = resource.content);
                resource.title && (shareInfoToJs.sharePointTitle = resource.title);
                resource.imageUrl && (shareInfoToJs.sharePicLocUrl = resource.imageUrl);
            }

            console.log("shareInfoToJs: " + JSON.stringify(shareInfoToJs));
            GameGlobal.TuyooSdk.Share(shareInfoToJs, 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);
        } catch (error) {
            console.error("GetDeviceLevelToBridge catch error: " + error);
        }
    },

    CreatGridGamePanelToBridge: function(gridGameModule) {
        var self = this;
        try {
            var gridGameModuleFromCS = UTF8ToString(gridGameModule);
            var gridGameInfo = JSON.parse(gridGameModuleFromCS);
            var gridGameInfoToJs = gridGameInfo;
            var callbackKey = gridGameInfo.gridGamePanelId;
            !gridGameInfoToJs.position && (delete gridGameInfoToJs.position)
            !gridGameInfoToJs.size && (delete gridGameInfoToJs.size)
            !gridGameInfo.query && (gridGameInfoToJs.query = {})
            var callback = function (code, result, error) {
                if(code == 0){
                    !GameGlobal._tyGridGamePanelJson && (GameGlobal._tyGridGamePanelJson = {})
                    GameGlobal._tyGridGamePanelJson[callbackKey] = result;
                }
                if(code != 900508){
                    result = {};
                }
                var action = callbackKey;
                var callbackString = JSON.stringify({ code, result, error, action});
                SendMessage("NativeCallBack", "UnifiedNativeCallback", callbackString);
            }
            GameGlobal.TuyooSdk.CallMethod("CreateGamePanel", gridGameInfoToJs, callback);
        } catch (e) {
            console.error("CreatGridGamePanelToBridge catch error: " + e);
        }
    },

    ShowGridGamePanelToBridge: function(panelId) {
        try {
            !GameGlobal._tyGridGamePanelJson && (GameGlobal._tyGridGamePanelJson = {})
            panelId = UTF8ToString(panelId);
            var gridGamePanel = GameGlobal._tyGridGamePanelJson[panelId];
            if(gridGamePanel){
                gridGamePanel.show();
            }
        } catch (e) {
            console.error("ShowGridGamePanelToBridge catch error: " + e);
        }
    },

    HideGridGamePanelToBridge: function(panelId) {
        try {
            !GameGlobal._tyGridGamePanelJson && (GameGlobal._tyGridGamePanelJson = {})
            panelId = UTF8ToString(panelId);
            var gridGamePanel = GameGlobal._tyGridGamePanelJson[panelId];
            if(gridGamePanel){
                gridGamePanel.hide();
            }
        } catch (e) {
            console.error("HideGridGamePanelToBridge catch error: " + e);
        }
    },

    DestroyGridGamePanelToBridge: function(panelId) {
        try {
            !GameGlobal._tyGridGamePanelJson && (GameGlobal._tyGridGamePanelJson = {})
            panelId = UTF8ToString(panelId);
            var gridGamePanel = GameGlobal._tyGridGamePanelJson[panelId];
            if(gridGamePanel){
                gridGamePanel.destroy();
            }
        } catch (e) {
            console.error("DestroyGridGamePanelToBridge catch error: " + e);
        }
    },

    MicroPaymentToBridge: 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));
            GameGlobal.TuyooSdk.MicroPayment(tyPayParams, payCallback, this);
        } catch (e) {
            console.error("PayToBridge catch error: " + e);
        }
    },
}

mergeInto(LibraryManager.library, TuyooSdkBridge);