package com.cico.nest.lcdp.boot.core.qxsd.adapter.web;

import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.event.EventDetailDispelReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.event.EventDetailPageReqVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.event.EventDetailRespVO;
import com.cico.nest.lcdp.boot.core.qxsd.adapter.web.vo.event.EventHisRespVO;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.EventDetailService;
import com.cico.nest.lcdp.boot.core.qxsd.application.service.HistoryService;
import com.cico.nest.lcdp.boot.core.qxsd.domain.gateway.EventGateway;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.constatnt.XlwConstants;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.result.XlwResultData;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.EventDetailMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.database.dataobject.EventDetailDO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.ISdgkApiService;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.sdgk.dto.SdgkEventAlarmDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwBayonetPictureDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwBayonetPictureSearchDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwPageDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.feign.XlwClientFeign;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.sdgk.dto.SdgkEventAlarmResourceDTO;
import com.cico.nest.lcdp.framework.common.exception.ServiceException;
import com.cico.nest.lcdp.framework.common.pojo.CommonResult;
import com.cico.nest.lcdp.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

import static com.cico.nest.lcdp.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
import static com.cico.nest.lcdp.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 事件详情")
@RestController
@RequestMapping("/qxsd/event-detail")
@Validated
public class EventDetailController {

    @Resource
    private EventDetailService eventDetailService;
    @Resource
    private EventGateway eventGateway;
    @Resource
    private XlwClientFeign xlwClientFeign;
    @Resource
    private HistoryService historyService;
    @Resource
    private EventDetailMapper eventDetailMapper;
    @Value("${qxsd.xlw.images}")
    private String xlwImagePath;
    @Resource
    private ISdgkApiService sdgkApiService;

    @GetMapping("/get")
    @Operation(summary = "获得事件详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<EventDetailRespVO> getEventDetail(@RequestParam("id") String id) {
        EventDetailDO eventDetail = eventDetailService.getEventDetail(id);
        return success(EventDetailDO.convert2VO(eventDetail,xlwImagePath));
    }

    @PostMapping("/page")
    @Operation(summary = "获得事件详情分页")
    public CommonResult<PageResult<EventDetailRespVO>> getEventDetailPage(@Valid @RequestBody EventDetailPageReqVO pageReqVO) {
        PageResult pageResult = eventDetailService.getEventDetailPage(pageReqVO);
        pageResult.setList(EventDetailDO.convert2VO(pageResult.getList(),xlwImagePath));
        return success(pageResult);
    }

    @PostMapping("/dispelEvent")
    @Operation(summary = "事件消除")
    public CommonResult<String> dispelEvent(@Valid @RequestBody EventDetailDispelReqVO reqVO){
        eventDetailService.dispelEvent(reqVO.getId(),reqVO.getDispelCause());
        return CommonResult.success("操作成功");
    }

    @GetMapping("/report")
    @Operation(summary = "事件上报给隧道管控")
    public CommonResult<String> reportEvent(@RequestParam("id") String id){
        EventDetailDO eventDetail = eventDetailService.getEventDetail(id);
        if (eventDetail==null){
            throw new ServiceException(INTERNAL_SERVER_ERROR.getCode(),"事件不存在。");
        }
        if (eventDetail.getEventSource()== XlwConstants.EVENT_SOURCE_SDGK){
            throw new ServiceException(INTERNAL_SERVER_ERROR.getCode(),"该事件来源属于隧道管控，不需要上报。");
        }
        eventDetailService.reporEventToSdgk(eventDetail);
        return CommonResult.success("操作成功");
    }

    @GetMapping("/syncEventDetail")
    @Operation(summary = "同步指定时间内的事件")
    public CommonResult<String> syncEventDetail(@RequestParam("startDateTime") Date startDateTime,@RequestParam("endDateTime") Date endDateTime){
        eventGateway.syncEventDetailList(startDateTime,endDateTime);
        return CommonResult.success("操作成功");
    }


    @PostMapping("/getBayonetPicture")
    @Operation(summary = "获取卡口图片")
    public CommonResult<PageResult<XlwBayonetPictureDTO>> getBayonetPicture(@RequestBody XlwBayonetPictureSearchDTO searchDTO){
        XlwResultData<XlwPageDTO<XlwBayonetPictureDTO>> resultData = xlwClientFeign.getBayonetPicture(searchDTO);
        PageResult<XlwBayonetPictureDTO> pageResult = new PageResult<>();
        XlwPageDTO<XlwBayonetPictureDTO> xlwPageDTO = resultData.getData();
        pageResult.setList(xlwPageDTO.getRows());
        pageResult.setTotal(xlwPageDTO.getTotal());
        return CommonResult.success(pageResult);
    }

    @GetMapping("/eventHis")
    @Operation(summary = "获取事件的历史数据")
    public CommonResult<EventHisRespVO> getEventHisDetail(@RequestParam("eventId") String eventId){
        return CommonResult.success(eventDetailService.getEventHisDetail(eventId));
    }

    @GetMapping("/saveEventHis")
    public CommonResult<String> saveEventHis(@RequestParam String eventId){
        EventDetailDO eventDetailDO = eventDetailMapper.selectById(eventId);
        historyService.saveEventHistoryData(eventDetailDO);
        return success("操作成功");
    }

    @GetMapping("/sdgkAlarm")
    @Operation(summary = "获取隧道管控告警事件详情")
    public CommonResult<SdgkEventAlarmDTO> getSdgkAlarm(@RequestParam String alarmId){
        SdgkEventAlarmDTO result = sdgkApiService.alarmDetail(alarmId);
        return success(result);
    }

    @GetMapping("/sdgkAlarm/resource")
    @Operation(summary = "获取隧道管控告警事件媒体资源")
    public CommonResult<SdgkEventAlarmResourceDTO> getSdgkAlarmResource(@RequestParam String alarmId){
        SdgkEventAlarmResourceDTO result = sdgkApiService.alarmResource(alarmId);
        return success(result);
    }

}
