// H5 去调用App的方法
import jsBridge from "./bridge";

class bridgeToAppFun {
  constructor() {
    this.userAgent = null;
    console.log(navigator.userAgent);
    if (
      navigator.userAgent.indexOf("Android") > -1 ||
      navigator.userAgent.indexOf("Adr") > -1
    ) {
      this.userAgent = "android";
    } else if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
      this.userAgent = "ios";
    } else {
      this.userAgent = "web";
    }
  }
  // 获取Token
  getAuthToken() {
    console.log("系统:", this.userAgent);
    if (this.userAgent === "android") {
      // android
      let token = null;
      return new Promise((resolve, reject) => {
        console.log("版本", window.android.getAppVersion());
        console.log("接收", window.android.getToken());
        token = window.android.getToken();
        console.log("token", token);
        if (token) {
          resolve(token);
        } else {
          reject("没有token");
        }
      });
    } else if (this.userAgent === "ios") {
      // ios
      return new Promise((resolve, reject) => {
        console.log("调ios的方法");
        jsBridge.callhandler("getAuthToken", null, data => {
          if (data) {
            // localStorage.setItem("token", data);
            resolve(data);
          } else {
            console.log("调用失败");
            reject("getAuthToken", data);
          }
        });
      });
    }
  }
  // 退出
  logoutToApp() {
    console.log("退出登录");
    if (this.userAgent === "android") {
      console.log("an");
      window.android.logout();
    } else {
      console.log("ios");
      jsBridge.callhandler("logout");
    }
  }
  navigateBack() {
    console.log("返回");
    if (this.userAgent === "android") {
      try {
        // const home_url =
        //   "https://shop92680967.youzan.com/v2/showcase/homepage?alias=6aFsQ4vCp7";
        // location.href = home_url;
        window.android1.navigateBack();
      } catch {
        console.log("返回调用失败，都不行");
      }
    } else {
      console.log("去调用ios返回");
      jsBridge.callhandler("navigateBack");
    }
  }
  showBottomBar(params) {
    console.log("隐藏底部");
    if (this.userAgent === "android") {
      console.log("an");
      window.android.showBottomBar(params);
    } else {
      jsBridge.callhandler("showBottomBar", params);
    }
  }
  redirectToLogin() {
    console.log("去登陆");
    if (this.userAgent === "android") {
      console.log("an");
      window.android.redirectToLogin();
    } else {
      jsBridge.callhandler("redirectToLogin");
    }
  }
  // 保存图片
  saveImage(url) {
    console.log("调用App保存图片, 图片地址", url);
    if (this.userAgent === "android") {
      console.log("an");
      window.android.appSaveImage(url);
    } else {
      jsBridge.callhandler("appSaveImage", url, response => {
        if (response) {
          console.log("保存成功");
        }
      });
    }
  }
  getAppVersion() {
    let version = "";
    if (this.userAgent === "android") {
      console.log("an");
      version = window.android.getAppVersion();
    } else {
      jsBridge.callhandler("getAppVersion", null, response => {
        if (response) {
          version = response;
        }
      });
    }
    return version;
  }
}

export default bridgeToAppFun;
