// app.js
import { cEvent } from './utils/event.js'
const Promise = require('lib/bluebird')
var utils = require('utils/util.js')
var commonFunc = require('utils/common.js')
var cityJson = require('utils/city.js')
var wxService = require('utils/wxService')
let config = require('./configScreen/configScreen');
const envInfo = require('config/index').envInfo

App({
  onLaunch: function (e) {
   //  "navigationStyle": "custom"
   // 默认 false
   wx.setStorageSync('reLoginErr', false)
    try {
      let res = wx.getSystemInfoSync();
      config.pixelRate = res.windowWidth / 750;
      config.platform = res.platform;
      config.statusBarHeight = res.statusBarHeight;
      if (res.platform.toLowerCase() == 'devtools') {
        config.capsuleHeight = 44;
      }
      if (res.platform.toLowerCase() == 'android') {
        config.capsuleHeight = 48;
      }
      config.titleHeight = (config.capsuleHeight + config.statusBarHeight) / config.pixelRate;
      if (res.statusBarHeight >= 44) {
        config.isHighHead = true;
      }
      if (res.windowHeight > 750) config.isAllScreen = true;
      config.systemHeight = res.windowHeight;
    } catch (e) {
      console.log(err);
    }
    
  },
  onShow: function (options = {}) {
    console.log('app-onshow', options)
    
    const baseUserInfo = wx.getStorageSync('_baseUserInfo')
    if (options.referrerInfo.extraData!=null){
      const { activate_ticket, card_id, code, errCode, wx_activate_after_submit_url } = options.referrerInfo.extraData
      let urlData = wx_activate_after_submit_url.split("?");
      let url = urlData[0];
      let parmas = urlData[1];
    
      // encodeURIComponent
      // decodeURIComponent
      wxService.router(`${url}`).search({
        activate_ticket: encodeURIComponent(activate_ticket),
        card_id,
        code,
        errCode,
        wx_activate_after_submit_url: parmas
      });

      // 激活成功 变更 _baseUserInfo
      baseUserInfo.wechatCardId = card_id
      baseUserInfo.wechatCode = code
      wx.setStorageSync('_baseUserInfo',baseUserInfo)
    }
    // 711_授权字体
    wx.loadFontFace({
      family: 'iconfont-711',
      source: 'url("https://cdn-alyun.bigaka.com/group1/M00/00/01/Cv6CDl0nE9aAdG45AC7AStq8LfA180.TTF")',
      success: function (res) {
        console.log(res.status) //  loaded
      },
      fail: function (res) {
        console.log(res.status) //  error
      }
    })

  },
  onHide: function () {
    this.globalData.loginFirst = false
  },
  getUserInfo() {
    return new Promise((resolve, reject) => {
      const _baseUserInfo = wx.getStorageSync('_baseUserInfo')
      if (_baseUserInfo) {
        resolve(_baseUserInfo)
      } else {
        reject('_baseUserInfo is null')
      }
    })
  },
  checkFullSucreen () {
    const self = this
    wxService.getSystemInfo().then(res => {
      if ((res.screenHeight - res.windowHeight - res.statusBarHeight - 32) > 72) {
　　　　　//  处理相关逻辑
        self.globalData.isFullSucreen = true
        // setGlobalData('fullScreen', true)
      }
    })
  },

  globalData: {
    userInfo: null,
    userBaseInfo: null, // openid
    systemInfo: wx.getSystemInfoSync(),
    ctx: envInfo.ctx,
    openCard: false,
    userNo: null,
    storeId: '',
    storeName: '',
    loginFirst: false,
    imageUrl: envInfo.imgCtx, //图片地址
    isFullSucreen: false, // 当前设备是否为 FullSucreen
    commonFunc: commonFunc,
    cityJson: cityJson,
    // brandId: 2711,
    // brandId: 2005,
    brandId: 1001,
    // brandId: 1002,
  }
})
