import readCert from './readCert'
import readCard from './card'
import takePhoto from './takePhoto'
import sign from './sign'
import * as ktService from './ktService'

const service = {
  // 一体式读证
  readIntegratedCard() {
    return new Promise((resolve, reject) => {
      readCert.rcOpenCertDevice((json) => {
        if (json.err_code === 0) {
          const data = JSON.parse(json.data) || {}
          resolve({
            name: data.name,
            address: data.address,
            birthday: `${data.birthday.substr(0, 4)}-${data.birthday.substr(4, 2)}-${data.birthday.substr(6)}`,
            effectDate: `${data.effectDate.substr(0, 4)}-${data.effectDate.substr(4, 2)}-${data.effectDate.substr(6)}`,
            expireDate: data.expireDate.length === 8 ? `${data.expireDate.substr(0, 4)}-${data.expireDate.substr(4, 2)}-${data.expireDate.substr(6)}` : data.expireDate,
            gender: data.gender === '男' ? '1' : '2',
            idNum: data.idNum,
            issueOrg: data.issueOrg,
            nation: data.nation
          })
        } else if (json.err_code === -12104) {
          reject('请检查读证设备上是否存在居民身份证', json.err_code)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 分离式读证秘钥
  setAppParamEx(appKey, appSecret, password) {
    return new Promise((resolve, reject) => {
      readCert.rcSetAppParamEx(appKey, appSecret, password, (json) => {
        if (json.err_code === 0 || json.resultFlag === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 分离式读证
  readCardEx() {
    return new Promise((resolve, reject) => {
      readCert.rcReadCardEx(function(json) {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 获取设备
  listCard() {
    return new Promise((resolve, reject) => {
      readCard.rdListCard(function(json) {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  /**
   * 获取卡尔的读卡设备
   */
  async listKtCard() {
    try {
      return await ktService.listCard()
    } catch (error) {
      console.error(error)
      return []
    }
  },

  /**
   * 读取卡尔的身份证
   */
  async readKtCardEx() {
    const info = await ktService.readCard()
    return info
  },

  // 连接卡
  connectCard() {
    return new Promise((resolve, reject) => {
      let isSuccess = false
      readCard.rdConnectCard((cert) => {
        if (cert.err_code !== -290 && isSuccess !== true) {
          isSuccess = true
          resolve(cert)
        } else {
          readCard.rdListCard((cert) => {
            resolve(cert)
            readCard.rdConnectCard((cert) => {
              resolve(cert)
            })
          })
        }
      })
    })
  },

  // 发送APDU
  transmitCard(Apdu, CardName) {
    return new Promise((resolve, reject) => {
      readCard.rdTransmitCard(Apdu, CardName, (json) => {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 断开卡
  disconnectCard() {
    return new Promise((resolve, reject) => {
      readCard.rdDisconnectCard((json) => {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 获取视频设备信息
  getVideoInfo() {
    return new Promise((resolve, reject) => {
      takePhoto.tpGetVideoInfo((json) => {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          resolve([])
        }
      })
    })
  },

  // 打开视频
  masterOpenVideo(videoParams, cutStatus, callback) {
    takePhoto.tpMasterOpenVideo(videoParams, cutStatus, function(json) {
      callback(json)
    })
  },

  // 关闭视频
  closeVideo() {
    return new Promise((resolve, reject) => {
      takePhoto.tpCloseVideo((json) => {
        if (json.err_code === 0) {
          resolve(json.err_msg)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 主头拍照
  masterTakePhoto() {
    return new Promise((resolve, reject) => {
      takePhoto.tpMasterTakePhoto((json) => {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 副头拍照
  slaveTakePhoto() {
    return new Promise((resolve, reject) => {
      takePhoto.tpslaveTakePhoto((json) => {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 照片旋转角度
  roteAngle(angleValue) {
    takePhoto.tpRoteAngle(angleValue)
  },

  // 签字获取设备在线状态
  getSignStatus() {
    return new Promise((resolve, reject) => {
      sign.sGetSignStatus((json) => {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 打开签字
  signOpen(callback) {
    sign.sSignOpen(function(cert) {
      callback(cert)
    })
  },

  // 获取坐标
  getCoordinate(callback) {
    sign.sGetCoordinate(function(cert) {
      callback(cert)
    })
  },

  // 签字
  signFile() {
    return new Promise((resolve, reject) => {
      sign.sSignFile((json) => {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 重签
  resetSign() {
    return new Promise((resolve, reject) => {
      sign.sResetSign((json) => {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  },

  // 取消签名
  closeSign() {
    return new Promise((resolve, reject) => {
      sign.sCloseSign((json) => {
        if (json.err_code === 0) {
          resolve(json.data)
        } else {
          reject(json.err_msg, json.err_code)
        }
      })
    })
  }
}

export default service
