package com.usky.sas.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.sas.service.SasHomepageService;
import com.usky.sas.service.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
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;

import com.usky.sas.common.util.GetIpUtils;
import org.springframework.beans.factory.annotation.Value;

/**
 * 首页接口
 *
 * 对应 API 文档：/prod-api/service-sas/homepage/...
 * 功能：左上角摄像头列表与直播、左下角服务器信息、中上事件列表与实时事件、中下预警历史
 */
@Api(tags = "首页管理")
@RestController
@RequestMapping("/homepage")
public class SasHomepageController {

    @Value("${server.port}")
    private String port;

    @Autowired
    private SasHomepageService sasHomepageService;

    /**
     * 1. 查询首页综合信息（摄像头列表、统计、服务器资源使用情况）
     */
    @ApiOperation("查询首页综合信息")
    @GetMapping("/info")
    public ApiResult<HomepageInfoResponse> getInfo(@RequestParam(value = "ipAddr",required = false) String ipAddr) {
        return ApiResult.success(sasHomepageService.getHomepageInfo(ipAddr));
    }

    /**
     * 2. 事件列表：各模块所有事件分页（中上-事件列表区域）
     */
    @ApiOperation("查询事件列表")
    @GetMapping("/events")
    public ApiResult<CommonPage<IntelligentEventItem>> getEventList(@Validated IntelligentEventPageRequest request) {
        return ApiResult.success(sasHomepageService.getEventList(request));
    }

    /**
     * 3. 实时事件：开启通知的最近事件，用于弹窗（左边关联摄像头、右边事件信息，可提前关闭）
     */
    @ApiOperation("查询实时通知事件")
    @GetMapping("/realtimeNotificationEvents")
    public ApiResult<List<IntelligentEventItem>> getRealtimeNotificationEvents() {
        return ApiResult.success(sasHomepageService.getRealtimeNotificationEvents());
    }

    /**
     * 4. 预警历史：按分类显示开启通知的事件预警历史，可分页、点详情
     */
    @ApiOperation("查询预警历史")
    @GetMapping("/alertHistory")
    public ApiResult<CommonPage<IntelligentEventItem>> getAlertHistory(@Validated HomepageAlertHistoryRequest request) {
        return ApiResult.success(sasHomepageService.getAlertHistory(request));
    }

    /**
     * 5. 系统资源信息：CPU、内存、磁盘、Agbox/MetaBus状态（左下角系统信息模块）
     */
    @ApiOperation("查询系统资源信息")
    @GetMapping("/systemResource")
    public ApiResult<SystemResourceResponse> getSystemResource() {
        return ApiResult.success(sasHomepageService.getSystemResource());
    }

    /**
     * 6. 事件详情：根据事件类型和事件ID查询事件详细信息
     */
    @ApiOperation("查询事件详情")
    @GetMapping("/eventDetail")
    public ApiResult<EventDetailResponse> getEventDetail(
            @ApiParam(value = "事件类型", required = true, example = "1001")
            @RequestParam String deviceType,
            @ApiParam(value = "事件ID", required = true, example = "123456") 
            @RequestParam String eventId) {
        return ApiResult.success(sasHomepageService.getEventDetail(deviceType, eventId));
    }

    /**
     * 7. 实时视频流
     */
    @ApiOperation("获取实时视频流")
    @GetMapping("/videoStream")
    public ApiResult<String> getUnityVideoStream(
            @ApiParam(value = "设备ID") @RequestParam(required = false) String id,
            @ApiParam(value = "设备编号") @RequestParam(required = false) String deviceId,
            @ApiParam(value = "通道号") @RequestParam(required = false) Integer channel,
            @ApiParam(value = "设备类型") @RequestParam(required = false) Integer deviceType) {
        return ApiResult.success(sasHomepageService.getUnityVideoStream(id, deviceId, channel, deviceType));
    }

    /**
     * 8. 获取IP地址
     */
    @ApiOperation("获取服务器IP地址和端口")
    @GetMapping({"/getIpAddr"})
    public ApiResult<String> getIpAddr() {
        return ApiResult.success(GetIpUtils.getServerIP() + ":" + this.port);
    }

//    /**
//     * 8. 回放视频流
//     */
//    @ApiOperation("获取回放视频流")
//    @GetMapping("/videoBackStream")
//    public ApiResult<VideoStreamVo> getUnityVideoBackStream(
//            @ApiParam(value = "设备ID") @RequestParam(required = false) String id,
//            @ApiParam(value = "开始时间") @RequestParam String beginTime,
//            @ApiParam(value = "结束时间") @RequestParam String endTime,
//            @ApiParam(value = "句柄") @RequestParam(required = false) Integer handle,
//            @ApiParam(value = "旧Mac地址") @RequestParam(required = false) String oldMac) {
//        return ApiResult.success(sasHomepageService.getUnityVideoBackStream(id, beginTime, endTime, handle, oldMac));
//    }
}
