import Taro from "@tarojs/taro";
import { createSlice } from "@reduxjs/toolkit";

// 设备连接蓝牙
const deviceInfoReducer = createSlice({
  name: "deviceinfo", // store的名字
  initialState: {
    bluetoothInfo: {
      deviceId: "", // 先测试一下
      servicesuuid: "",
      characteristicsuuid0: "",
      characteristicsuuid1: "",
      characteristicId: "",
    },
    OtaDeviceTypeEnum: {
      WL200: "WL200",
      WE100: "WE100",
      FACIALMASK: "FacialMask",
      STAND: "Stand",
    },
  },
  reducers: {
    setDeviceInfo(state, { payload }) {
      state = JSON.parse(JSON.stringify(payload));
    },
    setBluetoothInfo(state, { payload }) {
      console.log("setBluetoothInfo payload", payload);
      state.bluetoothInfo.deviceId = payload.deviceId;
      if (payload.servicesuuid) {
        state.bluetoothInfo.servicesuuid = payload.servicesuuid;
      }
      if (payload.characteristicsuuid0) {
        state.bluetoothInfo.characteristicsuuid0 = payload.characteristicsuuid0;
      }
      if (payload.characteristicsuuid1) {
        state.bluetoothInfo.characteristicsuuid1 = payload.characteristicsuuid1;
      }
      if (payload.characteristicId) {
        state.bluetoothInfo.characteristicId = payload.characteristicId;
      }
    },
    setDeviceId(state, { payload }) {
      console.log("setDeviceId payload", payload);
      state.bluetoothInfo.deviceId = payload.deviceId;
      console.log("state.bluetoothInfo.deviceId", state.bluetoothInfo.deviceId);
    },
  },
});

export const { setBluetoothInfo, setDeviceId } = deviceInfoReducer.actions;
export default deviceInfoReducer.reducer;
