package com.shining.cloud.controller.information;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.njcn.enums.app.ReturnCode;
import com.njcn.pojo.commons.EventEigDetail;
import com.njcn.pojo.commons.EventInfoDetail;
import com.njcn.pojo.commons.ResponseData;
import com.njcn.pojo.commons.ResponseEigMsgData;
import com.njcn.pojo.commons.ResponseInfoData;
import com.njcn.pojo.commons.ResponseMsgData;
import com.njcn.utils.PubUtils;
import com.shining.cloud.enums.information.EventMsgCodeEnum;
import com.shining.cloud.pojo.information.EventMsgDetail;
import com.shining.cloud.pojo.information.EventWaveDetail;
import com.shining.cloud.service.information.EventInfoService;
import com.shining.cloud.service.information.EventMsgService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @description: 暂态消息业务层
 * @author: denghuajun
 * @time: 2019-10-17 14:33:55
 **/
@RestController
@RequestMapping("/eventmsg")
@Api(tags = "暂态消息接口")
public class EventMsgController {

    private static final Logger logger = LoggerFactory.getLogger(EventMsgController.class);
    @Autowired
    private EventMsgService eventMsgService;
    @Autowired
    private EventInfoService eventInfoService;

    /**
     * 暂态消息获取
     */
    @PostMapping("eventDetailList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "num", value = "每页数量", required = true, paramType = "query"),
    })
    @ApiOperation(value = "暂态消息获取入口", notes = "暂态消息", response = ResponseMsgData.class)
    public ResponseMsgData eventDetailList(String userId, int page, int num, HttpServletRequest request) {
        ResponseMsgData responseData;

        if (PubUtils.isBlank(userId)) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, EventMsgCodeEnum.USERID_WRONG.getCode(), EventMsgCodeEnum.USERID_WRONG.getMsg(), 0, null);
            return responseData;
        }

        try {
            List<EventMsgDetail> eventMsgs = new ArrayList<>();
//            eventMsgs = eventMsgService.eventDetailList(userId, page, num);
            eventMsgs = eventMsgService.eventDetailAppList(userId, page, num);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, EventMsgCodeEnum.GETDETAIL_SUCCESS.getCode(), EventMsgCodeEnum.GETDETAIL_SUCCESS.getMsg(), eventMsgService.getUnState(userId), eventMsgs);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, EventMsgCodeEnum.GETDETAIL_FALL.getCode(), EventMsgCodeEnum.GETDETAIL_FALL.getMsg(), 0, null);
        }
        return responseData;
    }

    /**
     * 暂态事件基本信息获取
     */
    @PostMapping("eventDetailBaseInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventMsgIndex", value = "暂降事件id", required = true, paramType = "query"),
    })
    @ApiOperation(value = "暂态事件基本信息入口", notes = "暂态事件基本信息", response = ResponseData.class)
    public ResponseData eventDetailBaseInfo(String eventMsgIndex, HttpServletRequest request) {
        ResponseData responseData;
        if (PubUtils.isBlank(eventMsgIndex)) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, EventMsgCodeEnum.eventDetailIndex_WRONG.getCode(), EventMsgCodeEnum.eventDetailIndex_WRONG.getMsg(), null);
            return responseData;
        }
        try {
            EventInfoDetail eventInfoList = eventInfoService.eventDetailBaseInfo(eventMsgIndex);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, EventMsgCodeEnum.GETBASEDETAIL_SUCCESS.getCode(), EventMsgCodeEnum.GETBASEDETAIL_SUCCESS.getMsg(), eventInfoList);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, EventMsgCodeEnum.GETBASEDETAIL_FALL.getCode(), EventMsgCodeEnum.GETBASEDETAIL_FALL.getMsg(), null);
        }
        return responseData;
    }

    /**
     * 暂态事件特征幅值
     */
    @PostMapping("eventDetailEigenvalue")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventDetailIndex", value = "暂降事件id", required = true, paramType = "query"),
    })
    @ApiOperation(value = "暂态事件特征幅值入口", notes = "暂态事件特征幅值", response = ResponseData.class)
    public ResponseEigMsgData eventDetailEigenvalue(String eventDetailIndex, HttpServletRequest request) {
        ResponseEigMsgData responseData;
        logger.error("计算特征幅值，事件索引为：{}",eventDetailIndex);
        responseData = PubUtils.assignmentAppEigResponse(ReturnCode.RETURN_SUCCESS, EventMsgCodeEnum.GETEIGVALUEDETAIL_SUCCESS.getCode(), EventMsgCodeEnum.GETEIGVALUEDETAIL_SUCCESS.getMsg(), -1, null, null);
        return responseData;
//        if (PubUtils.isBlank(eventDetailIndex)) {
//            responseData = PubUtils.assignmentAppEigResponse(ReturnCode.RETURN_FAIL, EventMsgCodeEnum.eventDetailIndex_WRONG.getCode(), EventMsgCodeEnum.eventDetailIndex_WRONG.getMsg(), -1, null, null);
//            return responseData;
//        }
//
//        try {
//            List<EventEigDetail> eventDetailEigenvalue = eventInfoService.eventDetailEigenvalue(eventDetailIndex);
//            int backNumber = 0;
//            String sagReason = "";
//            if (!CollectionUtils.isEmpty(eventDetailEigenvalue)) {
//                sagReason = eventDetailEigenvalue.get(0).sagReason;
//                backNumber = eventInfoService.backNumber();
//            } else {
//                backNumber = 0;
//                sagReason = "无";
//            }
//            responseData = PubUtils.assignmentAppEigResponse(ReturnCode.RETURN_SUCCESS, EventMsgCodeEnum.GETEIGVALUEDETAIL_SUCCESS.getCode(), EventMsgCodeEnum.GETEIGVALUEDETAIL_SUCCESS.getMsg(), backNumber, sagReason, eventDetailEigenvalue);
//        } catch (Exception e) {
//            logger.error(e.toString());
//            responseData = PubUtils.assignmentAppEigResponse(ReturnCode.RETURN_FAIL, EventMsgCodeEnum.GETEIGVALUEDETAIL_FALL.getCode(), EventMsgCodeEnum.GETEIGVALUEDETAIL_FALL.getMsg(), -1, null, null);
//        }
//        return responseData;
    }

    /**
     * 暂态事件波形图
     */
    @PostMapping("eventDetailWave")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventDetailIndex", value = "暂降事件id", required = true, paramType = "query"),
    })
    @ApiOperation(value = "暂态事件波形图入口", notes = "暂态事件波形图", response = ResponseData.class)
    public ResponseData eventDetailWave(String eventDetailIndex, HttpServletRequest request) {
        ResponseData responseData;

        if (PubUtils.isBlank(eventDetailIndex)) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, EventMsgCodeEnum.eventDetailIndex_WRONG.getCode(), EventMsgCodeEnum.eventDetailIndex_WRONG.getMsg(), null);
            return responseData;
        }

        try {
            EventWaveDetail eventWaveDetails = eventInfoService.eventDetailWave(eventDetailIndex, request);
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, EventMsgCodeEnum.GETWAVEDETAIL_SUCCESS.getCode(), EventMsgCodeEnum.GETWAVEDETAIL_SUCCESS.getMsg(), eventWaveDetails);
        } catch (Exception e) {
            responseData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, EventMsgCodeEnum.GETWAVEDETAIL_FALL.getCode(), EventMsgCodeEnum.GETWAVEDETAIL_FALL.getMsg(), null);
        }
        return responseData;
    }

    /**
     * 暂态评价
     */
    @PostMapping("eventDetailEvaluate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eventDetailIndex", value = "暂降事件id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "evaluate", value = "暂态评价", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
    })
    @ApiOperation(value = "暂态评价入口", notes = "暂态评价获取", response = ResponseInfoData.class)
    public ResponseInfoData eventDetailEvaluate(String eventDetailIndex, int evaluate, String userId, HttpServletRequest request) {
        ResponseInfoData responseInfoData;
        if (PubUtils.isBlank(eventDetailIndex)) {
            responseInfoData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, EventMsgCodeEnum.eventDetailIndex_WRONG.getCode(), EventMsgCodeEnum.eventDetailIndex_WRONG.getMsg());
            return responseInfoData;
        }
        try {
            eventInfoService.eventDetailEvaluate(eventDetailIndex, evaluate, userId);
            responseInfoData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_SUCCESS, EventMsgCodeEnum.GETEVADETAIL_SUCCESS.getCode(), EventMsgCodeEnum.GETEVADETAIL_SUCCESS.getMsg());
        } catch (Exception e) {
            responseInfoData = PubUtils.assignmentAppResponse(ReturnCode.RETURN_FAIL, EventMsgCodeEnum.GETEVADETAIL_FALL.getCode(), EventMsgCodeEnum.GETEVADETAIL_FALL.getMsg());
        }
        return responseInfoData;
    }


}