const {
  BtHelper
} = require('../../devices/bt_helper');
const {
  deviceVersion
} = require('../../request/deviceListRequest')
import {
  EnumCmdEvent,
  CmdEvent
} from '../../devices/cmd_key_event';
import EventManager from '../../utils/event_bus'
import route_util from '../../utils/route_util';

Page({
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '设备详情', //导航栏 中间的标题
    },
    // 页面数据
    device: {},
    btHelper: null,
    updateData: {},
    powerOpen: false,
    isShowWallpaper: false,

  },
  checkOtaVersion(device) {
    let _this = this;
    device.clientType = "MW-S2"
    deviceVersion(device.clientType, device.connectType, device.version ?? "1.0.0").then(res => {
      console.log("OTA2：", res);
      let updateData = res ?? {};

      let hasNewVersion = updateData.isEnforcement === 1 || updateData.isEnforcement === 2;
      updateData.hasNewVersion = hasNewVersion;
      _this.setData({
        updateData: updateData
      });
    })
  },


  goToWallpaper: function () {
    let newParam = JSON.stringify(this.data.device);
    route_util.jumpParam('/pages/piano/wallpaper/wallpaper', newParam)
  },

  goToOta: function () {
    let param = {
      "device": this.data.device,
      "otaData": this.data.updateData,
    }
    let newParam = JSON.stringify(param);
    // wx.navigateTo({
    //   url: '/pages/ota/ota' + "?param=" + param
    // });
    route_util.jumpParam('/pages/ota/ota', newParam)
  },
  powerTap(e) {
    console.log(e)
    let btHelper = BtHelper.getInstance();
    btHelper.setPauseSleep(!this.data.powerOpen ? 1200 : 0)
    this.setData({
      powerOpen: !this.data.powerOpen
    })
  },
  questionTap() {
    wx.showModal({
      title: '省电模式',
      content: '开启省电模式，设备无蓝牙连接或内容播放20分钟后进入自动休眠，任意按键可唤醒。',
      showCancel: false,
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          // setSleepAfterPlayPause
          const btHelper = BtHelper.getInstance();
          btHelper.setPauseSleep(1200)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // let device = JSON.parse(options.param);
    console.log(getApp().globalData.mDeviceList.length)
    let device = JSON.parse(options.param);
    // let device = getApp().globalData.mDeviceList[0] ?? JSON.parse(options.param);
    if (device.connectType != '1') {
      return
    }
    device.clientType = device.clientType ?? device.ProdModel

    console.log("设备详情", device)
    let isConnect = device.state === 'online'
    if (!isConnect) {
      // btHelper
      wx.showToast({
        title: '设备已经失去连接',
      })
      return;
    }
    this.setData({
      device: device,
      powerOpen: (device.pauseSleep ?? -1) > 0,
      isShowWallpaper: device.clientType === '猫王妙播黑胶音箱(BLE)' || device.clientType === 'MW-S2'
    })

    const btHelper = BtHelper.getInstance();
    let isShowOta = device.clientType === '猫王妙播黑胶音箱(BLE)' || device.clientType === 'MW-S2'
    if (isShowOta) {
      btHelper.getVersion()
    }
    btHelper.getPauseSleep()

    let _this = this;
    EventManager.addNotification(CmdEvent.eventName, function (event) {
      let name = event.cmdEvent;
      // console.log("详情页：", name, event)
      switch (name) {
        case EnumCmdEvent.version:
          _this.data.device.version = event.version;
          console.log("详情页1：", event.version)

          _this.checkOtaVersion(_this.data.device);
          break;
        case EnumCmdEvent.authSleepStatus:
          let value = event.pauseSleep;
          console.log("详情页2：", value)
          _this.setData({
            powerOpen: (value ?? -1) > 0
          })
          break;
      }
    }, this)
  },

  onUnload() {
    EventManager.removeNotification(CmdEvent.eventName, this)
  },

})