package com.friday.distributor.queue;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.friday.distributor.constant.DistributorConstant;
import com.google.common.collect.Lists;
import com.friday.distributor.bean.Task;
import com.friday.operation.api.distributor.dto.ClearQueueDto;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author YaoYao
 * @Date Created In 10:47 2023/03/07
 * @Description 队列缓存相关
 * @Modified By
 */

@Service
@Slf4j
public class TaskRedisHelper {
    @Autowired
    private RedissonClient redissonClient;

    @Value("${spring.profiles.active}")
    private String env;

    private String getTaskQueueCacheKey(){
        String envTag = env;
        if("local".equals(envTag)){
            envTag = "dev";
        }
        return String.join("-", DistributorConstant.TaskQueueCacheKey, envTag);
    }

    public void setTaskData(String qKey, List<Task> value) {
        String taskQueueStr = JSON.toJSONString(value, SerializerFeature.WriteClassName, SerializerFeature.DisableCircularReferenceDetect);
        RMap<String, String> taskQueueMap = this.redissonClient.getMap(getTaskQueueCacheKey());
        taskQueueMap.put(qKey, taskQueueStr);
    }

    public List<Task> getTaskData(String qKey) {
        RMap<String, String> taskQueueMap = this.redissonClient.getMap(getTaskQueueCacheKey());
        if(Objects.isNull(taskQueueMap.get(qKey))){
            return Lists.newArrayList();
        }
        return JSON.parseArray(taskQueueMap.get(qKey), Task.class);
    }

    public void removeTaskData(String qKey){
        RMap<String, String> taskQueueMap = this.redissonClient.getMap(getTaskQueueCacheKey());
        if(Objects.nonNull(taskQueueMap.get(qKey))){
            taskQueueMap.remove(qKey);
        }
    }

    public void clearTaskData(ClearQueueDto clearQueueDto){
        List<Task> tasks = this.getTaskData(clearQueueDto.getKey());
        if(!CollectionUtils.isEmpty(clearQueueDto.getTaskTypes())){
            tasks = tasks.stream().filter(t -> !clearQueueDto.getTaskTypes().contains(t.getTaskType())).collect(Collectors.toList());
        }
        if(!CollectionUtils.isEmpty(clearQueueDto.getTaskIds())){
            tasks = tasks.stream().filter(t -> !clearQueueDto.getTaskIds().contains(t.getTaskId())).collect(Collectors.toList());
        }
        if(CollectionUtils.isEmpty(clearQueueDto.getTaskTypes()) && CollectionUtils.isEmpty(clearQueueDto.getTaskIds())){
            tasks = Lists.newArrayList();
        }
        this.setTaskData(clearQueueDto.getKey(), tasks);
    }

    public boolean queueExist(String qKey){
        RMap<String, String> taskQueueMap = this.redissonClient.getMap(getTaskQueueCacheKey());
        return Objects.nonNull(taskQueueMap) && taskQueueMap.containsKey(qKey);
    }
}
