package com.xindazhou.vehicle.service.impl;

import com.xindazhou.framework.mq.redis.core.RedisMQTemplate;
import com.xindazhou.vehicle.api.dto.control.VehicleControlReqDTO;
import com.xindazhou.vehicle.api.dto.mq.VehicleAlarmMessage;
import com.xindazhou.vehicle.service.VehicleControlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 车辆控制 Service 实现类
 *
 * @author xindazhou
 */
@Service
@Slf4j
public class VehicleControlServiceImpl implements VehicleControlService {

    @Resource
    private RedisMQTemplate redisMQTemplate;

    @Override
    public void controlVehicle(VehicleControlReqDTO reqDTO) {
        log.info("控制车辆，请求参数：{}", reqDTO);
        
        // TODO: 实现车辆控制逻辑（调用TSP或硬件接口）
        // 这里模拟控制成功后的报警推送
        
        // 如果控制失败或发生报警，发送报警消息到Redis MQ
        // 示例：模拟车辆报警
        if (shouldSendAlarm(reqDTO)) {
            sendAlarmMessage(reqDTO);
        }
    }

    /**
     * 判断是否需要发送报警
     * 这里只是示例，实际应该根据业务逻辑判断
     */
    private boolean shouldSendAlarm(VehicleControlReqDTO reqDTO) {
        // 示例：某些指令可能会触发报警
        // 实际应该根据TSP返回的结果或车辆状态判断
        return false; // 默认不发送，实际使用时根据业务逻辑修改
    }

    /**
     * 发送报警消息到Redis MQ
     */
    private void sendAlarmMessage(VehicleControlReqDTO reqDTO) {
        try {
            VehicleAlarmMessage message = new VehicleAlarmMessage();
            message.setUserId(reqDTO.getUserId());
            message.setVehicleId(reqDTO.getVehicleId());
            message.setAlarmType("vehicle_control_alarm");
            
            Map<String, Object> alarmData = new HashMap<>();
            alarmData.put("command", reqDTO.getCommand());
            alarmData.put("params", reqDTO.getParams());
            alarmData.put("message", "车辆控制异常");
            message.setAlarmData(alarmData);
            message.setTimestamp(System.currentTimeMillis());
            
            // 发送到Redis MQ
            redisMQTemplate.send(message);
            log.info("车辆报警消息已发送到Redis MQ: userId={}, vehicleId={}, alarmType={}", 
                    reqDTO.getUserId(), reqDTO.getVehicleId(), message.getAlarmType());
        } catch (Exception e) {
            log.error("发送车辆报警消息失败: userId={}, vehicleId={}", 
                    reqDTO.getUserId(), reqDTO.getVehicleId(), e);
        }
    }

    /**
     * 发送报警消息（供外部调用）
     * 
     * @param userId 用户ID
     * @param vehicleId 车辆ID
     * @param alarmType 报警类型
     * @param alarmData 报警数据
     */
    public void sendAlarmMessage(Long userId, Long vehicleId, String alarmType, Map<String, Object> alarmData) {
        try {
            VehicleAlarmMessage message = new VehicleAlarmMessage();
            message.setUserId(userId);
            message.setVehicleId(vehicleId);
            message.setAlarmType(alarmType);
            message.setAlarmData(alarmData);
            message.setTimestamp(System.currentTimeMillis());
            
            // 发送到Redis MQ
            redisMQTemplate.send(message);
            log.info("车辆报警消息已发送到Redis MQ: userId={}, vehicleId={}, alarmType={}", 
                    userId, vehicleId, alarmType);
        } catch (Exception e) {
            log.error("发送车辆报警消息失败: userId={}, vehicleId={}", userId, vehicleId, e);
        }
    }

}

