import { getUserInfo, setWxAuthUserInfo, getThirdInfo, createAccount, changePhone } from '@/api/user.js';
import store from '@/store';
import config from '@/config';

// 获取用户数据
function getUserData() {
    return new Promise((resolve, reject) => {
        Promise.all([getUserInfo(), getThirdInfo()]).then(([userInfoRes, thirdInfores]) => {
            let userInfo = userInfoRes.data || {};
            let thirdInfo = thirdInfores.data || {};
            let cachedInfo = wx.getStorageSync('UserInfo');
            userInfo = { ...cachedInfo, ...userInfo, ...thirdInfo };
            userInfo.userName = userInfo.userName || '';
            // test 用户身份
            // userInfo.roles = ['tenant.admin', 'operator', 'owner'];
            // userInfo.phone = '';wxLogin
            // userInfo.nickName = '';
            // userInfo.userName = ''
            userInfo = addRolesInfoToUserInfo(userInfo);
            if (!userInfo.userName) {
                userInfo.defaultUserName = 'Hello';
            }
            // todo 模拟银泰配置 接口增加buildingLogo
            if (config.projectName === 'yintai') {
                userInfo.buildingLogo = `${config.h5StaticPath}/page-yintai/project-logo.png`;
            }
            //   userInfo.companyId = 'c68dcccd57984277ab7736f2d257cd0c'
            store.commit('setUserInfo', userInfo);
            resolve(userInfo);
        }).catch((res) => {
            reject(res);
        })
    })
}

// 检查登录
function checkLogin(needGetetUserInfo = true) {
    let token = store.state.user.token;
    if (!token) {
        return wxLogin(needGetetUserInfo);
    } else {
        return Promise.resolve();
    }
}

//  检查用户信息
function checkHasUserInfo() {
    let userInfo = store.state.user.userInfo;
    if (userInfo && JSON.stringify(userInfo) !== '{}') {
        return Promise.resolve(userInfo);
    } else {
        return checkLogin(false).then(() => {
            return getUserData();
        })
    }
}

function setUserInfoByAuth() {
    return new Promise((resolve, reject) => {
      // if (ing) {
      //   return;
      // }
      // ing = true;
      // let { userInfo = {} } = store.state.user;
      // // 如果已经保存过了
      // let nickName = userInfo.nickName;
      // if (nickName) {
      //   ing = false;
      //   resolve();
      //   return;
      // }
      wx.getUserProfile({
        desc: '用于完善用户信息',
        success: res => {
          let detail = res;
          wx.login({
            success: loginRes => {
              // ing = false;
              // if (loginRes.code) {
              //   let params = {
              //     encryptedData: detail.encryptedData,
              //     iv: detail.iv,
              //     jsCode: loginRes.code
              //   };
              //   setWxAuthUserInfo(params).then(res => {
              //     let userInfo = JSON.parse(detail.rawData);
              //     let cachedInfo = wx.getStorageSync('UserInfo');
              //     userInfo = {
              //       ...cachedInfo,
              //       ...userInfo,
              //       headImgUrl: userInfo.avatarUrl
              //     };
              //     delete userInfo.avatarUrl;
              //     store.commit('setUserInfo', {
              //       ...userInfo
              //     });
              //     wx.setStorageSync('UserInfo', { ...userInfo });
              //     resolve(res.data);
              //   });
              // }
            },
            fail: res => {
              // ing = false;
              // reject(res);
            }
          });
        },
        fail: res => {
          // ing = false;
          // wx.showToast({
          //   icon: 'none',
          //   title: '授权用户公开信息后方可进入下一步操作'
          // });
          // reject();
        }
      });
    });
  }

export {
    checkHasUserInfo,
    setUserInfoByAuth
}