
var TuyooSdkBridge = {

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

    InitToBridge: function (initEventInfo) {
        try {
            var initInfoFromCS = UTF8ToString(initEventInfo);
            var initInfo = JSON.parse(initInfoFromCS);
            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.wxPayVersion = "2.0";
            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;
            if (initInfo.login.tywx.wxShareEnable === false) {
                GameGlobal.tywx.StateInfo.wxShareEnable = false;
                wx.hideShareMenu({
                    menus: ['shareAppMessage', 'shareTimeline']
                });
            }
            // 默认关闭拉取营销系统分享配置开关
            GameGlobal.tywx.StateInfo.pullShareConfig = false;
            if (initInfo.login.tywx.pullShareConfig === true) {
                GameGlobal.tywx.StateInfo.pullShareConfig = true;
            }
            // 默认关闭交叉导流功能
            GameGlobal.tywx.StateInfo.adManagerMode = false;
            if (initInfo.login.tywx.adManagerMode === true) {
                GameGlobal.tywx.StateInfo.adManagerMode = true;
            }
            // 广点通上报，默认开启
            if (initInfo.login.tywx.txAdEventReportMode === false) {
                GameGlobal.tywx.StateInfo.txAdEventReportMode = false;
            }
            // 错误日志上报，默认关闭
            if (initInfo.login.tywx.errorLogMode === true) {
                GameGlobal.tywx.StateInfo.errorLogMode = false;
            }

            if (initInfo.login.tywx.shareManagerUrl) {
                GameGlobal.tywx.SystemInfo.shareManagerUrl = initInfo.login.tywx.shareManagerUrl.endsWith("/") ? initInfo.login.tywx.shareManagerUrl : initInfo.login.tywx.shareManagerUrl + "/";
            }
            if (initInfo.login.tywx.txAdSetId) {
                GameGlobal.tywx.SystemInfo.txAdSetId = initInfo.login.tywx.txAdSetId;
            }
            if (initInfo.login.tywx.txAdSetKey) {
                GameGlobal.tywx.SystemInfo.txAdSetKey = initInfo.login.tywx.txAdSetKey;
            }
            if (initInfo.login.tywx.openFeatureFilter === true) {
                GameGlobal.tywx.SystemInfo.openFeatureFilter = true;
            }

            var launchInfo = wx.getLaunchOptionsSync();
            GameGlobal.tywx && GameGlobal.tywx.wxOnshowAction && GameGlobal.tywx.wxOnshowAction(launchInfo);
            wx.onShow(function (result) {
                GameGlobal.tywx && GameGlobal.tywx.wxOnshowAction && GameGlobal.tywx.wxOnshowAction(result);
            })
            var callbackResult = {
                code: 0,
                result: "wx init completed!",
                error: ""
            };
            var callbackString = JSON.stringify(callbackResult);
            GameGlobal.Module.SendMessage("NativeCallBack", "InitResult", callbackString);
        }
        catch (e) {
            console.error("InitToBridge catch error: " + e);
        }
    },

    // 切换SDK域名
    UpdateServerUrlToBridge: function (serverUrl) {
        try {
            var serverUrlFromCS = UTF8ToString(serverUrl);
            GameGlobal.tywx.SystemInfo.loginUrl = serverUrlFromCS.endsWith("/") ? serverUrlFromCS : serverUrlFromCS + "/";
            console.log("update serverUrl to: " + GameGlobal.tywx.SystemInfo.loginUrl);
        } catch (e) {
            console.error("UpdateServerUrlToBridge catch error: " + e);
        }
    },

    // 登录
    LoginToBridge: function (loginEventInfo) {
        try {
            var loginCallback = function (code, result, error) {
                var callbackString = JSON.stringify({ code, result, error });
                GameGlobal.Module.SendMessage("NativeCallBack", "LoginResult", callbackString);
            }
            GameGlobal.tywx.TuyooSDK.login(loginCallback);
        } 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 });
                GameGlobal.Module.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;
            }
            tyPayParams.prodId = payInfo.productId;
            tyPayParams.prodName = payInfo.productName;
            tyPayParams.prodPrice = payInfo.productPrice;
            tyPayParams.sdk_game_role_id = payInfo.gameRoleId;
            tyPayParams.sdk_game_zone_id = payInfo.gameZoneId;
            if (payInfo.extraParams) {
                var extraParams = JSON.parse(payInfo.extraParams);
                if (extraParams.actId) {
                    tyPayParams.actId = extraParams.actId;
                    delete extraParams.actId;
                }
                if (extraParams.levelList) {
                    tyPayParams.levelList = JSON.parse(extraParams.levelList);
                    delete extraParams.levelList;
                }
                if (extraParams.sendMessageTitle) {
                    tyPayParams.sendMessageTitle = extraParams.sendMessageTitle;
                    delete extraParams.sendMessageTitle;
                }
                if (extraParams.sendMessagePath) {
                    tyPayParams.sendMessagePath = extraParams.sendMessagePath;
                    delete extraParams.sendMessagePath;
                }
                if (extraParams.sendMessageImg) {
                    tyPayParams.sendMessageImg = extraParams.sendMessageImg;
                    delete extraParams.sendMessageImg;
                }
                tyPayParams.extraParams = extraParams;
            }
            console.log("tyPayParams: " + JSON.stringify(tyPayParams));
            GameGlobal.tywx.TYPayInterface.pay(tyPayParams, payCallback);
        } catch (e) {
            console.error("PayToBridge catch error: " + e);
        }
    },

    GAEventTrackToBridge: function (reportEventInfo) {
        try {
            var reportInfoFromCS = UTF8ToString(reportEventInfo);
            var reportInfo = JSON.parse(reportInfoFromCS);
            if (!reportInfo.typeName) {
                reportInfo.typeName = "track";
            }
            GameGlobal.tywx.GALog.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.tywx.GALog.profileTrack(reportInfo.profileType, reportInfo.profileSubjectType, reportInfo.profileSubjectPrimary, reportInfo.reportEvent);
        } catch (e) {
            console.error("GAProfileTrackToBridge catch error: " + e);
        }
    },

    GetShareInfoBridge: function (shareEventInfo) {
        try {
            var shareInfoFromCS = UTF8ToString(shareEventInfo);
            var shareInfo = JSON.parse(shareInfoFromCS);
            var resource = shareInfo.resource;
            var shareCallback = function (code, result, error) {
                if(code == 0 && result){
                    result.sharePoint = resource.sharePoint;
                }
                var callbackString = JSON.stringify({ code, result, error });
                SendMessage("NativeCallBack", "GetShareInfoResult", callbackString);
            }
            GameGlobal.tywx.ShareInterface.GetServerShareConfig(resource.type || 'wx_normal', resource.sharePoint, shareCallback);
        } catch (error) {
            console.error("GetShareInfoBridge catch error: " + error);
        }
    },

    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 extraData = shareInfo.extra;
            var shareInfoToJs = {};
            resource.title && (shareInfoToJs.shareContent = resource.title);
            resource.imageUrl && (shareInfoToJs.sharePicUrl = resource.imageUrl);
            resource.shareSchemeId && (shareInfoToJs.shareSchemeId = resource.shareSchemeId);
            resource.extraParams && (shareInfoToJs.extraInfo = resource.extraParams);
            resource.sharePointId && (shareInfoToJs.sharePointId = resource.sharePointId);
            resource.sharePoint && (shareInfoToJs.sharePoint = resource.sharePoint);
            if(extraData){
                Object.assign(shareInfoToJs, extraData);
            }
            console.log("shareInfoToJs: " + JSON.stringify(shareInfoToJs));
            GameGlobal.tywx.ShareInterface.setOnShareAppMessageInfo(shareInfoToJs.sharePoint, shareInfoToJs, shareCallback);
        } 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 });
                GameGlobal.Module.SendMessage("NativeCallBack", "ShareResult", callbackString);
            }
            var shareInfoFromCS = UTF8ToString(shareEventInfo);
            var shareInfo = JSON.parse(shareInfoFromCS);
            var resource = shareInfo.resource;
            var extraData = resource.extra;
            var shareInfoToJs = {};
            resource.title && (shareInfoToJs.shareContent = resource.title);
            resource.imageUrl && (shareInfoToJs.sharePicUrl = resource.imageUrl);
            resource.shareSchemeId && (shareInfoToJs.shareSchemeId = resource.shareSchemeId);
            resource.extraParams && (shareInfoToJs.extraInfo = resource.extraParams);
            resource.sharePointId && (shareInfoToJs.sharePointId = resource.sharePointId);
            resource.sharePoint && (shareInfoToJs.sharePoint = resource.sharePoint);
            if(extraData){
                Object.assign(shareInfoToJs, extraData);
            }
            console.log("shareInfoToJs: " + JSON.stringify(shareInfoToJs));
            if(resource.type == 'wx_activity_not_chat'){
                GameGlobal.tywx.ShareInterface.activityShare(shareInfoToJs.sharePoint, shareInfoToJs, shareCallback);
            }else if(resource.type == 'wx_poster'){
                GameGlobal.tywx.ShareInterface.posterShare(shareInfoToJs.sharePoint, shareInfoToJs, shareCallback);
            }else{
                GameGlobal.tywx.ShareInterface.share(shareInfoToJs.sharePoint, shareInfoToJs, shareCallback);
            }
        } catch (e) {
            console.error("ShareToBridge catch error: " + e);
        }
    },

    GetAdInfoBridge: function () {
        try {
            var iconCallback = function (result) {
                var callbackResult = {
                    code: 0,
                    result: result,
                };
                var callbackString = JSON.stringify(callbackResult);
                GameGlobal.Module.SendMessage("NativeCallBack", "GetIconInfoResult", callbackString);
            }
            var newIconCallback = function (result) {
                var callbackResult = {
                    code: 0,
                    result: result,
                };
                var callbackString = JSON.stringify(callbackResult);
                GameGlobal.Module.SendMessage("NativeCallBack", "GetNewIconInfoResult", callbackString);
            }
            GameGlobal.tywx.NotificationCenter.listen(tywx.EventType.GET_ADMANAGER_ICON_INFO_SUCCESS, iconCallback);
            GameGlobal.tywx.NotificationCenter.listen(tywx.EventType.GET_ADMANAGER_NEWICON_INFO_SUCCESS, newIconCallback);
            GameGlobal.tywx.AdManager.init();
        } catch (error) {
            console.error("ShareToBridge catch error: " + error);
        }
    },

    TxAdSDKReportToBridge: function (txAdReportInfo) {
        try {
            var reportInfoFromCS = UTF8ToString(txAdReportInfo);
            var reportInfo = JSON.parse(reportInfoFromCS);
            console.log("reportInfoToJs: " + JSON.stringify(reportInfo.actionParams));
            GameGlobal.tywx.TxAdSDKReportInterface.track(reportInfo.actionType, JSON.parse(reportInfo.actionParams));
        } catch (error) {
            console.error("TxAdSDKReportToBridge catch error: " + error);
        }
    },

    CheckBindToBridge: function (bindEventInfo) {
        try {
            var bindInfoFromCS = UTF8ToString(bindEventInfo);
            var bindInfo = JSON.parse(bindInfoFromCS);
            var checkBindCallback = function (code, result) {
                var finnalResult = {
                    code: code,
                };
                if (code == -1) {
                    finnalResult.error = result;
                } else {
                    finnalResult.result = result;
                }
                var callbackString = JSON.stringify(finnalResult);
                GameGlobal.Module.SendMessage("NativeCallBack", "CheckResult", callbackString);
            }
            GameGlobal.tywx.TuyooSDK.CheckBind(bindInfo.userId, checkBindCallback);
        } catch (error) {
            console.error("checkBindToBridge catch error: " + error);
        }
    },

    SendSmsCodeToBridge: function (loginEventInfo) {
        try {
            var loginInfoFromCS = UTF8ToString(loginEventInfo);
            var phoneInfo = JSON.parse(loginInfoFromCS);
            var sendSmsCodeCallback = function (code, result) {
                var finnalResult = {
                    code: code,
                };
                if (code == 0) {
                    finnalResult.result = result;
                } else {
                    finnalResult.error = result;
                }
                var callbackString = JSON.stringify(finnalResult);
                GameGlobal.Module.SendMessage("NativeCallBack", "SendCodeResult", callbackString);
            }
            GameGlobal.tywx.TuyooSDK.ThirdSmsSend(phoneInfo.phoneNumber, sendSmsCodeCallback);
        } catch (error) {
            console.error("SendSmsCodeToBridge catch error: " + error);
        }
    },

    BindToBridge: function (bindEventInfo) {
        try {
            var bindInfoFromCS = UTF8ToString(bindEventInfo);
            var bindInfo = JSON.parse(bindInfoFromCS);
            var doBindCallback = function (code, result) {
                var finnalResult = {
                    code: code,
                };
                if (code == 0) {
                    finnalResult.result = result;
                } else {
                    finnalResult.error = result;
                }
                var callbackString = JSON.stringify(finnalResult);
                GameGlobal.Module.SendMessage("NativeCallBack", "onBindSucc", callbackString);
            }
            GameGlobal.tywx.TuyooSDK.ThirdBindSns(bindInfo.phoneNumber, bindInfo.phoneCode, bindInfo.userId, bindInfo.password, doBindCallback);
        } catch (error) {
            console.error("BindToBridge catch error: " + error);
        }
    },

    SetWxUserInfoToBridge: function (type, userInfo) {
        try {
            var userInfoFromCS = UTF8ToString(userInfo);
            var currentUserInfo = JSON.parse(userInfoFromCS);
            var reportInfo = currentUserInfo.wxUserInfo;
            var currentType = UTF8ToString(type);
            var succCb = function () {
                var finnalResult = {
                    code: 0,
                    result: "success!"
                };
                var callbackString = JSON.stringify(finnalResult);
                GameGlobal.Module.SendMessage("NativeCallBack", "SetWxInfoResult", callbackString);
            }
            var failCb = function () {
                var finnalResult = {
                    code: -1,
                    error: "fail!"
                };
                var callbackString = JSON.stringify(finnalResult);
                GameGlobal.Module.SendMessage("NativeCallBack", "SetWxInfoResult", callbackString);
            }
            if (currentType == "1") {
                GameGlobal.tywx.NotificationCenter.listen(tywx.EventType.SET_WX_USER_INFO_SUCCESS, succCb);
                GameGlobal.tywx.NotificationCenter.listen(tywx.EventType.SET_WX_USER_INFO_FAIL, failCb);
                GameGlobal.tywx.TuyooSDK.setWxUserInfo(reportInfo);
            } else if (currentType == "2") {
                GameGlobal.tywx.NotificationCenter.listen(tywx.EventType.SET_WX_SENSITIVE_DATA_SUCCESS, succCb);
                GameGlobal.tywx.NotificationCenter.listen(tywx.EventType.SET_WX_SENSITIVE_DATA_FAIL, failCb);
                GameGlobal.tywx.TuyooSDK.setWxEncryptedData(reportInfo.encryptedData, reportInfo.iv, reportInfo.setattr);
            } else if (currentType == "3") {
                GameGlobal.tywx.NotificationCenter.listen(tywx.EventType.SET_WX_SENSITIVE_DATA_SUCCESS, succCb);
                GameGlobal.tywx.NotificationCenter.listen(tywx.EventType.SET_WX_SENSITIVE_DATA_SUCCESS, failCb);
                GameGlobal.tywx.TuyooSDK.setThreeWxEncryptedData(reportInfo.wxOpenId, reportInfo.unionId, reportInfo.session_key);
            }
        } catch (error) {
            console.error("SetWxUserInfoToBridge catch error: " + error);
        }
    },

    GetDeviceLevelToBridge: function() {
        try {
            var callback = function (code, result, error) {
                var callbackString = JSON.stringify({ code, result, error });
                GameGlobal.Module.SendMessage("NativeCallBack", "GetDeviceLevelResult", callbackString);
            }
            GameGlobal.tywx.TuyooSDK.getDeviceBenchmarkLevel(callback);
        } 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");
    },

    RequestUserRiskRankToBridge: function() {
        try {
            var callback = function (code, result, error) {
                var callbackString = JSON.stringify({ code, result, error });
                GameGlobal.Module.SendMessage("NativeCallBack", "UserRiskRankResult", callbackString);
            }
            GameGlobal.tywx.TuyooSDK.getUserRiskRank(callback);
        } catch (error) {
            console.error("RequestUserRiskRankToBridge catch error: " + error);
        }
    },

    RequestMchTransferToBridge: function(mchTransferEventInfo) {
        try {
            var callback = function (code, result, error) {
                var callbackString = JSON.stringify({ code, result, error });
                GameGlobal.Module.SendMessage("NativeCallBack", "MchTransferResult", callbackString);
            }
            var mchTransferInfoFromCS = UTF8ToString(mchTransferEventInfo);
            var mchTransferInfo = JSON.parse(mchTransferInfoFromCS);
            console.log("mchTransferInfoToJs: " + JSON.stringify(mchTransferInfo));
            GameGlobal.tywx.TYPayInterface.requestMerchantTransfer(mchTransferInfo, callback);
        } catch (error) {
            console.error("RequestUserRiskRankToBridge catch error: " + error);
        }
    },

}


mergeInto(LibraryManager.library, TuyooSdkBridge);