package com.njcn.gather.tool.wave.controller;

import com.njcn.common.pojo.annotation.OperateInfo;
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.tool.wave.pojo.param.WaveComtradeParseParam;
import com.njcn.gather.tool.wave.pojo.param.WaveParseParam;
import com.njcn.gather.tool.wave.pojo.vo.WaveComtradeResultVO;
import com.njcn.gather.tool.wave.pojo.vo.WaveComtradeVectorResultVO;
import com.njcn.gather.tool.wave.pojo.vo.WaveParseResultVO;
import com.njcn.gather.tool.wave.service.WaveService;
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.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 波形查看接口。
 */
@Slf4j
@Api(tags = "波形查看")
@RestController
@RequestMapping("/wave")
@RequiredArgsConstructor
public class WaveController extends BaseController {

    /** 波形解析服务。 */
    private final WaveService waveService;

    /**
     * 解析文本波形内容。
     *
     * @param param 波形解析参数
     * @return 波形解析结果
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("解析波形文本")
    @ApiImplicitParam(name = "param", value = "波形解析参数", required = true, dataType = "WaveParseParam")
    @PostMapping("/parse")
    public HttpResult<WaveParseResultVO> parse(@RequestBody WaveParseParam param) {
        String methodDescribe = getMethodDescribe("parse");
        LogUtil.njcnDebug(log, "{}，开始解析文本波形", methodDescribe);
        WaveParseResultVO result = waveService.parse(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 解析 COMTRADE 波形文件。
     *
     * @param cfgFile cfg 文件
     * @param datFile dat 文件
     * @param param 解析参数
     * @return COMTRADE 解析结果
     * @throws IOException 读取上传文件流失败时抛出
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("解析 COMTRADE 波形文件")
    @PostMapping("/parseComtrade")
    public HttpResult<WaveComtradeResultVO> parseComtrade(@RequestParam("cfgFile") MultipartFile cfgFile,
                                                          @RequestParam("datFile") MultipartFile datFile,
                                                          @ModelAttribute WaveComtradeParseParam param) throws IOException {
        String methodDescribe = getMethodDescribe("parseComtrade");
        LogUtil.njcnDebug(log, "{}，开始解析 COMTRADE 波形文件", methodDescribe);
        WaveComtradeResultVO result = waveService.parseComtrade(cfgFile.getInputStream(), datFile.getInputStream(), param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 解析 COMTRADE 原始波形并计算向量结果。
     *
     * @param cfgFile cfg 文件
     * @param datFile dat 文件
     * @param param 解析参数
     * @return 向量结果
     * @throws IOException 读取上传文件流失败时抛出
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @ApiOperation("解析 COMTRADE 向量")
    @PostMapping("/parseComtradeVector")
    public HttpResult<WaveComtradeVectorResultVO> parseComtradeVector(@RequestParam("cfgFile") MultipartFile cfgFile,
                                                                      @RequestParam("datFile") MultipartFile datFile,
                                                                      @ModelAttribute WaveComtradeParseParam param) throws IOException {
        String methodDescribe = getMethodDescribe("parseComtradeVector");
        LogUtil.njcnDebug(log, "{}，开始解析 COMTRADE 向量", methodDescribe);
        WaveComtradeVectorResultVO result = waveService.parseComtradeVector(cfgFile.getInputStream(), datFile.getInputStream(), param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}
