package com.zksy.web.controller.base.alarm;

import com.zksy.base.alarm.domain.AlarmData;
import com.zksy.base.alarm.service.AlarmDataService;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/alarmTest")
@Api(tags = "报警测试", description = "模拟采集数据和报警测试")
public class AlarmTestController {

    @Autowired
    private AlarmDataService alarmDataService;

    private Random random = new Random();

    @GetMapping("/simulateManhole")
    @ApiOperation(value = "模拟井盖设备采集数据并报警")
    public AjaxResult simulateManhole() {
        String deviceCode = "TEST-MANHOLE-001";

        double temperature = 30 + random.nextDouble() * 40;
        double tiltAngle = 0 + random.nextDouble() * 90;
        double threshold = 60.0;

        Map<String, Object> result = new HashMap<>();
        result.put("deviceCode", deviceCode);
        result.put("temperature", temperature);
        result.put("tiltAngle", tiltAngle);
        result.put("threshold", threshold);

        if (temperature > 50) {
            AlarmData alarmData = new AlarmData();
            alarmData.setDeviceCode(deviceCode);
            alarmData.setWarningType("温度预警");
            alarmData.setWarningCode("WARN-TEMPERATURE");
            alarmData.setMaxValue(BigDecimal.valueOf(50.0));
            alarmData.setActualValue(BigDecimal.valueOf(temperature));
            alarmData.setAlarmStatus(0);
            alarmData.setAlarmTime(LocalDateTime.now());
            alarmData.setRemark("模拟井盖温度报警");
            alarmData.setCreateTime(LocalDateTime.now());
            alarmDataService.saveAlarmData(alarmData);
            result.put("temperatureAlarm", true);
        } else {
            result.put("temperatureAlarm", false);
        }

        if (tiltAngle > threshold) {
            AlarmData alarmData = new AlarmData();
            alarmData.setDeviceCode(deviceCode);
            alarmData.setWarningType("倾斜预警");
            alarmData.setWarningCode("WARN-TILT");
            alarmData.setMaxValue(BigDecimal.valueOf(threshold));
            alarmData.setActualValue(BigDecimal.valueOf(tiltAngle));
            alarmData.setAlarmStatus(0);
            alarmData.setAlarmTime(LocalDateTime.now());
            alarmData.setRemark("模拟井盖倾斜报警");
            alarmData.setCreateTime(LocalDateTime.now());
            alarmDataService.saveAlarmData(alarmData);
            result.put("tiltAlarm", true);
        } else {
            result.put("tiltAlarm", false);
        }

        return AjaxResult.success(result);
    }

    @GetMapping("/simulateGas")
    @ApiOperation(value = "模拟可燃气体设备采集数据并报警")
    public AjaxResult simulateGas() {
        String deviceCode = "TEST-GAS-001";

        float temperature = 20 + random.nextFloat() * 40;
        float humidity = 40 + random.nextFloat() * 40;
        BigDecimal gasConcentration = BigDecimal.valueOf(random.nextDouble() * 100);

        Map<String, Object> result = new HashMap<>();
        result.put("deviceCode", deviceCode);
        result.put("temperature", temperature);
        result.put("humidity", humidity);
        result.put("gasConcentration", gasConcentration);

        if (temperature > 50) {
            AlarmData alarmData = new AlarmData();
            alarmData.setDeviceCode(deviceCode);
            alarmData.setWarningType("温度预警");
            alarmData.setWarningCode("WARN-TEMPERATURE");
            alarmData.setMaxValue(BigDecimal.valueOf(50.0));
            alarmData.setActualValue(BigDecimal.valueOf(temperature));
            alarmData.setAlarmStatus(0);
            alarmData.setAlarmTime(LocalDateTime.now());
            alarmData.setRemark("模拟可燃气体温度报警");
            alarmData.setCreateTime(LocalDateTime.now());
            alarmDataService.saveAlarmData(alarmData);
            result.put("temperatureAlarm", true);
        } else {
            result.put("temperatureAlarm", false);
        }

        if (humidity > 80) {
            AlarmData alarmData = new AlarmData();
            alarmData.setDeviceCode(deviceCode);
            alarmData.setWarningType("湿度预警");
            alarmData.setWarningCode("WARN-HUMIDITY");
            alarmData.setMaxValue(BigDecimal.valueOf(80.0));
            alarmData.setActualValue(BigDecimal.valueOf(humidity));
            alarmData.setAlarmStatus(0);
            alarmData.setAlarmTime(LocalDateTime.now());
            alarmData.setRemark("模拟可燃气体湿度报警");
            alarmData.setCreateTime(LocalDateTime.now());
            alarmDataService.saveAlarmData(alarmData);
            result.put("humidityAlarm", true);
        } else {
            result.put("humidityAlarm", false);
        }

        if (gasConcentration.doubleValue() > 50) {
            AlarmData alarmData = new AlarmData();
            alarmData.setDeviceCode(deviceCode);
            alarmData.setWarningType("气体浓度预警");
            alarmData.setWarningCode("WARN-GAS-CONCENTRATION");
            alarmData.setMaxValue(BigDecimal.valueOf(50.0));
            alarmData.setActualValue(gasConcentration);
            alarmData.setAlarmStatus(0);
            alarmData.setAlarmTime(LocalDateTime.now());
            alarmData.setRemark("模拟可燃气体浓度报警");
            alarmData.setCreateTime(LocalDateTime.now());
            alarmDataService.saveAlarmData(alarmData);
            result.put("gasConcentrationAlarm", true);
        } else {
            result.put("gasConcentrationAlarm", false);
        }

        return AjaxResult.success(result);
    }

    @GetMapping("/simulateEnvironment")
    @ApiOperation(value = "模拟环境设备采集数据并报警")
    public AjaxResult simulateEnvironment() {
        String deviceCode = "TEST-ENV-001";

        double temperature = 15 + random.nextDouble() * 50;
        double humidity = 30 + random.nextDouble() * 60;

        Map<String, Object> result = new HashMap<>();
        result.put("deviceCode", deviceCode);
        result.put("temperature", temperature);
        result.put("humidity", humidity);

        if (temperature > 50) {
            AlarmData alarmData = new AlarmData();
            alarmData.setDeviceCode(deviceCode);
            alarmData.setWarningType("温度预警");
            alarmData.setWarningCode("WARN-TEMPERATURE");
            alarmData.setMaxValue(BigDecimal.valueOf(50.0));
            alarmData.setActualValue(BigDecimal.valueOf(temperature));
            alarmData.setAlarmStatus(0);
            alarmData.setAlarmTime(LocalDateTime.now());
            alarmData.setRemark("模拟环境温度报警");
            alarmData.setCreateTime(LocalDateTime.now());
            alarmDataService.saveAlarmData(alarmData);
            result.put("temperatureAlarm", true);
        } else {
            result.put("temperatureAlarm", false);
        }

        if (humidity > 90) {
            AlarmData alarmData = new AlarmData();
            alarmData.setDeviceCode(deviceCode);
            alarmData.setWarningType("湿度预警");
            alarmData.setWarningCode("WARN-HUMIDITY");
            alarmData.setMaxValue(BigDecimal.valueOf(90.0));
            alarmData.setActualValue(BigDecimal.valueOf(humidity));
            alarmData.setAlarmStatus(0);
            alarmData.setAlarmTime(LocalDateTime.now());
            alarmData.setRemark("模拟环境湿度报警");
            alarmData.setCreateTime(LocalDateTime.now());
            alarmDataService.saveAlarmData(alarmData);
            result.put("humidityAlarm", true);
        } else {
            result.put("humidityAlarm", false);
        }

        return AjaxResult.success(result);
    }

    @PostMapping("/customAlarm")
    @ApiOperation(value = "自定义报警测试")
    public AjaxResult customAlarm(@RequestBody Map<String, Object> params) {
        AlarmData alarmData = new AlarmData();
        alarmData.setDeviceCode((String) params.get("deviceCode"));
        alarmData.setWarningType((String) params.get("warningType"));
        alarmData.setWarningCode((String) params.get("warningCode"));
        
        Object minValueObj = params.get("minValue");
        if (minValueObj != null) {
            alarmData.setMinValue(new BigDecimal(minValueObj.toString()));
        }
        
        Object maxValueObj = params.get("maxValue");
        if (maxValueObj != null) {
            alarmData.setMaxValue(new BigDecimal(maxValueObj.toString()));
        }
        
        Object actualValueObj = params.get("actualValue");
        if (actualValueObj != null) {
            alarmData.setActualValue(new BigDecimal(actualValueObj.toString()));
        }
        
        alarmData.setAlarmStatus(0);
        alarmData.setAlarmTime(LocalDateTime.now());
        alarmData.setRemark((String) params.getOrDefault("remark", "自定义测试报警"));
        alarmData.setCreateTime(LocalDateTime.now());
        
        boolean success = alarmDataService.saveAlarmData(alarmData);
        return success ? AjaxResult.success(alarmData) : AjaxResult.error("保存失败");
    }
}
