<template>
  <view>
    <view class="py-3 px-4 bg-white flex items-center justify-between text-base" @click="toSelect">
      <view class="item-title">猫眼工作模式</view>
      <view class="flex items-center">
        <view class="mr-2">
          {{
            $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0]
              ? catEyeMode[
                  $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0]?.catEyeMode
                ].name
              : ""
          }}
        </view>
        <up-icon name="arrow-right"></up-icon>
      </view>
    </view>
    <view class="py-3 px-4 bg-white !py-2 flex items-center justify-between text-base mt-4rpx">
      <view class="item-title">自动亮屏</view>
      <switch
        @click="change('autoLightScreen')"
        :checked="$bluetooth.currentLockSetting.lockSettingInfo.autoLightScreen === 1"
        class="transform-scale-90"
        :disabled="true"
        color="#002ce5"
      />
    </view>
    <view
      class="py-3 px-4 bg-white !py-2 flex items-center justify-between text-base mt-4rpx"
      @click="open"
    >
      <view class="item-title">亮屏持续时间</view>
      <view class="flex items-center">
        <view class="mr-2">
          {{ $bluetooth.currentLockSetting.lockSettingInfo.autoLightScreenTime }}秒
        </view>
        <up-icon name="arrow-right"></up-icon>
      </view>
    </view>
    <view class="py-3 px-4 bg-white !py-2 flex items-center justify-between text-base mt-4rpx">
      <view class="item-title">逗留警告</view>
      <switch
        @click="change('stayWarn')"
        :checked="$bluetooth.currentLockSetting.lockSettingInfo.stayWarn === 1"
        class="transform-scale-90"
        :disabled="true"
        color="#002ce5"
      />
    </view>
    <view class="py-3 px-4 bg-white !py-2 flex items-center justify-between text-base mt-4rpx">
      <view class="item-title">异常警告</view>
      <switch
        @click="change('abnormalWarn')"
        :checked="$bluetooth.currentLockSetting.lockSettingInfo.abnormalWarn === 1"
        class="transform-scale-90"
        :disabled="true"
        color="#002ce5"
      />
    </view>
  </view>
</template>

<script setup>
  import { computed, ref } from 'vue'
  import { updateCatEyeConfigRequest } from '@/api/setting'
  import { useBluetoothStore } from '@/stores/bluetooth'
  import { useUserStore } from '@/stores/user'
  import { useBasicStore } from '@/stores/basic'
  import { catEyeMode } from '@/constant/catEyeMode'

  const $bluetooth = useBluetoothStore()
  const $user = useUserStore()
  const $basic = useBasicStore()

  const value = computed(() => {
    const list = []
    if ($bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0]) {
      list.push($bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeMode)
      if ($bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeMode === 4) {
        if (
          $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig
            .recordStartTime === 0 &&
          $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig
            .recordEndTime === 1440
        ) {
          list.push(1)
        } else {
          list.push(0)
        }
        list.push(
          $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig
            .recordStartTime / 256
        )
        list.push(
          $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig
            .recordStartTime % 256
        )
        list.push(
          $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig
            .recordEndTime / 256
        )
        list.push(
          $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig
            .recordEndTime % 256
        )
        list.push(
          $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig.recordTime
            ? $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig
                .recordTime
            : 0
        )
        list.push(
          $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig
            .detectionDistance
            ? $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig
                .detectionDistance
            : 0
        )
        list.push(
          $bluetooth.currentLockSetting.lockSettingInfo.catEyeConfig[0].catEyeModeConfig
            .realTimeMode
        )
      } else {
        list.push(...[0, 0, 0, 0, 0, 0, 0, 0])
      }
    } else {
      list.push(...[0, 0, 0, 0, 0, 0, 0, 0, 0])
    }
    list.push($bluetooth.currentLockSetting.lockSettingInfo.autoLightScreen)
    list.push($bluetooth.currentLockSetting.lockSettingInfo.autoLightScreenTime)
    list.push($bluetooth.currentLockSetting.lockSettingInfo.stayWarn)
    list.push($bluetooth.currentLockSetting.lockSettingInfo.abnormalWarn)
    return list
  })

  const pending = ref(false)

  const open = () => {
    uni.showActionSheet({
      itemList: ['10秒', '15秒', '20秒'],
      success(res) {
        let code
        if (res.tapIndex === 0) {
          code = 10
        } else if (res.tapIndex === 1) {
          code = 15
        } else if (res.tapIndex === 2) {
          code = 20
        }
        let array = value.value
        array[10] = code
        update(array, 'autoLightScreenTime', code)
      }
    })
  }

  const toSelect = () => {
    $basic.routeJump({
      name: 'catEyeMode',
      events: {
        update: data => {
          update(data.value, data.type, data.params, data.back)
        }
      }
    })
  }

  const change = type => {
    if (type === 'autoLightScreen') {
      let array = value.value
      array[9] = $bluetooth.currentLockSetting.lockSettingInfo.autoLightScreen === 1 ? 0 : 1
      update(array, type, array[9])
    } else if (type === 'stayWarn') {
      let array = value.value
      array[11] = $bluetooth.currentLockSetting.lockSettingInfo.stayWarn === 1 ? 0 : 1
      update(array, type, array[11])
    } else if (type === 'abnormalWarn') {
      let array = value.value
      array[12] = $bluetooth.currentLockSetting.lockSettingInfo.abnormalWarn === 1 ? 0 : 1
      update(array, type, array[12])
    }
  }

  const update = async (array, key, params, back = false) => {
    if (pending.value) return
    const netWork = await $basic.getNetworkType()
    if (!netWork) {
      return
    }
    pending.value = true
    uni.showLoading({
      title: '更新中'
    })
    const featureBit = 64
    const { code } = await $bluetooth.updateSetting({
      keyId: $bluetooth.keyId.toString(),
      uid: $user.userInfo.uid.toString(),
      featureBit,
      params: array,
      withParams: true
    })
    $bluetooth.closeBluetoothConnection()
    if (code === 0) {
      const { code, message } = await updateCatEyeConfigRequest({
        lockId: $bluetooth.currentLockInfo.lockId,
        [key]: params
      })
      pending.value = false
      uni.hideLoading()
      if (code === 0) {
        $bluetooth.updateCurrentLockSetting({
          ...$bluetooth.currentLockSetting,
          lockSettingInfo: {
            ...$bluetooth.currentLockSetting.lockSettingInfo,
            [key]: params
          }
        })

        if (back) {
          $basic.backAndToast('更新成功')
        } else {
          uni.showToast({
            title: '更新成功',
            icon: 'none'
          })
        }
      } else {
        uni.showToast({
          title: message,
          icon: 'none'
        })
      }
    } else {
      pending.value = false
      uni.hideLoading()
      uni.showToast({
        title: '更新失败，请保持在锁附近',
        icon: 'none'
      })
    }
  }
</script>

<style lang="scss">
  page {
    background-color: $uni-bg-color-grey;
  }
</style>
