package net.educoder.ecsonar.controller;

import com.alibaba.fastjson.JSONArray;
import net.educoder.ecsonar.constant.Constant;
import net.educoder.ecsonar.model.RollPage;
import net.educoder.ecsonar.model.api.QualityInspect;
import net.educoder.ecsonar.model.api.QualityInspectIsCompleted;
import net.educoder.ecsonar.model.api.QualityInspectResultData;
import net.educoder.ecsonar.model.dto.AnalyseDetailDTO;
import net.educoder.ecsonar.model.dto.AnalyseDetailListDTO;
import net.educoder.ecsonar.model.dto.CodeDetailDTO;
import net.educoder.ecsonar.model.dto.ProblemAnalysisDTO;
import net.educoder.ecsonar.model.vo.*;
import net.educoder.ecsonar.services.QualityInspectService;
import net.educoder.ecsonar.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: youys
 * @Date: 2022/1/17
 * @Description:
 */
@Controller
@RequestMapping("/api")
public class QualityInspectController {

    @Autowired
    QualityInspectService qualityInspectService;


    /**
     * 质量检测
     *
     * @param language   语言
     * @param homeworkId 实训作业id
     * @param userDatas  用户数据
     * @return
     */
    @RequestMapping(value = "qualityInspect", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult<String> qualityInspect(@RequestParam String language,
                                                 @RequestParam String homeworkId,
                                                 @RequestParam String userDatas) {

        List<QualityInspectUserDataVO> userDataVOList = JSONArray.parseArray(userDatas, QualityInspectUserDataVO.class);

        QualityInspectVO qualityInspectVO = new QualityInspectVO(language, homeworkId, userDataVOList);
        if (!Constant.language.contains(qualityInspectVO.getLanguage())) {
            return ResponseResult.error("不支持的语言");
        }
        QualityInspect qualityInspect = qualityInspectService.qualityInspect(qualityInspectVO);
        return ResponseResult.success(qualityInspect);
    }


    /**
     * 质量检测任务是否完成
     *
     * @param taskId
     * @return
     */
    @RequestMapping(value = "qualityInspectIsCompleted", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult<QualityInspectIsCompleted> qualityInspectIsCompleted(@RequestParam String taskId) {
        QualityInspectIsCompleted result = qualityInspectService.qualityInspectIsCompleted(taskId);
        return ResponseResult.success(result);
    }


    /**
     * 质量检测结果查询
     *
     * @param taskId
     * @return
     */
    @RequestMapping(value = "qualityInspectResultQuery", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult<RollPage<QualityInspectResultData>> qualityInspectResultQuery(
            @RequestParam(defaultValue = "50") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam String taskId) {
        if (pageNum <= 1) {
            pageNum = 1;
        }
        QualityInspectIsCompleted isCompleted = qualityInspectService.qualityInspectIsCompleted(taskId);
        if (isCompleted.getCompleted() != 1) {
            return ResponseResult.error("质量检测正在处理中");
        }

        RollPage<QualityInspectResultData> result = qualityInspectService.qualityInspectResultQuery(pageNum, pageSize, taskId);
        return ResponseResult.success(result);
    }


    /**
     * 分析详情
     *
     * @return
     */
    @GetMapping("/analyseDetail")
    @ResponseBody
    public ResponseResult<AnalyseDetailDTO> analyseDetail(@Valid AnalyseDetailVO analyseDetailVO) {
        AnalyseDetailDTO analyseDetailDTO = qualityInspectService.getAnalyseDetail(analyseDetailVO);
        return ResponseResult.success(analyseDetailDTO);
    }

    /**
     * 分析详情列表
     *
     * @return
     */
    @GetMapping("/analyseDetailList")
    @ResponseBody
    public ResponseResult<RollPage> analyseDetailList(@Valid AnalyseDetailListVO analyseDetailListVO) {
        RollPage<AnalyseDetailListDTO> rollPage = qualityInspectService.getAnalyseDetailList(analyseDetailListVO);
        return ResponseResult.success(rollPage);
    }


    /**
     * 问题分析
     *
     * @return
     */
    @GetMapping("/problemAnalysis")
    @ResponseBody
    public ResponseResult<ProblemAnalysisDTO> problemAnalysis(@RequestParam Integer ruleId) {
        ProblemAnalysisDTO problemAnalysis = qualityInspectService.getProblemAnalysis(ruleId);
        return ResponseResult.success(problemAnalysis);
    }


    /**
     * 代码详情
     *
     * @return
     */
    @GetMapping("/codeDetail")
    @ResponseBody
    public ResponseResult<CodeDetailDTO> codeDetail(@Valid CodeDetailVO codeDetailVO) {
        CodeDetailDTO codeDetail = qualityInspectService.getCodeDetail(codeDetailVO);
        return ResponseResult.success(codeDetail);
    }


}



