const wx2my = require('./wx2my');
const Behavior = require('./Behavior');
// 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 wxService = require('./utils/wxService');

let config = require('./configScreen/configScreen');

const envInfo = require('./config/index').envInfo;

App({
  onLaunch: function (e) {
    // 默认 false
    wx2my.setStorageSync('reLoginErr', false);    
  },
  onShow: function (options = {}) {
    const baseUserInfo = wx2my.getStorageSync('_baseUserInfo').data;
    if (options && options.referrerInfo && 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;
      wx2my.setStorageSync('_baseUserInfo', baseUserInfo);
    } 
    // 711_授权字体 支付宝不支持
    
    // wx2my.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 = wx2my.getStorageSync('_baseUserInfo').data;

      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: wx2my.getSystemInfoSync(),
    ctx: envInfo.ctx,
    openCard: false,
    userNo: null,
    storeId: '',
    storeName: '',
    loginFirst: false,
    imageUrl: envInfo.imgCtx,
    //图片地址
    isFullSucreen: false,
    // 当前设备是否为 FullSucreen
    commonFunc: commonFunc,
    brandId: 2711,
    // brandId: 2005,
    // brandId: 1001, 
    // brandId: 1002,

  }
});