const app = getApp();
import strings from '../../utils/strings.js';
import route_util from '../../utils/route_util.js';
import route_constant from '../../utils/route_constant.js'

Page({
  data: {
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '连接配网', //导航栏 中间的标题
    },
    scopeBluetooth: false,
    userFuzzyLocation: true,
    isNotruter: false,
    connectDevice: {},
    imageUrl: "./../../img/c.png",
  },

  ///定位，蓝牙权限，蓝牙适配器

  // {"applicationType":"[0, 1]","deviceId":430635766251545,"img":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113416291791429.png","offlineImg":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113421204031133.png","connectImg":null,"name":"猫王音响x阿基米德 SR1 妙播收音机","bluetoothName":"SR1妙播收音机","bluetoothNames":["SR1妙播收音机","猫王XOG·CyberRadio 001"],"isChannelsPlatforms":1,"platform":0,"typeList":[{"is5g":0,"type":1,"connectType":1,"functionList":[6,1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113427142232548.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113431143471942.png","icon":null,"guideUrl":null}},{"is5g":0,"type":2,"connectType":3,"functionList":[1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113437930271177.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113441600058940.png","icon":null,"guideUrl":null}},{"is5g":0,"type":4,"connectType":3,"functionList":[1,3],"deviceLinkResp":{"icon1":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113446634000744.png","icon2":"https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20220812113450885467009.png","icon":null,"guideUrl":null}}],"clientType":"猫王妙播黑胶音箱(BLE)","firstVersion":"1","filter":null,"guideUrl":null,"manufacturer":"ShanJing","deviceType":0}
  onLoad(options) {
    var that = this;
    var param = options.param;
    if (!strings.isEmpty(param)) {
      param = JSON.parse(param);
      that.data.connectDevice = param;
      if (!strings.isEmpty(param.typeList)) {
        var typeList = param.typeList;
        typeList = typeList.filter((v) => v.connectType == 3);
        if (!strings.isEmpty(typeList)) {
          that.setData({
            imageUrl: typeList[0].deviceLinkResp.icon1,
          });
        }
      }
    }

    wx.offAppShow(that.getSetting);
    wx.onAppShow(that.getSetting);
    that.getSetting();
    if (options.isLognlink) {
      that.setData({
        isNotruter: true,
        nvabarData: {
          showCapsule: 1,
          title: '连接配网',
          callback: () => {
            route_util.goBackRoute(route_constant.indexRoot);
          }
        },
      })
    }
  },

  ///获取定位服务
  getSetting() {
    var that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting["scope.userFuzzyLocation"]) {
          app.getBluetoothStatus();
          that.setData({
            userFuzzyLocation: true,
          });
        } else if (res.authSetting["scope.userFuzzyLocation"] === undefined) {
          ///请求定位权限
          wx.authorize({
            scope: "scope.userFuzzyLocation",
            success() {
              that.getSetting();
            }
          });
        } else {
          that.setData({
            userFuzzyLocation: false,
          });
          wx.showModal({
            title: '请打开系统位置获取',
            success(res) {
              if (res.confirm) {
                wx.openSetting({
                  complete() {
                    // that.getSetting();
                  }
                })
              } else if (res.cancel) {}
            }
          });
        }
      }
    })
  },

  ///检测蓝牙权限回调 true:false
  getBluetoothStatusCallck(v) {
    var that = this;
    that.setData({
      scopeBluetooth: v
    });
  },

  ///下一步
  next() {
    var that = this;
    ///初始化蓝牙
    wx.openBluetoothAdapter({
      success(res) {
        if (that.data.scopeBluetooth && that.data.userFuzzyLocation) {
          var param = JSON.stringify(that.data.connectDevice);
          route_util.jumpParam(route_constant.deviceConnect1, param);
        }
      },
      fail(res) {
        if (res.errCode === 10001) {
          wx.showToast({
            title: '蓝牙未打开',
            icon: "none",
            duration: 2000
          })
        }
      }
    });
  },

  ///手动开启定位权限
  setUserFuzzyLocation() {
    const that = this;
    wx.openSetting({
      complete() {
        // that.getSetting();
      }
    })
  },

  //手动开启蓝牙
  setBluetooth() {
    wx.openSetting({
      complete() {
        // app.getBluetoothStatus();
      }
    })
  },

  ///返回到首页
  notRoter() {
    route_util.goBackRoute(route_constant.indexRoot);
  },

  onUnload() {
    var that = this;
    ///传入的监听函数。不传此参数则移除所有监听函数
    wx.offAppShow(that.getSetting);
  },
})