export default function () {
  let dllPath = require('path').join(__static, 'donsee', 'est32.dll')

  if (process.env.NODE_ENV !== 'development') {
    dllPath = dllPath.replace('app.asar', 'app.asar.unpacked')
  }

  const fs = require('fs')
  const isExist = fs.existsSync(dllPath)
  const iconv = require('iconv-lite')
  console.log(isExist, dllPath)

  function formatResult (result) {
    for (let key in result) {
      result[key] = iconv
        .decode(Buffer.from(result[key]), 'gbk')
        .replace(/[\0,\s]/g, '')
    }
  }

  if (!isExist) return console.error('未找到dll文件')

  const ffi = require('ffi')
  const kernel32 = ffi.Library('kernel32', {
    SetDllDirectoryA: ['bool', ['string']]
  })

  kernel32.SetDllDirectoryA(require('path').join(__dirname, 'x86'))

  const dev = ffi.Library(dllPath, {
    EU_Reader_Open: ['int', ['string']],
    EU_Reader_Close: ['int', ['int']],
    EU_PosBeep: ['int', ['int', 'byte']],
    iReadCardBas_SAM: ['int', ['int', 'int', 'char*']],
    PEU_Reader_SICARD: [
      'int',
      [
        'int',
        'char*',
        'char*',
        'char*',
        'char*',
        'char*',
        'char*',
        'char*',
        'char*',
        'char*'
      ]
    ],
    GetCardType: ['int', []],
    PEU_Reader_ReadIDCard: ['int', ['int', 'char*']],

    GetName: ['int', ['char*']],
    GetSex: ['int', ['char*']],
    GetNation: ['int', ['char*']],
    GetBirth: ['int', ['char*']],
    GetCertNo: ['int', ['char*']],
    GetAddress: ['int', ['char*']],
    GetDepartemt: ['int', ['char*']],
    GetEffectDate: ['int', ['char*']],
    GetExpireDate: ['int', ['char*']],

    GetTXZHM: ['int', ['char*']],
    GetTXZQFCS: ['int', ['char*']],
    GetNationalityCode: ['int', ['char*']]
  })

  // 读取加密卡
  function readJmj () {
    let RHandle = -1
    let pCardInfo = Buffer.alloc(1024)

    RHandle = dev.EU_Reader_Open('USB1') // 打开设备
    if (RHandle < 0) console.error('设备未连接')

    let nRt = dev.iReadCardBas_SAM(RHandle, 3, pCardInfo)
    if (nRt !== 0) {
      console.error('读卡失败：' + pCardInfo.toString() + nRt)
      return
    }

    let result = iconv.decode(Buffer.from(pCardInfo), 'gbk').replace(/\0/g, '')

    console.log(result)

    dev.EU_Reader_Close(RHandle)
  }

  function readSiCard () {
    let RHandle = -1

    RHandle = dev.EU_Reader_Open('USB1') // 打开设备
    if (RHandle < 0) console.error('设备未连接')

    let pName = Buffer.alloc(100)
    let pSex = Buffer.alloc(100)
    let pNation = Buffer.alloc(100)
    let pBirth = Buffer.alloc(100)
    let pCertNo = Buffer.alloc(100)
    let pEffectData = Buffer.alloc(100)
    let pExpire = Buffer.alloc(100)
    let pCardNo = Buffer.alloc(9)
    let pErrMsg = Buffer.alloc(100)

    let nRt = dev.PEU_Reader_SICARD(
      RHandle,
      pCardNo,
      pName,
      pSex,
      pNation,
      pBirth,
      pCertNo,
      pEffectData,
      pExpire,
      pErrMsg
    )

    const result = {
      pCardNo,
      pName,
      pSex,
      pNation,
      pBirth,
      pCertNo,
      pEffectData,
      pExpire,
      pErrMsg
    }

    formatResult(result)

    if (nRt !== 0) {
      console.error('读社保卡失败：' + result.pErrMsg + nRt)
      return
    }
    result.typName = '社保卡'
    console.log(result)
    dev.EU_PosBeep(RHandle, 0x30)

    dev.EU_Reader_Close(RHandle)

    return result
  }

  function readSfz () {
    let RHandle = -1

    RHandle = dev.EU_Reader_Open('USB1') // 打开设备
    if (RHandle < 0) console.error('设备未连接')

    let pName = Buffer.alloc(100)
    let pSex = Buffer.alloc(100)
    let pNation = Buffer.alloc(100)
    let pBirth = Buffer.alloc(100)
    let pAddress = Buffer.alloc(100)
    let pCertNo = Buffer.alloc(100)
    let pDepartment = Buffer.alloc(100)
    let pEffectData = Buffer.alloc(100)
    let pExpire = Buffer.alloc(100)
    // let pData = Buffer.alloc(100);
    let pErrMsg = Buffer.alloc(100)

    let pTXZHM = Buffer.alloc(100)
    let pTXZQFCS = Buffer.alloc(100)

    let pEnName = Buffer.alloc(200)
    let pEnNation = Buffer.alloc(100)

    let nRt = dev.PEU_Reader_ReadIDCard(RHandle, pErrMsg)

    if (nRt === 0) {
    // textBox1
      let icardType = -1
      icardType = dev.GetCardType()
      let result = null
      if (icardType === 0) {
        dev.GetName(pName)
        dev.GetSex(pSex)
        dev.GetNation(pNation)
        dev.GetBirth(pBirth)
        dev.GetCertNo(pCertNo)
        dev.GetAddress(pAddress)
        dev.GetDepartemt(pDepartment)
        dev.GetEffectDate(pEffectData)
        dev.GetExpireDate(pExpire)

        result = {
          pName,
          pSex,
          pNation,
          pBirth,
          pCertNo,
          pAddress,
          pDepartment,
          pEffectData,
          pExpire
        }

        formatResult(result)

        result.typName = '居民身份证'
      }

      if (icardType === 2) {
        dev.GetName(pName)
        dev.GetSex(pSex)

        dev.GetBirth(pBirth)
        dev.GetCertNo(pCertNo)
        dev.GetAddress(pAddress)
        dev.GetDepartemt(pDepartment)
        dev.GetEffectDate(pEffectData)
        dev.GetExpireDate(pExpire)

        dev.GetTXZHM(pTXZHM)
        dev.GetTXZQFCS(pTXZQFCS)

        result = {
          pName,
          pSex,
          pBirth,
          pCertNo,
          pAddress,
          pDepartment,
          pEffectData,
          pExpire,

          pTXZHM,
          pTXZQFCS
        }

        formatResult(result)

        result.typName = '港澳台居民居住证'
      }

      if (icardType === 1) {
        dev.GetEnName(pEnName)
        dev.GetName(pName)
        dev.GetSex(pSex)
        dev.GetBirth(pBirth)
        dev.GetCertNo(pCertNo)
        dev.GetEffectDate(pEffectData)
        dev.GetExpireDate(pExpire)
        dev.GetNationalityCode(pEnNation)

        result = {
          pEnName,
          pName,
          pSex,
          pBirth,
          pCertNo,
          pEffectData,
          pExpire,
          pEnNation
        }

        formatResult(result)

        result.typName = '港澳台居民居住证'
      }

      dev.EU_PosBeep(RHandle, 0x30)
      dev.EU_Reader_Close(RHandle)

      console.log(result)

      return result
    } else {
      console.error('读身份证失败：' + nRt)
    }

    dev.EU_Reader_Close(RHandle)
  }

  return {
    readSiCard,
    readSfz,
    readJmj
  }
}
