// 签名方法：signAuth（同步方法）
// 获取用户信息：getUserInfo（同步方法）
// 扫描二维码：scanCode
// 打开企业联系人详情页面：openCompanyUserInfo 参数：联系人地址，企业id

import router from '@/router'
// import { AcceptJoinDTO } from '@/service/moudles/service.dto'
import dsbridge from 'dsbridge'

export enum RoleType{
  LEADER = 0,
  SUPER_ADMIN = 1,
  CUSTOMER_ADMIN = 2,
  COMMON_MEMBER = 3,
  CHILD_ADMIN = 4,
}

export interface GETUSERINFO{
  company: any,
  depId: string,
  entId: string,
  id: string,
  joinTime: number,
  name: string,
  phone: string,
  position: string,
  role: RoleType
}

enum BridgeMethods {
  SIGN_AUTH = 'signAuth',
  GET_USERINFO = 'getUserInfo',
  SCAN_CODE = 'scanCode',
  OPEN_COMPANY_USER_INFO = 'openCompanyUserInfo',
  Back = 'back',
  GEN_JOIN_FORM = 'genJoinForm',
  SIGN = 'sign',
  GET_PUBLIC_KEY = 'getPublicKey',
  REFRESH_COMPANY_STATE = 'refreshCompanyState',
  Close = 'close'
}

export function isAppEnv() {
  const env = window.navigator.userAgent || navigator.userAgent
  console.log(env, env.includes(';FZM-3SYXIN;'), 'appEnv')
  if (env.includes(';FZM-3SYXIN;')) {
    return true
  } else {
    return false
  }
}

export function appNavBack() {
  const appEnv = isAppEnv()
  if (appEnv) {
    goBack()
  } else {
    router.go(-1)
  }
}

/**
 * 获取用户的资料
 * @returns
 */
export function getUserInfo():any {
  const data = dsbridge.call(BridgeMethods.GET_USERINFO, {}, (res) => {
    return res
  })
  return data
}

/**
 * 获取签名
 * @returns
 */
export function signAuth() {
  const data = dsbridge.call(BridgeMethods.SIGN_AUTH, {}, (res) => {
    return res
  })
  return data
}

/**
 * 调用相机进行扫码
 * @returns
 */
export function scanCode() {
  const data = dsbridge.call(BridgeMethods.SCAN_CODE, {}, (res) => {
    return res
  })
  return data
}

/**
 * 用于导航
 * @returns
 */
export function goBack() {
  const data = dsbridge.call(BridgeMethods.Back, {}, (res) => {
    return res
  })
  return data
}

/**
 * 用于导航
 * @returns
 */
export function close() {
  const data = dsbridge.call(BridgeMethods.Close, {}, (res) => {
    return res
  })
  return data
}

/**
 * 企业联系人详情页面
 * @returns
 */
export function openCompanyUserInfo(address: string) {
  const data = dsbridge.call(BridgeMethods.OPEN_COMPANY_USER_INFO, {
    address
  }, (res) => {
    return res
  })
  return data
}

export function refreshCompanyState() {
  const data = dsbridge.call(BridgeMethods.REFRESH_COMPANY_STATE, {}, res => {
    return res
  })
  return data
}

/**
 * 接受邀请验签hash
 * @returns
 */
export function getSign(obj: Object) {
  const data = dsbridge.call(BridgeMethods.SIGN, {
    ...obj
  }, (res) => {
    return res
  })
  return data
}

export function getPublicKey() {
  const data = dsbridge.call(BridgeMethods.GET_PUBLIC_KEY, {}, res => {
    console.log(res)
    return res
  })
  return data
}
// /**
//  * 提交
//  */

// export function genJoinForm(data: ){
//     let req = dsbridge.register(BridgeMethods.GEN_JOIN_FORM,()=>{
//         return data
//     })
//     return req
// }
export default {
  getUserInfo,
  signAuth,
  scanCode,
  openCompanyUserInfo,
  goBack,
  close
}
