package com.fuyuanshen.web.controller;

import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.equipment.domain.vo.*;
import com.fuyuanshen.equipment.service.IDeviceAlarmService;
import com.fuyuanshen.equipment.service.DeviceService;
import com.fuyuanshen.equipment.service.IWeatherService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 大屏数据
 *
 * @author: 默苍璃
 * @date: 2025-09-27 08:42
 */
@Tag(name = "大屏数据", description = "大屏数据")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/largeScreen")
public class LargeScreenController {

    private final IDeviceAlarmService deviceAlarmService;
    private final DeviceService deviceService;
    private final IWeatherService weatherService;


    /**
     * 获取设备总览信息
     * 包含设备总数、在线设备数量、设备型号数量
     */
    @GetMapping("/getDeviceOverview")
    public R<DeviceOverviewVo> getDeviceOverview() {
        return R.ok(deviceService.getDeviceOverview());
    }


    /**
     * 获取 实时报警信息
     * RealtimeAlarm
     */
    @GetMapping("/getRealtimeAlarm")
    public R<List<DeviceAlarmVo>> getRealtimeAlarm() {
        return R.ok(deviceAlarmService.getRealtimeAlarm());
    }


    /**
     * 获取报警统计数据
     * 包括正在报警数量、报警总数、已处理报警数量
     */
    @GetMapping("/getAlarmStatistics")
    public R<AlarmStatisticsVo> getAlarmStatistics() {
        return R.ok(deviceAlarmService.getAlarmStatistics());
    }


    /**
     * 获取最近一年每月告警统计数据
     */
    @GetMapping("/getMonthlyAlarmStatistics")
    public R<MonthlyAlarmStatisticsVo> getMonthlyAlarmStatistics() {
        return R.ok(deviceAlarmService.getMonthlyAlarmStatistics());
    }


    /**
     * 获取设备通讯方式统计数据
     * 包含通讯方式名称、设备总数、异常设备数
     */
    @GetMapping("/getDeviceCommunicationModeStatistics")
    public R<List<DeviceCommunicationModeStatisticsVo>> getDeviceCommunicationModeStatistics() {
        return R.ok(deviceService.getDeviceCommunicationModeStatistics());
    }


    /**
     * 获取设备使用频次统计数据
     *
     * @param days 天数（近一个月传30，近半年传180）
     */
    @GetMapping("/getDeviceUsageFrequency")
    public R<List<DeviceUsageFrequencyVo>> getDeviceUsageFrequency(@RequestParam int days) {
        return R.ok(deviceService.getDeviceUsageFrequency(days));
    }


    /**
     * 根据条件查询设备位置信息
     *
     * @param groupId    设备分组ID
     * @param deviceType 设备类型
     * @param deviceImei 设备IMEI
     * @return 设备位置信息列表
     */
    @GetMapping("/getDeviceLocationInfo")
    public R<List<DeviceLocationVo>> getDeviceLocationInfo(
            @RequestParam(required = false) Long groupId,
            @RequestParam(required = false) Long deviceType,
            @RequestParam(required = false) String deviceImei) {
        return R.ok(deviceService.getDeviceLocationInfo(groupId, deviceType, deviceImei));
    }


    /**
     * 根据经纬度获取天气信息
     *
     * @param latitude  纬度
     * @param longitude 经度
     * @return 天气信息
     */
    @GetMapping("/weather")
    public R<WeatherInfoVo> getWeatherInfo(
            @RequestParam Double latitude,
            @RequestParam Double longitude) {
        if (latitude == null || longitude == null) {
            return R.fail("经纬度参数不能为空");
        }

        // 简单的经纬度范围校验
        if (latitude < -90 || latitude > 90 || longitude < -180 || longitude > 180) {
            return R.fail("经纬度参数范围不正确");
        }

        WeatherInfoVo weatherInfo = weatherService.getWeatherByCoordinates(latitude, longitude);
        if (weatherInfo != null) {
            return R.ok(weatherInfo);
        } else {
            return R.fail("获取天气信息失败");
        }
    }


}