const app = getApp();
import strings from '../../utils/strings.js';
const {
  BtHelper
} = require('../../devices/bt_helper');
const {
  BleUtil
} = require('../../utils/blue_ble/util');

let percentIn = null;
Page({
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '连接配网', //导航栏 中间的标题
    },
    scopeBluetooth: app.globalData.scopeBluetooth,
    ssid: app.globalData.ssid,
    password: app.globalData.pwdData,
    version: '2.0',
    name: '',
    connectedDeviceId: '',
    connected: true,
    deviceInfo: null,
    isInitOK: false,
    customData: '',
    percent: 0,
    ruterStatus: 0, // 0 连接中 // 1 成功 // 2失败
    imageUrl: "./../../img/g.png",
  },

  onLoad: function (options) {
    var that = this;
    var param = options.param;
    if (!strings.isEmpty(param)) {
      param = JSON.parse(param);
      that.setData({
        imageUrl: param.img,
      });
    }

    // AIrSMArT_861210052356337===D0:31:10:86:AC:9A
    /// 纯蓝牙：MW_BLE CC:90:DD:88:C7:E8
    var name = options.name;
    var deviceId = options.deviceId;
    that.setData({
      name: name,
      connectedDeviceId: deviceId,
    });

    // var device = {
    //   "mac": name,
    //   "deviceId": deviceId
    // };

    var device = {
      "mac": "MW_BLE",
      "deviceId": "CC:90:DD:88:C7:E8"
    };
    console.log("gadsfasdfadfaf===00==");

    percentIn = setInterval(() => {
      if (that.data.percent === 100) {
        clearInterval(percentIn);
        ///失败
        that.setData({
          ruterStatus: 2
        });
        return;
      };
      that.data.percent++;
      that.setData({
        percent: that.data.percent
      });
    }, 600);

    BtHelper.getInstance().startScan(device,
      async function (b) {

          if (!b) {
            that.setData({
              connected: false
            })
            that.setData({
              ruterStatus: 2
            });
          }

          setTimeout(function () {
            BleUtil.getInstance().startConnect(device, function (changed) {
              if (changed) {
                BleUtil.getInstance().sendData(device, app.globalData.ssid, app.globalData.pwdData);
              }
            });
          }, 10 * 1000);
        },
        async function (res) {
          var deviceId = that.data.name;
          var device = {
            connectType: 3,
            devName: "",
            state: "online",
            name: deviceId,
            deviceId: deviceId,
            mac: deviceId,
            imageUrl: that.data.imageUrl,
            // imageUrl: "./../../img/min.png",
          }
          getCurrentPages()[0].addWifiDevice(device);
          // 记住密码
          wx.setStorage({
            key: "wifiInfo",
            data: JSON.stringify({
              "password": app.globalData.pwdData,
              "ssid": app.globalData.ssid
            }),
          });

          setTimeout(() => {
            clearInterval(percentIn);
            that.setData({
              percent: 100,
              ruterStatus: 1,
            });
          }, 500);
        }
    );
  },
})