package com.css.txw.sso.util;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.txw.sso.pojo.domain.oauth2.OAuth2AccessTokenDO;
import com.css.txw.sso.pojo.dto.session.*;
import com.css.ggzc.framework.common.util.collection.CollectionUtils;
import com.css.ggzc.framework.common.util.json.JsonUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class OAuth2AccessTokenRedisDAO {


    private static final String KEY_QYUUID = "qyuuid";
    private static final String KEY_QYMC = "qymc";
    private static final String KEY_NSRSBH = "nsrsbh";
    private static final String KEY_YHXX = "yhxx";
    private static final String KEY_TOKEN_INFO = "tokenInfo";
    private static final String KEY_THIRDPARTY = "isThirdParty";
    private static final String KEY_YHLX = "yhlx";

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    public AccessTokenInfo getTokenInfo(String accessToken){
        String redisKey = formatKey(accessToken);
        return JsonUtils.toBean((String) stringRedisTemplate.opsForHash().get(redisKey,KEY_TOKEN_INFO), AccessTokenInfo.class);
    }

    public SessionInfo getSessionInfo(String accessToken){
        String redisKey = formatKey(accessToken);
        final SessionInfo sessionInfo = new SessionInfo();

        sessionInfo.setTokenInfo(JsonUtils.toBean((String) stringRedisTemplate.opsForHash().get(redisKey,KEY_TOKEN_INFO), AccessTokenInfo.class));
        sessionInfo.setYhxx(JsonUtils.toBean((String) stringRedisTemplate.opsForHash().get(redisKey,KEY_YHXX), YhInfo.class));

        final String yhlx = (String) stringRedisTemplate.opsForHash().get(redisKey, KEY_YHLX);
        if (!GyUtils.isNull(yhlx)){
            sessionInfo.setCurrentYhlx(yhlx);
        }
        sessionInfo.setQyuuid((String) stringRedisTemplate.opsForHash().get(redisKey, KEY_QYUUID));
        sessionInfo.setQymc((String) stringRedisTemplate.opsForHash().get(redisKey, KEY_QYMC));
        sessionInfo.setNsrsbh((String) stringRedisTemplate.opsForHash().get(redisKey, KEY_NSRSBH));
        return sessionInfo;
    }

    public void set(SessionInfo sessionInfo){
        String redisKey = formatKey(sessionInfo.getTokenInfo().getAccessToken());
        long time = LocalDateTimeUtil.between(LocalDateTime.now(), sessionInfo.getTokenInfo().getGqsj(), ChronoUnit.SECONDS);
        if (time > 0) {
            stringRedisTemplate.opsForHash().put(redisKey,KEY_TOKEN_INFO,JsonUtils.toJson(sessionInfo.getTokenInfo()));
//            stringRedisTemplate.opsForHash().put(redisKey,KEY_JGXX,GyUtils.isNull(sessionInfo.getQyxx())?"":JsonUtils.toJson(sessionInfo.getQyxx()));
            stringRedisTemplate.opsForHash().put(redisKey,KEY_YHXX,GyUtils.isNull(sessionInfo.getYhxx())?"":JsonUtils.toJson(sessionInfo.getYhxx()));
            stringRedisTemplate.opsForHash().put(redisKey,KEY_YHLX,GyUtils.isNull(sessionInfo.getCurrentYhlx())?"":sessionInfo.getCurrentYhlx());
            stringRedisTemplate.opsForHash().put(redisKey,KEY_QYUUID,GyUtils.isNull(sessionInfo.getQyuuid())?"":sessionInfo.getQyuuid());
            stringRedisTemplate.opsForHash().put(redisKey,KEY_QYMC,GyUtils.isNull(sessionInfo.getQymc())?"":sessionInfo.getQymc());
            stringRedisTemplate.opsForHash().put(redisKey,KEY_NSRSBH,GyUtils.isNull(sessionInfo.getNsrsbh())?"":sessionInfo.getNsrsbh());
            stringRedisTemplate.opsForHash().getOperations().expire(redisKey,time, TimeUnit.SECONDS);
        }
    }

    public void set(AccessTokenInfo accessTokenInfo){
        String redisKey = formatKey(accessTokenInfo.getAccessToken());
        long time = LocalDateTimeUtil.between(LocalDateTime.now(), accessTokenInfo.getGqsj(), ChronoUnit.SECONDS);
        if (time > 0) {
            stringRedisTemplate.opsForHash().put(redisKey,KEY_TOKEN_INFO,JsonUtils.toJson(accessTokenInfo));
            stringRedisTemplate.opsForHash().getOperations().expire(redisKey,time, TimeUnit.SECONDS);
        }
    }

    public void updateAccessTokenInfo(OAuth2AccessTokenDO tokenDO){
        final AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
        accessTokenInfo.setAccessToken(tokenDO.getAccessToken());
        accessTokenInfo.setRefreshToken(tokenDO.getRefreshToken());
        accessTokenInfo.setUuid(tokenDO.getUuid());
        accessTokenInfo.setYhUuid(tokenDO.getYhUuid());
        accessTokenInfo.setQyuuid(tokenDO.getQyuuid());
        accessTokenInfo.setGqsj(tokenDO.getGqsj());
        accessTokenInfo.setClientid(tokenDO.getClientid());
        accessTokenInfo.setSqnr(tokenDO.getSqnr());
        accessTokenInfo.setGllp(tokenDO.getGllp());
        String redisKey = formatKey(accessTokenInfo.getAccessToken());
        stringRedisTemplate.opsForHash().put(redisKey,KEY_TOKEN_INFO,JsonUtils.toJson(accessTokenInfo));
    }

    public void refreshQyxx(String accessToken, String qyuuid,String qymc,String nsrsbh){
        String redisKey = formatKey(accessToken);
        stringRedisTemplate.opsForHash().put(redisKey,KEY_QYUUID,qyuuid);
        stringRedisTemplate.opsForHash().put(redisKey,KEY_QYMC,qymc);
        stringRedisTemplate.opsForHash().put(redisKey,KEY_NSRSBH,nsrsbh);
    }

//    public void refreshCompany(String accessToken, CktsMhqxQyxxbDO jgxx,List<String> kczDjxhList,String swjgDm,List<String> jsuuidList){
//        String redisKey = formatKey(accessToken);
//
//
//        if (GyUtils.isNull(jgxx)){
//            stringRedisTemplate.opsForHash().put(redisKey,KEY_JGXX,"");
//        }else {
//            JgInfo jgInfo = new JgInfo();
//            jgInfo.setQyuuid(jgxx.getQyuuid());
//            jgInfo.setQymc(jgxx.getQymc());
//            jgInfo.setNsrsbh(jgxx.getNsrsbh());
//            jgInfo.setShxydm(jgxx.getShxydm());
//            jgInfo.setDjxh(jgxx.getDjxh());
//            stringRedisTemplate.opsForHash().put(redisKey,KEY_JGXX,JsonUtils.toJson(jgInfo));
//        }
//
//        if (GyUtils.isNull(kczDjxhList)){
//            stringRedisTemplate.opsForHash().put(redisKey,KEY_KZCDJXHLIST,"");
//        }else {
//            stringRedisTemplate.opsForHash().put(redisKey,KEY_KZCDJXHLIST,JsonUtils.toJson(kczDjxhList));
//        }
//        if (GyUtils.isNull(jsuuidList)){
//            stringRedisTemplate.opsForHash().put(redisKey,KEY_JSUUIDLIST,"");
//        }else {
//            stringRedisTemplate.opsForHash().put(redisKey,KEY_JSUUIDLIST,JsonUtils.toJson(jsuuidList));
//        }
//        stringRedisTemplate.opsForHash().put(redisKey,KEY_SWJGDM,GyUtils.isNull(swjgDm)?"":swjgDm);
//        final AccessTokenInfo tokenInfo = JsonUtils.toBean((String) stringRedisTemplate.opsForHash().get(redisKey, KEY_TOKEN_INFO), AccessTokenInfo.class);
//        tokenInfo.setQyuuid(GyUtils.isNull(jgxx)?"":jgxx.getQyuuid());
//        stringRedisTemplate.opsForHash().put(redisKey,KEY_TOKEN_INFO,JsonUtils.toJson(tokenInfo));
//    }


    public void delete(String accessToken) {
        String redisKey = formatKey(accessToken);
        stringRedisTemplate.delete(redisKey);
    }

    public void deleteList(Collection<String> accessTokens) {
        List<String> redisKeys = CollectionUtils.convertList(accessTokens, OAuth2AccessTokenRedisDAO::formatKey);
        stringRedisTemplate.delete(redisKeys);
    }

    private static String formatKey(String accessToken) {
        String OAUTH2_ACCESS_TOKEN = "oauth2_access_token:%s";
        return String.format(OAUTH2_ACCESS_TOKEN, accessToken);
    }
}
