package net.educoder.ecsonar.controller;

import com.alibaba.fastjson.JSONObject;
import net.educoder.ecsonar.model.api.QualityInspect;
import net.educoder.ecsonar.model.api.QualityInspectIsCompleted;
import net.educoder.ecsonar.model.dto.CloudBrainQualityInspectDTO;
import net.educoder.ecsonar.model.vo.*;
import net.educoder.ecsonar.services.CloudBrainService;
import net.educoder.ecsonar.utils.ResponseResult;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: youys
 * @Date: 2022/7/5
 * @Description: 云脑质量检测
 */
@RestController
@RequestMapping("/api")
public class CloudBrainController {

    private final Logger logger = LoggerFactory.getLogger(CloudBrainController.class);

    @Autowired
    private CloudBrainService cloudBrainService;

    /**
     * 云脑代码质量检测
     *
     * @param cloudBrainQualityInspectVO
     * @return
     */
    @PostMapping("/cloudQualityInspect")
    public ResponseResult<List<CloudBrainQualityInspectDTO>> cloudQualityInspect(@RequestBody List<CloudBrainQualityInspectVO> cloudBrainQualityInspectVO) {
        logger.info("cloudQualityInspect请求参数:{}", JSONObject.toJSONString(cloudBrainQualityInspectVO));
        if (CollectionUtils.isEmpty(cloudBrainQualityInspectVO)) {
            ResponseResult.error("参数不能为空");
        }
        List<CloudBrainQualityInspectDTO> inspectDTOList = cloudBrainService.startQualityInspect(cloudBrainQualityInspectVO);
        return ResponseResult.success(inspectDTOList);
    }


    /**
     * 检测结果查询
     *
     * @param taskId
     * @return
     */
    @GetMapping(value = "/cloudQualityInspectResult")
    public ResponseResult<CloudQualityInspectResultVO> cloudQualityInspectResult(@RequestParam String taskId) {
        CloudQualityInspectResultVO result = cloudBrainService.cloudQualityInspectResult(taskId);
        return ResponseResult.success(result);
    }


    /**
     * issues详情查询
     *
     * @param searchVO
     * @return
     */
    @GetMapping(value = "/issues/search")
    public ResponseResult<IssuesVO> issuesSearch(IssuesSearchVO searchVO) {
        if (StringUtils.isBlank(searchVO.getTaskName())) {
            return ResponseResult.error("taskName不能为空");
        }
        if (StringUtils.isBlank(searchVO.getType())) {
            return ResponseResult.error("type不能为空");
        }
        List<IssuesVO> result = cloudBrainService.issuesSearch(searchVO);
        return ResponseResult.success(result);
    }


    /**
     * 获取源码
     *
     * @param component
     * @return
     */
    @GetMapping(value = "/getSourceCode")
    public ResponseResult<Map> getSourceCode(@RequestParam String component) {

        List<SourceCodeVO> sources = cloudBrainService.getSourceCode(component);

        Map<String, Object> result = new HashMap<>(2);
        result.put("sources", sources);

        return ResponseResult.success(result);
    }

}
