<template>
  <view class="box">
    <input placeholder="请输入密码" v-model="password" />
    <view :class="{ button: true, selected: operation === 'unlock' }" @click="unlock">开锁</view>
    <view :class="{ button: true, selected: operation === 'lock' }" @click="lock">关锁</view>
    <view :class="{ button: true, selected: operation === 'setKey' }" @click="setKey">设置密码</view>
    <view :class="{ button: true, selected: operation === 'writeCustomData' }" @click="writeCustomData">写自定义数据</view>
    <view :class="{ button: true, selected: operation === 'readCustomData' }" @click="readCustomData">读自定义数据</view>
    <view class="text">ID: {{ deviceId }}</view>
    <view class="text">RSSI: {{ deviceRssi }}</view>
    <view class="text">Status: {{ deviceStatus }}</view>
  </view>
</template>

<script setup>
  /**
   * 请在项目根目录下的 manifest.json 中加入以下权限
   * <uses-permission android:name="android.permission.NFC"/>
   * 
   * 请将项目的 minSdkVersion 改为 >= 28
   * 
   * 需要调试 NFC 功能请先打包一个自定义调试基座，不可使用标准基座进行调试
   */
  import { ref } from "vue"
  import { onShow, onHide, onUnload } from "@dcloudio/uni-app"

  import * as UTSNFCLock from "../../uni_modules/uts-nfclock"
  
  const operation = ref("")
  const password = ref("123456")
  const deviceId = ref("")
  const deviceRssi = ref("")
  const deviceStatus = ref("")

  const unlock = () => {
    operation.value = "unlock"
    UTSNFCLock.unlock(password.value)
  }

  const lock = () => {
    operation.value = "lock"
    UTSNFCLock.lock(password.value)
  }
  
  const setKey = () => {
    operation.value = "setKey"
    UTSNFCLock.setKey(password.value, "")
  }
  
  const writeCustomData = () => {
    const int8 = new Int8Array(56)
    for (let t = 0; t < 56; t++) {
      int8[t] = 166;
    }
    operation.value = "writeCustomData"
    UTSNFCLock.customDataOperation("WRITE", Array.from(int8))
  }
  
  const readCustomData = () => {
    operation.value = "readCustomData"
    UTSNFCLock.customDataOperation("READ", null)
  }

  onShow(() => {
    // 启用 NFC
    UTSNFCLock.enable()
    // 检测到 NFC 锁
    UTSNFCLock.onDiscoveredCb((id, isNew, rssi) => {
      deviceId.value = id
      deviceRssi.value = rssi
    })
    // 检测结束
    UTSNFCLock.onFinishedCb(() => {
      const systemInfo = uni.getSystemInfoSync()
      const platform = systemInfo.platform
      if (platform === "android") {
        deviceId.value = ""
        deviceRssi.value = ""
        deviceStatus.value = ""
        operation.value = ""
      }
    })
    // 充电回调
    UTSNFCLock.onChargeCb((res, level) => {
      switch (res) {
        case "OK": {
          deviceStatus.value = `充电中（${level}%）`
          break
        }
        case "UNAUTHORIZED": {
          deviceStatus.value = "验证失败"
          break
        }
        case "ERROR": {
          deviceStatus.value = "充电失败"
          break
        }
      }
    })
    // 开关锁回调
    UTSNFCLock.onControlCb((res) => {
      let keyword = "";
      if (operation.value === "unlock") {
        keyword = "开锁";
      } else if (operation.value === "lock") {
        keyword = "关锁";
      }
      switch (res) {
        case "OK": {
          deviceStatus.value = `${keyword}成功`
          break
        }
        case "UNAUTHORIZED": {
          deviceStatus.value = "验证失败"
          break
        }
        case "ERROR": {
          deviceStatus.value = `${keyword}失败`
          break
        }
      }
    })
    // 设置密码回调
    UTSNFCLock.onSetKeyCb((res) => {
      switch (res) {
        case "OK": {
          deviceStatus.value = "设置密码成功"
          break
        }
        case "UNAUTHORIZED": {
          deviceStatus.value = "验证失败"
          break
        }
        case "ERROR": {
          deviceStatus.value = "设置密码失败"
          break
        }
      }
    })
    // 读写自定义数据回调
    UTSNFCLock.onCustomDataCb((res, config, data) => {
      let keyword = "";
      if (operation.value === "writeCustomData") {
        keyword = "写数据";
      } else if (operation.value === "readCustomData") {
        keyword = "读数据";
      }
      switch (res) {
        case "OK": {
          deviceStatus.value = `${keyword}成功`
		  if (data) {
			  const int8 = new Int8Array(data)
			  console.log(int8)
		  }
          break
        }
        case "ERROR": {
          deviceStatus.value = `${keyword}失败`
          break
        }
      }
    })
  })
  
  onHide(() => {
    // 页面隐藏或销毁前必须关闭 NFC
    UTSNFCLock.disable()
  })
  
  onUnload(() => {
    // 页面隐藏或销毁前必须关闭 NFC
    UTSNFCLock.disable()
  })
</script>

<style lang="less" scoped>
  .box {
    height: 100%;
    padding: 20rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    
    input {
      width: 100%;
      height: 100rpx;
      padding: 0rpx 20rpx;
      margin: 20rpx 0rpx;
      border: 2rpx solid #aaaaaa;
      border-radius: 10rpx;
    }
    
    .button {
      width: 100%;
      height: 80rpx;
      margin: 6rpx 0rpx;
      color: #666666;
      background-color: lightgray;
      border-radius: 10rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      
      &.selected {
        color: white;
        background-color: #409eff;
      }
    }
    
    .text {
      width: 100%;
      margin-top: 10rpx;
      font-size: 110%;
      font-weight: bold;
    }
  }
</style>
