package com.tongran.frp.handler;

import com.tongran.common.core.enums.MsgEnum;
import com.tongran.frp.domain.DeviceMessage;
import com.tongran.frp.domain.vo.RspVo;
import com.tongran.frp.model.ProducerMode;
import com.tongran.frp.producer.MessageProducer;
import com.tongran.frp.utils.JsonDataParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * 设备消息处理器
 */
@Slf4j
@Component
@EnableScheduling
public class MessageHandler {

    private final Map<String, Consumer<DeviceMessage>> messageHandlers = new HashMap<>();
    // 心跳状态
    private static final String HEARTBEAT_STATUS_PREFIX = "mtr:heartbeat:status:";
    // 心跳时间
    private static final String HEARTBEAT_TIME_PREFIX = "mtr:heartbeat:time:";
    // 心跳告警
    private static final String HEARTBEAT_ALERT_PREFIX = "mtr:heartbeat:alert:";
    String HEARTBEAT_RECOVERY_COUNT_PREFIX = "mtr:heartbeat:recovery:count:";

    String HEARTBEAT_COUNT_PREFIX = "mtr:heartbeat:count:";
    private static final long HEARTBEAT_TIMEOUT = 30000; // 3分钟超时


    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ProducerMode producerMode;


    /**
     * 初始化处理器映射
     */
    @PostConstruct
    public void init() {
        registerHandler(MsgEnum.Agent版本更新应答.getValue(), this::handleAgentUpdateRspMessage);

        // 其他类型消息可以单独注册处理器
        registerHandler(MsgEnum.注册.getValue(), this::handleRegisterMessage);
        registerHandler(MsgEnum.获取最新策略.getValue(), this::handleNewPolicyMessage);
    }


    private void handleRegisterMessage(DeviceMessage message) {
        MessageProducer messageProducer = new MessageProducer();
    }
    /**
     * agent更新响应
     * @param message
     */
    private void handleAgentUpdateRspMessage(DeviceMessage message) {
        List<RspVo> rspVoList = JsonDataParser.parseJsonData(message.getData(), RspVo.class);
        if (!rspVoList.isEmpty()) {
            RspVo rsp = rspVoList.get(0);
            if(rsp.getResCode() == 1){
            }else{
            }
        }
    }

    /**
     * 注册消息处理器
     */
    private void registerHandler(String dataType, Consumer<DeviceMessage> handler) {
        messageHandlers.put(dataType, handler);
    }

    /**
     * 处理设备消息（对外暴露的主方法）
     */
    public void handleMessage(DeviceMessage message) {
        String dataType = message.getDataType();
        Consumer<DeviceMessage> handler = messageHandlers.get(dataType);

        if (handler != null) {
            handler.accept(message);
        } else {
            log.warn("未知数据类型：{}", dataType);
        }
    }

    // ========== 具体的消息处理方法 ==========
    /**
     * 获取最新策略
     * @param deviceMessage
     */
    private void handleNewPolicyMessage(DeviceMessage deviceMessage) {
        MessageProducer messageProducer = new MessageProducer();
    }


    // 更新资源状态的公共方法
    private void updateResourceStatus(String clientId, String status) {
        log.info("开启更新资源状态========");
    }

}