package cn.iselab.mooctest.user.web.logic.impl;

import cn.iselab.mooctest.rpc.user.data.UserDTO;
import cn.iselab.mooctest.rpc.user.data.UserWechatDTO;
import cn.iselab.mooctest.rpc.user.data.WechatUserInfoDTO;
import cn.iselab.mooctest.user.configure.WechatConfiguration;
import cn.iselab.mooctest.user.constants.WechatUserConstants;
import cn.iselab.mooctest.user.data.Token;
import cn.iselab.mooctest.user.data.UserInfo;
import cn.iselab.mooctest.user.model.User;
import cn.iselab.mooctest.user.model.UserWechat;
import cn.iselab.mooctest.user.service.UserService;
import cn.iselab.mooctest.user.service.WechatService;
import cn.iselab.mooctest.user.util.WechatUtil;
import cn.iselab.mooctest.user.web.logic.BaseLogic;
import cn.iselab.mooctest.user.web.logic.UserLogic;
import cn.iselab.mooctest.user.web.logic.WechatLogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ROKG
 * @Description
 * @Date: Created in 上午11:11 2018/4/17
 * @Modified By:
 */
@Service
public class WechatLogicImpl extends BaseLogic implements WechatLogic {

    @Autowired
    WechatConfiguration wechatConfiguration;

    @Autowired
    UserService userService;

    @Autowired
    WechatService wechatService;

    @Autowired
    UserLogic userLogic;

    @Override
    public UserWechatDTO getUserByCode(String code){
        LOG.info(String.format("CODE: %s", code));
        UserWechatDTO dto=new UserWechatDTO();
        Token token=wechatConfiguration.getToken(code);
        LOG.info(String.format("OPENID: %s",token.getOpenId()));
        WechatUserInfoDTO userInfo= WechatUtil.getWxUser(token.getAccessToken(),token.getOpenId());
        LOG.info(userInfo.getUnionid());
        UserWechat wechat=wechatService.findByUnionId(userInfo.getUnionid());
        if (wechat != null && !wechat.isDelete()) {
            UserDTO userDTO=userLogic.getUserById(wechat.getUserId());
            LOG.info(userDTO.getName());
            dto.setUser(userDTO);
            dto.setType(UserWechatDTO.OLD);
            return dto;
        }else {
            dto.setUserInfo(userInfo);
            dto.setType(UserWechatDTO.NEW);
            return dto;
        }
    }

    @Override
    public UserDTO setUserWechat(UserWechatDTO dto){
        UserDTO userDTO=dto.getUser();
        WechatUserInfoDTO userInfoDTO=dto.getUserInfo();
        UserWechat wechat=wechatService.findByUserId(userDTO.getId());
        if (wechat != null) {
            if(wechat.isDelete()){
                wechat.setDelete(false);
                wechatService.update(wechat);
                return userDTO;
            }else {
                return userDTO;
            }
        }else {
            wechat=new UserWechat();
            wechat.setUserId(userDTO.getId());
            wechat.setUnionId(userInfoDTO.getUnionid());
            wechat.setOpenId(userInfoDTO.getOpenid());
            wechat.setDelete(false);
            wechatService.save(wechat);
            return userDTO;
        }
    }
}
