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

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.SceneNaturalNewParam;
import api.common.pojo.param.scene.SceneNaturalParam;
import api.common.pojo.po.scene.SceneComplexityPO;
import api.common.pojo.po.scene.SceneEvaluationOperatePO;
import api.common.pojo.po.scene.SceneNaturalPO;
import api.common.pojo.po.scene.SceneRiskPO;
import api.common.pojo.po.system.SceneImportPO;
import api.common.pojo.vo.scene.SceneNaturalVO;
import api.common.util.CollectionUtil;
import com.css.simulation.resource.server.infra.util.PageUtil;
import com.css.simulation.resource.server.app.service.SceneNaturalService;
import com.css.simulation.resource.server.app.service.SceneImportService;
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("/SceneNatural")
public class SceneNaturalController {

    @Resource
    private SceneNaturalService sceneNaturalService;
    @Resource
    private SceneImportService sceneImportService;

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

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

    //* -------------------------------- 删除自然驾驶 --------------------------------
    @PostMapping("/deleteSceneNatural")
    public ResponseBodyVO<String> deleteSceneNatural(@RequestBody SceneNaturalPO params) {
        sceneNaturalService.deleteSceneNatural(CollectionUtil.createArrayList(params.getNaturalName()));
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }
    //* -------------------------------- Comment --------------------------------

    /**
     * 查询自然驾驶数据
     */
    @PostMapping("/querySceneNaturalList")
    public ResponseBodyVO<PageInfo<SceneNaturalVO>> querySceneNaturalList(@RequestBody SceneNaturalNewParam params) {
        PageUtil.setPageInfo(params);
        List<SceneNaturalPO> list = sceneNaturalService.querySceneNaturalList(params);
        PageInfo<SceneNaturalPO> objectPageInfo = new PageInfo<>(list);
        PageHelper.clearPage();
        SceneEvaluationOperatePO sceneEvaluationOperatePO = sceneNaturalService.getSceneEvaluationFirst(params, list.stream().map(SceneNaturalPO::getNaturalId).collect(Collectors.toList()));
        Map<String, SceneComplexityPO> complexityPOMap = sceneEvaluationOperatePO.getSceneComplexityPOMap();
        Map<String, SceneRiskPO> riskPOMap = sceneEvaluationOperatePO.getSceneRiskPOMap();
        List<SceneNaturalVO> list1 = new ArrayList<>();
        for (SceneNaturalPO po : list) {
            SceneNaturalVO vo = new SceneNaturalVO();
            BeanUtils.copyProperties(po, vo);
            if (complexityPOMap.get(vo.getNaturalId()) != null) {
                vo.setComplexity(String.valueOf(complexityPOMap.get(vo.getNaturalId()).getComplexity()));
                vo.setComplexityLevel(String.valueOf(complexityPOMap.get(vo.getNaturalId()).getComplexityLevel()));
            }
            if (riskPOMap.get(vo.getNaturalId()) != null) {
                vo.setRisk(String.valueOf(riskPOMap.get(vo.getNaturalId()).getRisk()));
                vo.setRiskLevel(String.valueOf(riskPOMap.get(vo.getNaturalId()).getRiskLevel()));
            }
            list1.add(vo);
        }
        PageInfo<SceneNaturalVO> pageInfo = new PageInfo<>(list1);
        pageInfo.setTotal(objectPageInfo.getTotal());
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, pageInfo);
    }


    //新增自然场景
    @PostMapping("/saveSceneNatural")
    public ResponseBodyVO<String> saveSceneNatural(@RequestBody SceneNaturalParam params) {
        return sceneNaturalService.importZrjsSceneCicv(params);
    }

    @PostMapping("/batchDeleteSceneNatural")
    public ResponseBodyVO<String> batchDeleteSceneNatural(@RequestBody SceneNaturalNewParam params) {
        sceneNaturalService.batchDeleteSceneNatural(params.getNaturalNameList());
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }


    //分享
    @PostMapping("/instShareSceneNatural")
    public ResponseBodyVO<String> instShareSceneNatural(@RequestBody SceneNaturalPO po) {
        sceneNaturalService.instShareSceneNatural(po);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }
}
