package com.njcn.gather.detection.controller;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.detection.pojo.param.ContrastDetectionParam;
import com.njcn.gather.detection.pojo.param.PreDetectionParam;
import com.njcn.gather.detection.pojo.param.SimulateDetectionParam;
import com.njcn.gather.detection.service.PreDetectionService;
import com.njcn.gather.detection.util.socket.CnSocketUtil;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author chendaofei
 */
@Slf4j
@Api(tags = "预检测")
@RestController
@RequestMapping("/prepare")
@RequiredArgsConstructor
public class PreDetectionController extends BaseController {

    private final PreDetectionService preDetectionService;

    /**
     * 开始检测通用入口
     *
     * @param param 实体参数
     */
    @PostMapping("/startPreTest")
    @OperateInfo
    @ApiOperation("开始检测")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<String> startPreTest(@RequestBody @Validated PreDetectionParam param) {
        String methodDescribe = getMethodDescribe("startPreTest");
        preDetectionService.sourceCommunicationCheck(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    /**
     * 程控源-源通讯校验
     *
     * @param param
     * @return
     */
    @PostMapping("/ytxCheckSimulate")
    @OperateInfo
    @ApiOperation("源通讯校验")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<String> ytxCheckSimulate(@RequestBody @Validated SimulateDetectionParam param) {
        String methodDescribe = getMethodDescribe("ytxCheckSimulate");
        preDetectionService.ytxCheckSimulate(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 程控源检测
     */
    @PostMapping("/startSimulateTest")
    @OperateInfo
    @ApiOperation("启动")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<String> startTestSimulate(@RequestBody @Validated SimulateDetectionParam param) {
        String methodDescribe = getMethodDescribe("startTestSimulate");
        preDetectionService.sendScript(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 关闭检测-程控源
     */
    @PostMapping("/closeSimulateTest")
    @OperateInfo
    @ApiOperation("停止")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<String> closeSimulateTest(@RequestBody @Validated SimulateDetectionParam param) {
        String methodDescribe = getMethodDescribe("closeSimulateTest");
        preDetectionService.closeTestSimulate(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    /**
     * 系数校验
     */
    @PostMapping("/coefficientCheck")
    @OperateInfo
    @ApiOperation("系数校验")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<String> coefficientCheck(@RequestBody PreDetectionParam param) {
        String methodDescribe = getMethodDescribe("coefficientCheck");
        preDetectionService.coefficientCheck(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    /**
     * 暂停检测
     */
    @GetMapping("/closePreTest")
    @OperateInfo
    @ApiOperation("暂停检测")
    @ApiImplicitParam(name = "param", value = "参数", required = true)
    public HttpResult<String> temStopTest() {
        String methodDescribe = getMethodDescribe("temStopTest");
        preDetectionService.temStopTest();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    /**
     * 重新开始检测
     */
    @PostMapping("/restartTemTest")
    @OperateInfo
    @ApiOperation("重新开始检测")
    @ApiImplicitParam(name = "param", value = "参数", required = true)
    public HttpResult<String> restartTemTest(@RequestBody PreDetectionParam param) {
        String methodDescribe = getMethodDescribe("restartTemTest");
        preDetectionService.restartTemTest(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    /**
     * 开始比对检测（包括预检测、正式检测）通用入口
     */
    @PostMapping("/startContrastTest")
    @OperateInfo
    @ApiOperation("开始比对检测")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<String> startContrastTest(@RequestBody @Validated ContrastDetectionParam param) {
        String methodDescribe = getMethodDescribe("startContrastTest");
        preDetectionService.startContrastTest(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/exportAlignData")
    @ApiOperation("实时对齐数据导出为csv文件")
    public void exportAlignData() {
        String methodDescribe = getMethodDescribe("exportAlignData");
        LogUtil.njcnDebug(log, "{}", methodDescribe);
        preDetectionService.exportAlignData();
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/canCoefficient")
    @ApiOperation("比对模式是否能够进行系数校验")
    public HttpResult<Boolean> canCoefficient() {
        String methodDescribe = getMethodDescribe("canCoefficient");
        LogUtil.njcnDebug(log, "{}", methodDescribe);

        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, preDetectionService.getCanCoefficient(), methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/startCoefficient")
    @ApiOperation("比对模式开启系数校验")
    public HttpResult<String> startCoefficient() {
        String methodDescribe = getMethodDescribe("startCoefficient");
        LogUtil.njcnDebug(log, "{}", methodDescribe);

        preDetectionService.startCoefficient();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/startFreqConverter")
    @ApiOperation("开启变频器测试")
    public HttpResult<String> startFreqConverter(@RequestParam("userId") String userId, @RequestParam("converterId") String converterId, @RequestParam("monitorId") String monitorId, @RequestParam("reset") Boolean reset) {
        String methodDescribe = getMethodDescribe("startFreqConverter");
        LogUtil.njcnDebug(log, "{}", methodDescribe);

        preDetectionService.startFreqConverter(userId, converterId, monitorId,reset);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/stopFreqConverter")
    @ApiOperation("关闭变频器测试")
    public HttpResult<String> stopFreqConverter(@RequestParam("userId") String userId) {
        String methodDescribe = getMethodDescribe("stopFreqConverter");
        LogUtil.njcnDebug(log, "{}", methodDescribe);

        preDetectionService.stopFreqConverter(userId + CnSocketUtil.FREQ_CONVERTER_TAG, userId + CnSocketUtil.DEV_TAG);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }
}
