package com.seer.teach.user.app.auth.service.strategy;

import cn.hutool.json.JSONUtil;
import com.seer.teach.common.enums.ProviderEnum;
import com.seer.teach.common.utils.AESUtils;
import com.seer.teach.mp.api.resp.WxMiniProgramSessionDTO;
import com.seer.teach.mp.api.resp.WxOAuth2AccessTokenDTO;
import com.seer.teach.user.entity.UserAuthEntity;
import com.seer.teach.user.service.IUserAuthService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public class AbstractLoginStrategy {

    @Autowired
    protected IUserAuthService userAuthService;

    /**
     * 保存用户第三方认证信息
     *
     * @param authType 登录类型
     * @param userId 用户Id
     * @param wxOAuth2AccessToken 微信授权信息
     * @param appId 应用Id
     * @return
     */
    public boolean saveUserAuth(String authType, Integer userId, WxOAuth2AccessTokenDTO wxOAuth2AccessToken,String appId) {
        UserAuthEntity userAuth = new UserAuthEntity();
        userAuth.setOpenId(wxOAuth2AccessToken.getOpenId());
        userAuth.setUnionId(wxOAuth2AccessToken.getUnionId());
        userAuth.setAuthType(authType);
        userAuth.setStatus(1);
        userAuth.setAppId(appId);
        userAuth.setUserId(userId);
        userAuth.setAccessToken(AESUtils.encrypt(wxOAuth2AccessToken.getAccessToken()));
        userAuth.setRefreshToken(AESUtils.encrypt(wxOAuth2AccessToken.getRefreshToken()));
        userAuth.setProvider(ProviderEnum.WECHAT.name());

        // 计算 token 过期时间
        LocalDateTime tokenExpiresAt = LocalDateTime.now().plusSeconds(wxOAuth2AccessToken.getExpiresIn());
        userAuth.setTokenExpiresAt(tokenExpiresAt);
        userAuth.setLoginCount(1);
        userAuth.setLastLoginAt(LocalDateTime.now());
        return userAuthService.saveOrUpdate(userAuth);
    }

    public boolean saveUserAuth(String authType, Integer userId, WxMiniProgramSessionDTO miniProgramSessionDTO) {
        UserAuthEntity userAuth = new UserAuthEntity();
        userAuth.setOpenId(miniProgramSessionDTO.getOpenid());
        userAuth.setUnionId(miniProgramSessionDTO.getUnionid());
        userAuth.setAuthType(authType);
        userAuth.setAppId(miniProgramSessionDTO.getAppId());
        userAuth.setStatus(1);
        userAuth.setUserId(userId);
        userAuth.setProvider(ProviderEnum.WECHAT.name());
        Map<String,String> credential = new HashMap<>();
        credential.put("session_key", miniProgramSessionDTO.getSessionKey());
        userAuth.setCredentialJson(JSONUtil.toJsonStr(credential));
        userAuth.setLoginCount(1);
        userAuth.setLastLoginAt(LocalDateTime.now());
        return userAuthService.saveOrUpdate(userAuth);
    }


    /**
     * 更新用户的登录次数和最后登录时间
     *
     * @param userAuth
     * @return  boolean 更新成功返回 true
     */
    public boolean updateUserAuth(UserAuthEntity userAuth) {
        UserAuthEntity updateUserAuthEntity = new UserAuthEntity();
        updateUserAuthEntity.setLoginCount(userAuth.getLoginCount()+1);
        updateUserAuthEntity.setLastLoginAt(LocalDateTime.now());
        updateUserAuthEntity.setId(userAuth.getId());
        updateUserAuthEntity.setAppId(userAuth.getAppId());
        return userAuthService.updateById(updateUserAuthEntity);
    }

}
