var util = require('./util');
import {
  BLE
} from "./btls/ble";
const app = getApp();
const emitter = app.globalData.emitter

function ab2hex(buffer) {
  var hexArr = Array.prototype.map.call(
    new Uint8Array(buffer),
    function (bit) {
      return ('00' + bit.toString(16)).slice(-2)
    }
  )
  return hexArr.join(',');
}

function startFp1_63(managerTelephone, managerPassword, userTelephone, userPassword, password, startTime, endTime,
  num,timestamp,callback) {
  wx.showLoading({
    title: "正在发送...",
    mask:true
  });
  setTimeout(() => {
    wx.hideLoading()
  }, 1000);

  let cmd = 0x63
  let mt = util.stringToUint8Array(managerTelephone)
  let mp = util.stringToUint8Array(managerPassword)
  let ut = util.stringToUint8Array(userTelephone)
  let up = util.stringToUint8Array(userPassword)

  let pd = util.hexStringToBytesWithPadding(password, 10, 0xff)

  let ust = util.time40ToBytes(startTime)
  let uet = util.time40ToBytes(endTime)
  let fst = util.time40ToBytes(startTime)
  let fet = util.time40ToBytes(endTime)
  let length = 1 + 11 + 6 + 11 + 6 + 5 + 5 + 4 + 2 + 10 + 5 + 5 + 1 + 2 + 1

  let b = new Uint8Array(length)

  b[0] = cmd
  b.set(mt, 1)
  b.set(mp, 12)
  b.set(ut, 18)
  b.set(up, 29)
  b.set(ust, 35)
  b.set(uet, 40)

  //权限
  b[45] = 0xff;
  b[46] = 0xff;
  b[47] = 0xff;
  b[48] = 0xff;

  //密码长度 uint16
  b[49] = 0
  b[50] = 0x0a
  //密码数据
  b.set(pd, 51)

  b.set(fst, 61)
  b.set(fet, 66)

  b[71] = 0xff;

  b[72] = 0xff;
  b[73] = 0xff;

  b[74] = num; //添加指纹
  app.globalData.ble.send(cmd, b,timestamp,function(data){
    callback(data)
  })
}
function startFp_26(managerTelephone, managerPassword,timestamp,callback) {
  wx.showLoading({
    title: "正在发送...",
    mask:true
  });
  setTimeout(() => {
    wx.hideLoading()
  }, 1000);

  let cmd = 0x26
  let mt = util.stringToUint8Array(managerTelephone)
  let mp = util.stringToUint8Array(managerPassword)

  let length = 1 + 11 + 6

  let b = new Uint8Array(length)

  b[0] = cmd
  b.set(mt, 1)
  b.set(mp, 12)
  app.globalData.ble.send(cmd, b,timestamp,function(data){
    callback(data)
  })
}

function startFp_23(deviceNum, managerTelephone, managerPassword, newPassword, key,timestamp,callback) {
  wx.showLoading({
    title: "正在发送...",
    mask:true
  });
  setTimeout(() => {
    wx.hideLoading()
  }, 1000);

  let cmd = 0x23
  let dn = util.stringToBytesWithPadding(deviceNum, 20, 255)
  let mt = util.stringToUint8Array(managerTelephone)
  let mp = util.stringToUint8Array(managerPassword)
  let ut = util.stringToUint8Array(newPassword)
  let up = util.stringToUint8Array(key)

  let length = 1 + 20 + 11 + 6 + 6 + 10

  let b = new Uint8Array(length)

  b[0] = cmd
  b.set(dn, 1)
  b.set(mt, 21)
  b.set(mp, 32)
  b.set(ut, 38)
  b.set(up, 44)
  app.globalData.ble.send(cmd, b,timestamp,function(data){
    callback(data)
  })
}

function openLock(userTelephone,userPassword,pwd,openLockType,timestamp,callback) {
  wx.showLoading({
    title: "正在发送...",
    mask:true
  });
  setTimeout(()=>{
    wx.hideLoading()
  },3000)
  let cmd = 0x41
  let ut = util.stringToUint8Array(userTelephone)

  let up = util.stringToUint8Array(userPassword)

  let p = util.hexStringToBytesWithPadding(pwd, 10, 0xff)
  let length = 1 + 11 + 6 + 10 + 1

  let b = new Uint8Array(length)

  b[0] = cmd
  b.set(ut, 1)
  b.set(up, 12)
  b.set(p, 18)
  b[28] = openLockType
  app.globalData.ble.send(cmd, b, length,timestamp,function(data){
    callback(data)
  })
}

function startFp_29(deviceNum, status,timestamp,callback) {
  wx.showLoading({
    title: "正在发送...",
    mask:true
  });

  let cmd = 0x29
  let dn = util.stringToUint8ArrayWithPadding(deviceNum, 20, 0xff)

  let length = 1 + 20 + 5 + 1 + 1

  let b = new Uint8Array(length)

  b[0] = cmd
  b.set(dn, 1)

  //send data=0x0036000008 
  b[21] = 0x00;
  b[22] = 0x36;
  b[23] = 0;
  b[24] = 0;
  b[25] = 0x08

  b[26] = status //status: 0x03, 0x02, 0x01
  b[27] = 0x01
  app.globalData.ble.send(cmd, b,timestamp,function(data){
    callback(data)
  })
}
function startFp_63(managerTelephone, managerPassword, userTelephone, userPassword, startTime, endTime, num,fingerprintId,timestamp,callback) {
  wx.showLoading({
    title: "正在发送...",
    mask:true
  });
  let cmd = 0x63
  let mt = util.stringToUint8Array(managerTelephone)
  let mp = util.stringToUint8Array(managerPassword)
  let ut = util.stringToUint8Array(userTelephone)
  let up = util.stringToUint8Array(userPassword)

  let ust = util.time40ToBytes(startTime)
  let uet = util.time40ToBytes(endTime)
  let fst = util.time40ToBytes(startTime)
  let fet = util.time40ToBytes(endTime)
  let length = 1 + 11 + 6 + 11 + 6 + 5 + 5 + 4 + 2 + 10 + 5 + 5 + 1 + 2 + 1

  let b = new Uint8Array(length)

  b[0] = cmd
  b.set(mt, 1)
  b.set(mp, 12)
  b.set(ut, 18)
  b.set(up, 29)
  b.set(ust, 35)
  b.set(uet, 40)

  //权限
  b[45] = 0x46;
  b[46] = 0x46;
  b[47] = 0x46;
  b[48] = 0x46;

  //密码长度 uint16
  b[49] = 0
  b[50] = 0x0a
  //密码数据
  console.log(fingerprintId, '指纹ID')
  b[51] = fingerprintId
  b[52] = 0xff
  b[53] = 0xff
  b[54] = 0xff
  b[55] = 0xff
  b[56] = 0xff
  b[57] = 0xff
  b[58] = 0xff
  b[59] = 0xff
  b[60] = 0xff

  b.set(fst, 61)
  b.set(fet, 66)

  b[71] = 0xff;

  b[72] = 0xff;
  b[73] = 0xff;

  b[74] = num; //添加指纹
  app.globalData.ble.send(cmd, b,timestamp,function(data){
    callback(data)
  })
}
module.exports = {
  startFp1_63,startFp_26,startFp_23,openLock,startFp_29,startFp_63
}