package com.zksy.web.controller.monitor;

import com.zksy.base.domain.StationVideoDevice;
import com.zksy.base.service.EquipmentBaseService;
import com.zksy.base.service.StationVideoDeviceService;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/api/monitor")
@Api(tags = "实时监测看板")
public class RealtimeMonitorController {

    @Autowired
    private StationVideoDeviceService videoDeviceService;

    @Autowired
    private EquipmentBaseService equipmentService;

    @GetMapping("/pressure-history")
    @ApiOperation("压力历史曲线")
    @Anonymous
    public AjaxResult getPressureHistory(@RequestParam(required = false) String pointId,
                                          @RequestParam(defaultValue = "24") Integer hours) {
        // TODO: 从监测数据表获取真实压力历史数据
        List<Map<String, Object>> mockData = new ArrayList<>();
        long now = System.currentTimeMillis();
        for (int i = hours; i >= 0; i--) {
            Map<String, Object> point = new HashMap<>();
            point.put("time", System.currentTimeMillis() - i * 3600000L);
            point.put("value", 0.3 + Math.random() * 0.4);
            mockData.add(point);
        }
        return AjaxResult.success(mockData);
    }

    @GetMapping("/flow-history")
    @ApiOperation("流量历史曲线")
    @Anonymous
    public AjaxResult getFlowHistory(@RequestParam(required = false) String pointId,
                                      @RequestParam(defaultValue = "24") Integer hours) {
        // TODO: 从监测数据表获取真实流量历史数据
        List<Map<String, Object>> mockData = new ArrayList<>();
        for (int i = hours; i >= 0; i--) {
            Map<String, Object> point = new HashMap<>();
            point.put("time", System.currentTimeMillis() - i * 3600000L);
            point.put("value", 50 + Math.random() * 100);
            mockData.add(point);
        }
        return AjaxResult.success(mockData);
    }

    @GetMapping("/gas-alarm-latest")
    @ApiOperation("最新10条气体浓度报警")
    @Anonymous
    public AjaxResult getGasAlarmLatest() {
        // TODO: 从alarm_data表获取最新报警数据
        return AjaxResult.success(new ArrayList<>());
    }

    @GetMapping("/video-list")
    @ApiOperation("获取视频设备列表")
    @Anonymous
    public AjaxResult getVideoList() {
        return AjaxResult.success(videoDeviceService.list());
    }

    @GetMapping("/points-with-latest-data")
    @ApiOperation("获取测点最新数据")
    @Anonymous
    public AjaxResult getPointsWithLatestData() {
        // TODO: 从监测数据表获取各测点最新数据
        List<Map<String, Object>> mockData = new ArrayList<>();
        return AjaxResult.success(mockData);
    }
}
