import log from './log.js';
import Monitor from '@/utils/monitor.js';
import uni from './uniHooks';
export default {
  cleanMemberCache(app) {
    app.$store.commit('SET_MEMBER', '');
    app.$store.commit('SET_USER_INFO', '');
    app.$store.commit('SET_OPENID', '');
    // uni.removeStorageSync('memberArray');
  },
  cleanCurMemberCache(app) {
    this.cleanMemberCache(app);
    // app.$store.commit('SET_MEMBER', '');
    // app.$store.commit('SET_USER_INFO', '');
    // app.$store.commit('SET_MEMBER', '');
    // app.$store.commit('SET_USER_INFO', '');
    // // return ''
    // try {
    // 	app.$store.state.member = null
    // 	app.$store.state.wxMember = null
    // 	app.$store.state.userInfo = null;
    // } catch (err) {
    // 	console.log(17, err);
    // }
    // uni.removeStorageSync('member')
    // uni.removeStorageSync('wxMember')
    // uni.removeStorageSync("userInfo");
  },
  cleanKipMemberCache(app) {
    // return ''
    try {
      app.$store.state.kipMember = null;
      app.$store.state.openId = null;
    } catch (err) {
      console.log(29, err);
    }
    uni.removeStorageSync('kipMember');
    uni.removeStorageSync('openId');
  },
  setMemberCache(app, { member, wxMember }) {
    if (wxMember) {
      member.openId = wxMember.openid;
      member.unionid = wxMember.unionid;
    } else {
      wxMember = member;
    }
    const userInfo = memberToWxUserInfo(member);

    const memberObj = {
      member,
      wxMember,
      userInfo,
    };

    setMemberArrayCache(app, member.groupId, memberObj);
  },
  setCurMemberCacheByGroupId(app, curGroupId) {
    if (!curGroupId) {
      console.error('[setCurMemberCache] curGroupId不存在');
      return;
    }
    const memberArray = uni.getStorageSync('memberArray');
    if (!memberArray) {
      console.warn('[setCurMemberCache] memberArray不存在');
      return null;
    }
    if (memberArray && curGroupId) {
      const curMemberObj = memberArray[curGroupId];
      if (curMemberObj) {
        changeCurMemberCache(app, curMemberObj);
        return curMemberObj;
      } else {
        console.info('[setCurMemberCache] 清空当前会员信息');
        this.cleanCurMemberCache(app);
      }
    }
    return null;
  },
  setKipMember(app, kipMember) {
    if (kipMember) {
      // app.globalData.kipMember = kipMember
      // app.globalData.openId = kipMember.openId;
      uni.setStorageSync('kipMember', kipMember);
      // uni.setStorageSync('openid', kipMember.openId);
      // Monitor.setConfig({
      // 	c1: kipMember.id
      // })
    }
  },
  getKipMember() {
    const kipMember = uni.getStorageSync('kipMember');
    if (isNotEmpty(kipMember)) {
      return kipMember;
    }
    return null;
  },
  handleKipMemberToCrmMember(kipMemberInfo) {
    if (!kipMemberInfo) return null;
    // lastLoginTime: null
    let crmMember = {
      kipUserId: kipMemberInfo.id,
    };
    if (kipMemberInfo.avatar) {
      crmMember.headimgUrl = kipMemberInfo.avatar;
    }
    if (kipMemberInfo.birthDate) {
      crmMember.birthday = kipMemberInfo.birthDate;
    }
    if (kipMemberInfo.gender || kipMemberInfo.gender == '0') {
      if (kipMemberInfo.gender == '0') {
        crmMember.sex = 'U';
      } else if (kipMemberInfo.gender == '1') {
        crmMember.sex = 'M';
      } else if (kipMemberInfo.gender == '2') {
        crmMember.sex = 'F';
      }
    }
    if (kipMemberInfo.nickName) {
      crmMember.nickname = kipMemberInfo.nickName;
    }
    if (kipMemberInfo.openId) {
      crmMember.openid = kipMemberInfo.openId;
    }
    if (kipMemberInfo.phoneNumber) {
      crmMember.mobile = kipMemberInfo.phoneNumber;
    }
    if (kipMemberInfo.realName) {
      crmMember.realName = kipMemberInfo.realName;
    }
    crmMember.source = '1';
    return crmMember;
  },
  getMemberCacheByGroupId(groupId) {
    if (!groupId) {
      console.warn('[getMemberCacheByGroupId] groupId 不能为空');
      return null;
    }
    const memberArray = uni.getStorageSync('memberArray');
    if (!memberArray) {
      console.warn('[getMemberCacheByGroupId] memberArray 为空');
      return null;
    }
    return memberArray[groupId];
  },
  setOpenId(app, openId) {
    if (!openId) {
      return;
    }
    if (app) {
      app.globalData.openId = openId;
    }
    uni.setStorageSync('openid', openId);
  },
  getOpenId() {
    // let openId = app.globalData.openId;
    let openId = '00000';
    log.info(`===>获取到globalData中的openId: ${openId}`);
    // if (!openId) {
    // 	// openId = app.globalData.member?.openId
    // 	let openId = '00000'
    // 	log.info(`===>获取到globalData.member中的openId: ${openId}`)
    // }
    if (!openId) {
      openId = uni.getStorageSync('openid');
      log.info(`===>获取到storage中的openId: ${openId}`);
    }
    // if (!openId) {
    // 	openId = uni.getStorageSync("member")?.openId;
    // 	log.info(`===>获取到storage.member中的openId: ${openId}`)
    // }
    return openId;
  },
};

// 改变当前会员信息
function changeCurMemberCache(app, { member, wxMember, userInfo }) {
  if (app) {
    app.globalData.member = member;
    app.globalData.wxMember = wxMember;
    app.globalData.userInfo = userInfo;
  }
  uni.setStorageSync('member', member);
  uni.setStorageSync('wxMember', wxMember);
  uni.setStorageSync('userInfo', userInfo);
  Monitor.setConfig({
    setUsername: () => member.vipcode,
  });
}

// 将会员信息存入数组
function setMemberArrayCache(app, groupId, memberObj) {
  if (!groupId || !memberObj) {
    return null;
  }
  let memberArray = uni.getStorageSync('memberArray');
  if (!memberArray) {
    memberArray = {};
  }
  memberArray[groupId] = memberObj;
  uni.setStorageSync('memberArray', memberArray);
  changeCurMemberCache(app, memberObj);
}

function isEmpty(obj) {
  if (!obj || JSON.stringify(obj) == '{}' || obj == 'undefined') {
    return true;
  }
  return false;
}

function isNotEmpty(obj) {
  return !isEmpty(obj);
}

function memberToWxUserInfo(member) {
  if (!member) return null;
  const userInfo = {
    kipUserId: member.kipUserId,
  };
  if (member.headimgurl) {
    userInfo.avatarUrl = member.headimgurl;
  }
  if (member.nickname) {
    userInfo.nickName = member.nickname;
  }
  if (member.countryAddress) {
    userInfo.country = member.countryAddress;
  }
  if (member.headimgurl) {
    userInfo.province = member.headimgurl;
  }
  if (member.provinceAddress) {
    userInfo.city = member.provinceAddress;
  }
  if (member.sex) {
    if (member.sex == 'U') {
      userInfo.gender = 0;
    } else if (member.sex == 'M') {
      userInfo.gender = 1;
    } else if (member.sex == 'F') {
      userInfo.gender = 2;
    }
  }
  return userInfo;
}
