const config = {
  accessToken: "SEoqDFTNcaZOCt5fzuhzr0BWmJswebs8", // 项目通行证,在:网站后台-->设置-->应用列表中找到Access Token列 复制(首次使用可能需要先新增应用)
  clientId: "your_client_id", // 用户唯一标识,如产品为小游戏,则必须填用户openid(注意,不是小游戏的APPID!!!)
  autoTrack: {
    appLaunch: true, // 自动采集 $MPLaunch
    appShow: true, // 自动采集 $MPShow
    appHide: true, // 自动采集 $MPHide
  },
  sendTimeout: 3000, // 网络请求超时时间,单位毫秒,默认值 3000 ms
  maxRetries: 3, // 网络请求失败时的重试次数,1 表示不重试。默认值是 3
  enablePersistence: true, // 是否使用本地缓存,主实例默认为 true,子实例默认为 false
  asyncPersistence: false, // 是否使用异步存储,默认为 false
  name: "ge", // 全局变量名称
  // debugMode: "debug", // 是否开启测试模式,开启测试模式后,可以在 网站后台--设置--元数据--事件流中查看实时数据上报结果。(测试时使用,上线之后一定要关掉,改成none或者删除)
};
export default class GEMgr {

  private static nameInit = "zyzy";
  private static versionInit = 1;

  private static isInit = false;
  private static ge;

  public static GESetup(openidInit) {
    console.log("ge setup:",openidInit)
  }

  public static async GEInit(openidInit) {
    if (openidInit == null || openidInit =="") {
      console.log("openidInit openid is null");
      return;
    }
    
    console.log("ge init:",openidInit)
    config.clientId = openidInit;
    this.ge = new GravityAnalyticsAPI(config);
    this.ge.setupAndStart();

    this.ge.initialize({
      name: this.nameInit,
      version: this.versionInit,
      openid: openidInit,
      enable_sync_attribution: false,
    })
      .then((res) => {
        //console.log("initialize success " + res);
        this.isInit = true;
      })
      .catch((err) => {
        console.log("initialize failed, error is " + err);
      });
  }

  public static async GEShowAD(adID) {
    if (!this.isInit) {
      ZYSDK.ZYSDK.getUserId().then((uid)=>{
        this.GEInit(uid)
      })
      return;
    }
    this.ge.adShowEvent("reward", adID, { custom_param: "" });
  }

  public static async GEReportEvent(msg) {
    let isAdNum = 0;
    let moduleStr = "";
    let actionStr = "";
    let isZt = 0;
    moduleStr = msg.split("-")[0];
    actionStr = msg.substring(moduleStr.length+1);
    if (msg.indexOf("AD") > -1){
      isAdNum = 1;
    }

    console.log("GEReportEvent:",isAdNum,moduleStr,actionStr);
    if (!this.isInit) {
      ZYSDK.ZYSDK.getUserId().then((uid)=>{
        this.GEInit(uid)
      })
      return;
    }
    isZt = ZYSDK.ZYSDK.getScenes();
    this.ge.track("userAction", {action:actionStr,module:moduleStr,isAD:isAdNum,isZT:isZt}, new Date());
  }
}