package com.css.simulation.resource.server.adapter.controller.scene_library.scene_reference_lib;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.scene.SceneImportParam;
import api.common.pojo.param.scene.SceneReferenceLibParam;
import api.common.pojo.po.scene.*;
import api.common.pojo.po.system.SceneImportPO;
import api.common.pojo.vo.scene.SceneReferenceLibVO;
import api.common.util.CollectionUtil;
import api.common.util.ObjectUtil;
import com.css.simulation.resource.server.app.service.SceneImportService;
import com.css.simulation.resource.server.app.service.SceneReferenceLibService;
import com.css.simulation.resource.server.infra.util.AuthUtil;
import com.css.simulation.resource.server.infra.util.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
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.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 基准场景库
 */
@RestController
@RequestMapping("/sceneReference")
public class SceneReferenceLibController {

    @Resource
    private SceneImportService sceneImportService;

    @Resource
    private SceneReferenceLibService sceneReferenceLibService;

    //* -------------------------------- 场景上传 --------------------------------

    @PostMapping("/saveSceneReferenceLibBatch")
    public ResponseBodyVO<String> saveSceneReferenceLibBatch(@RequestBody SceneImportParam sceneImportParam) {
        SceneImportPO sceneImportPO = new SceneImportPO();
        sceneImportPO.setId(sceneImportParam.getId());
        sceneImportPO.setStatus(DictConstants.SCENE_IMPORT_STATUS_3);
        sceneImportService.updateStatus(sceneImportPO);
        // 解析参数
        sceneImportPO = sceneReferenceLibService.importMinio(sceneImportParam);
        sceneImportService.updateInfo(sceneImportPO);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    //* -------------------------------- Comment --------------------------------

    /**
     * 查询基准场景库列表
     *
     * @param params
     * @return
     */
    @PostMapping("/querySceneReferenceLibList")
    public ResponseBodyVO<PageInfo<SceneReferenceLibVO>> querySceneReferenceLibList(@RequestBody SceneReferenceLibParam params) {
        PageUtil.setPageInfo(params);
        List<SceneReferenceLibPO> list = sceneReferenceLibService.querySceneReferenceLibList(params);
        PageInfo<SceneReferenceLibPO> objectPageInfo = new PageInfo<>(list);
        PageHelper.clearPage();
        SceneEvaluationOperatePO sceneEvaluationOperatePO = sceneReferenceLibService.getSceneEvaluationFirst(params, list.stream().map(SceneReferenceLibPO::getSceneId).collect(Collectors.toList()));
        Map<String, SceneComplexityPO> complexityPOMap = sceneEvaluationOperatePO.getSceneComplexityPOMap();
        Map<String, SceneRiskPO> riskPOMap = sceneEvaluationOperatePO.getSceneRiskPOMap();
        List<SceneReferenceLibVO> list1 = new ArrayList<>();
        for (SceneReferenceLibPO po : list) {
            SceneReferenceLibVO vo = new SceneReferenceLibVO();
            BeanUtils.copyProperties(po, vo);
            // 赋值复杂度
            if (complexityPOMap.get(po.getSceneId()) != null) {
                vo.setComplexity(String.valueOf(complexityPOMap.get(po.getSceneId()).getComplexity()));
                vo.setComplexityLevel(String.valueOf(complexityPOMap.get(po.getSceneId()).getComplexityLevel()));
            }
            if (riskPOMap.get(po.getSceneId()) != null) {
                vo.setRisk(String.valueOf(riskPOMap.get(po.getSceneId()).getRisk()));
                vo.setRiskLevel(String.valueOf(riskPOMap.get(po.getSceneId()).getRiskLevel()));
            }
            list1.add(vo);
        }
        PageInfo<SceneReferenceLibVO> pageInfo = new PageInfo<>(list1);
        pageInfo.setTotal(objectPageInfo.getTotal());
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, pageInfo);
    }


    @PostMapping("/saveSceneReferenceById")
    public ResponseBodyVO<String> saveSceneReference(@RequestBody SceneReferenceLibPO sceneReferenceLibPO) {
        if (ObjectUtil.isNull(sceneReferenceLibPO.getSceneName())) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "参数 SceneName 不能为空，请检查");
        }
        if (ObjectUtil.isNull(sceneReferenceLibPO.getSceneId())) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "参数 SceneId 不能为空，请检查");
        }
        sceneReferenceLibService.updateSceneReference(sceneReferenceLibPO);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }


    @PostMapping("/batchDeleteSceneReference")
    public ResponseBodyVO<String> batchDeleteSceneReference(@RequestBody SceneReferenceLibParam params) {
        String roleCode = AuthUtil.getCurrentUserRoleCode();
        if (!DictConstants.ROLE_CODE_ADMIN.equals(roleCode) && !DictConstants.ROLE_CODE_SYSADMIN.equals(roleCode)) {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "非管理员禁止删除");
        }
        if(CollectionUtil.isEmpty(params.getSceneNames())){
            return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        }
        sceneReferenceLibService.batchDeleteSceneReference(params.getSceneNames());
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

}
