package cn.com.poc.user.wecom.service;

import cn.com.poc.common.utils.Assert;
import cn.com.poc.common.utils.StringUtils;
import cn.com.poc.user.wecom.constant.WecomConstant;
import cn.com.poc.user.wecom.domain.WecomCorpInfo;
import cn.com.poc.user.wecom.domain.WecomLoginInfoResponse;
import cn.com.poc.user.wecom.domain.WecomUserInfo;
import cn.com.poc.user.wecom.domain.WecomUserInfo3rd;
import cn.com.poc.user.wecom.domain.WecomUserProfile;
import cn.com.yict.framemax.core.i18n.I18nMessageException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WecomLoginService {

    @Resource
    private WecomTokenService wecomTokenService;

    @Resource
    private WecomUserService wecomUserService;

    public WecomUserProfile getUserProfile(String code, String loginType) {
        Assert.notBlank(code, "code");
        Assert.notBlank(loginType, "loginType");

        if (WecomConstant.LoginType.QR_CODE_LOGIN.equals(loginType)) {
            return getProfileByQrCode(code);
        }
        if (WecomConstant.LoginType.WEB_PAGE_LOGIN.equals(loginType)) {
            return getProfileByWebPage(code);
        }
        throw new I18nMessageException("exception/system.error");
    }

    private WecomUserProfile getProfileByQrCode(String code) {
        String providerToken = wecomTokenService.getProviderToken();
        WecomLoginInfoResponse loginInfo = wecomUserService.getLoginInfo(providerToken, code);
        WecomUserInfo userInfo = loginInfo.getUser_info();
        WecomCorpInfo corpInfo = loginInfo.getCorp_info();
        if (userInfo == null || corpInfo == null) {
            throw new I18nMessageException("exception/system.error");
        }
        WecomUserProfile profile = new WecomUserProfile();
        profile.setCorpId(corpInfo.getCorpid());
        profile.setUserId(userInfo.getUserid());
        profile.setOpenUserId(userInfo.getOpen_userid());
        profile.setName(userInfo.getName());
        profile.setAvatarUrl(userInfo.getAvatar());
        profile.setMobile(userInfo.getMobile());
        profile.setEmail(userInfo.getEmail());
        return profile;
    }

    private WecomUserProfile getProfileByWebPage(String code) {
        String suiteToken = wecomTokenService.getSuiteToken();
        WecomUserInfo3rd userInfo = wecomUserService.getUserInfo3rd(suiteToken, code);
        if (StringUtils.isBlank(userInfo.getUser_ticket())) {
            throw new I18nMessageException("exception/system.error");
        }
        WecomUserInfo3rd userDetail = wecomUserService.getUserDetail3rd(suiteToken, userInfo.getUser_ticket());
        WecomUserProfile profile = new WecomUserProfile();
        profile.setCorpId(resolveCorpId(userInfo, userDetail));
        profile.setUserId(resolveUserId(userInfo, userDetail));
        profile.setOpenUserId(resolveOpenUserId(userInfo, userDetail));
        profile.setName(StringUtils.defaultIfBlank(userDetail.getName(), userInfo.getName()));
        profile.setAvatarUrl(StringUtils.defaultIfBlank(userDetail.getAvatar(), userInfo.getAvatar()));
        profile.setMobile(StringUtils.defaultIfBlank(userDetail.getMobile(), userInfo.getMobile()));
        profile.setEmail(StringUtils.defaultIfBlank(userDetail.getEmail(), userInfo.getEmail()));
        return profile;
    }

    private String resolveCorpId(WecomUserInfo3rd userInfo, WecomUserInfo3rd userDetail) {
        return StringUtils.defaultIfBlank(
                StringUtils.defaultIfBlank(userDetail.getCorpid(), userInfo.getCorpid()),
                userInfo.getCorpId());
    }

    private String resolveUserId(WecomUserInfo3rd userInfo, WecomUserInfo3rd userDetail) {
        return StringUtils.defaultIfBlank(
                StringUtils.defaultIfBlank(userDetail.getUserid(), userInfo.getUserid()),
                userInfo.getUserId());
    }

    private String resolveOpenUserId(WecomUserInfo3rd userInfo, WecomUserInfo3rd userDetail) {
        return StringUtils.defaultIfBlank(
                StringUtils.defaultIfBlank(userDetail.getOpen_userid(), userInfo.getOpen_userid()),
                userInfo.getOpenId());
    }
}
