package com.td.WaDa.util;

import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.td.WaDa.game.comsetting.service.ComSettingService;
import com.td.WaDa.game.comsetting.vo.ComSettingVo;
import com.td.WaDa.sys.syssetting.service.SysSettingService;
import com.td.WaDa.sys.syssetting.vo.SysSettingVo;
import com.td.WaDa.sys.syssettinglimit.service.SysSettingLimitService;
import com.td.WaDa.sys.syssettinglimit.vo.SysSettingLimitVo;
import com.td.WaDa.sys.syssettingtask.service.SysSettingTaskService;
import com.td.WaDa.sys.syssettingtask.vo.SysSettingTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

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

/**
 *
 * 存储对应的配置
 * @author:slambb
 * @date:2022/4/19
 */

@Component
public class RedisSettingMap {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SysSettingService sysSettingService;

    @Autowired
    private SysSettingTaskService sysSettingTaskService;

    @Autowired
    private SysSettingLimitService sysSettingLimitService;

    @Autowired
    private ComSettingService comSettingService;

    //动态设置一个RSA key保存
    private String rsaPublicKey = "data:rsa:public";
    private String rsaPrivateKey = "data:rsa:private";
    //update 一次rsa数据
    public void updateRSAMap(){
        RSA rsa = new RSA();
        String privateKeyBase64 = rsa.getPrivateKeyBase64();
        String publicKeyBase64 = rsa.getPublicKeyBase64();
        redisTemplate.opsForValue().set(rsaPublicKey, JSONUtil.toJsonStr(publicKeyBase64));
        redisTemplate.opsForValue().set(rsaPrivateKey, JSONUtil.toJsonStr(privateKeyBase64));
    }
    //从redis 获取系统设置 rsa 设置
    public String getRSAPublicKeyBase64(){
        if (redisTemplate.hasKey(rsaPublicKey)) {
            return  redisTemplate.opsForValue().get(rsaPublicKey);
        } else {
            RSA rsa = new RSA();
            String privateKeyBase64 = rsa.getPrivateKeyBase64();
            String publicKeyBase64 = rsa.getPublicKeyBase64();
            redisTemplate.opsForValue().set(rsaPublicKey, JSONUtil.toJsonStr(publicKeyBase64));
            redisTemplate.opsForValue().set(rsaPrivateKey, JSONUtil.toJsonStr(privateKeyBase64));
            return publicKeyBase64;
        }
    }
    public String getRSAPrivateKeyBase64(){
        if (redisTemplate.hasKey(rsaPrivateKey)) {
            return  redisTemplate.opsForValue().get(rsaPrivateKey);
        } else {
            RSA rsa = new RSA();
            String privateKeyBase64 = rsa.getPrivateKeyBase64();
            String publicKeyBase64 = rsa.getPublicKeyBase64();
            redisTemplate.opsForValue().set(rsaPublicKey, JSONUtil.toJsonStr(publicKeyBase64));
            redisTemplate.opsForValue().set(rsaPrivateKey, JSONUtil.toJsonStr(privateKeyBase64));
            return privateKeyBase64;
        }
    }

    //系统设置key
    private String sysSettingKey = "data:sys:setting";
    //update 一次redis数据
    public void updateSysSettingMap(){
        SysSettingVo sysSettingVo = sysSettingService.get("1").getData();
        redisTemplate.opsForValue().set(sysSettingKey, JSONUtil.toJsonStr(sysSettingVo));
    }
    //从redis 获取系统设置 sysSettingMap
    public SysSettingVo getSysSettingMap(){
        SysSettingVo sysSettingVo;
        if (redisTemplate.hasKey(sysSettingKey)) {
            sysSettingVo = JSON.parseObject(redisTemplate.opsForValue().get(sysSettingKey),new TypeReference<SysSettingVo>(){});
        } else {
            sysSettingVo = sysSettingService.get("1").getData();
            redisTemplate.opsForValue().set(sysSettingKey, JSONUtil.toJsonStr(sysSettingVo));
        }
        return sysSettingVo;
    }
    //redis 设置 sysSettingMap
    public void setSysSettingMap(SysSettingVo sysSettingVo){
        redisTemplate.opsForValue().set(sysSettingKey, JSONUtil.toJsonStr(sysSettingVo));
    }
    //系统任务key
    private String sysSettingTaskKey = "data:sys:settingTask";
    //update 一次redis数据
    public void updateSysSettingTaskMap(){
        SysSettingTaskVo sysSettingTaskVo = sysSettingTaskService.get("1").getData();
        redisTemplate.opsForValue().set(sysSettingTaskKey, JSONUtil.toJsonStr(sysSettingTaskVo));
    }
    //从redis 获取系统设置 sysSettingTaskMap
    public SysSettingTaskVo getSysSettingTaskMap(){
        SysSettingTaskVo sysSettingTaskVo;
        if (redisTemplate.hasKey(sysSettingTaskKey)) {
            sysSettingTaskVo = JSON.parseObject(redisTemplate.opsForValue().get(sysSettingTaskKey),new TypeReference<SysSettingTaskVo>(){});
        } else {
            //系统启动时获取数据库数据，设置到公用静态集合sysSettingTaskMap
            sysSettingTaskVo = sysSettingTaskService.get("1").getData();
            redisTemplate.opsForValue().set(sysSettingTaskKey, JSONUtil.toJsonStr(sysSettingTaskVo));
        }
        return sysSettingTaskVo;
    }
    //设置SysSettingTask到Map
    public void setSysSettingTaskMap(SysSettingTaskVo sysSettingTaskVo){
        redisTemplate.opsForValue().set(sysSettingTaskKey, JSONUtil.toJsonStr(sysSettingTaskVo));
    }

    //限制登录ip key
    private String sysSettingLimitKey = "data:sys:settingLimit";
    //update 一次redis数据
    public void updateSysSettingLimitMap(){
        SysSettingLimitVo sysSettingLimitVo = sysSettingLimitService.get("1").getData();
        redisTemplate.opsForValue().set(sysSettingLimitKey, JSONUtil.toJsonStr(sysSettingLimitVo));;
    }
    //从redis 获取系统设置 sysSettingLimitMap
    public SysSettingLimitVo getSysSettingLimitMap(){
        SysSettingLimitVo sysSettingLimitVo;
        if (redisTemplate.hasKey(sysSettingLimitKey)) {
            sysSettingLimitVo = JSON.parseObject(redisTemplate.opsForValue().get(sysSettingLimitKey),new TypeReference<SysSettingLimitVo>(){});
        } else {
            sysSettingLimitVo = sysSettingLimitService.get("1").getData();
            redisTemplate.opsForValue().set(sysSettingLimitKey, JSONUtil.toJsonStr(sysSettingLimitVo));
        }
        return sysSettingLimitVo;
    }
    //设置SysSettingTask到Map
    public void setSysSettingLimitMap(SysSettingLimitVo sysSettingLimitVo){
        redisTemplate.opsForValue().set(sysSettingLimitKey, JSONUtil.toJsonStr(sysSettingLimitVo));
    }



    //游戏comSetting  key
    private String comSettingKey = "data:com:setting";
    //update 一次redis数据
    public void updateComSettingMap(){
        ComSettingVo comSettingVo = comSettingService.get("1").getData();
        redisTemplate.opsForValue().set(comSettingKey, JSONUtil.toJsonStr(comSettingVo));
    }
    //从redis 获取系统设置 comSettingKey
    public ComSettingVo getComSettingMap(){
        ComSettingVo comSettingVo;
        if (redisTemplate.hasKey(comSettingKey)) {
            comSettingVo = JSON.parseObject(redisTemplate.opsForValue().get(comSettingKey),new TypeReference<ComSettingVo>(){});
        } else {
            comSettingVo = comSettingService.get("1").getData();
            redisTemplate.opsForValue().set(comSettingKey, JSONUtil.toJsonStr(comSettingVo));
        }
        return comSettingVo;
    }
    //设置SysSettingTask到Map
    public void setComSettingMap(ComSettingVo comSettingVo){
        redisTemplate.opsForValue().set(comSettingKey, JSONUtil.toJsonStr(comSettingVo));
    }
}
