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

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.scene.SceneAccidentParam;
import api.common.pojo.param.scene.SceneImportParam;
import api.common.pojo.po.scene.*;
import api.common.pojo.po.system.SceneImportPO;
import api.common.pojo.vo.scene.SceneAccidentVO;
import api.common.util.CollectionUtil;
import com.css.simulation.resource.server.infra.util.PageUtil;
import com.css.simulation.resource.server.app.service.SceneAccidentService;
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;

//交通事故场景 1
@RestController
@RequestMapping("/SceneAccident")
public class SceneAccidentController {

    @Resource
    private SceneAccidentService sceneAccidentService;
    @Resource
    private SceneImportService sceneImportService;

    //* -------------------------------- 场景上传任务 --------------------------------

    @PostMapping("/saveSceneAccidentBatch")
    public ResponseBodyVO<String> saveSceneAccidentBatch(@RequestBody SceneImportParam sceneImportParam) {
        //1 更新任务状态
        SceneImportPO sceneImportPO = new SceneImportPO();
        sceneImportPO.setId(sceneImportParam.getId());
        sceneImportPO.setStatus(DictConstants.SCENE_IMPORT_STATUS_3);
        sceneImportService.updateStatus(sceneImportPO);
        //2 上传场景
        sceneImportPO = sceneAccidentService.importMiNio(sceneImportParam);
        //3 更新任务状态
        sceneImportService.updateInfo(sceneImportPO);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

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

    @PostMapping("/batchDeleteSceneAccident")
    public ResponseBodyVO<String> batchDeleteSceneAccident(@RequestBody SceneAccidentParam params) {
        List<String> sceneNameList = params.getSceneNameList();
        sceneAccidentService.batchDeleteSceneAccident(sceneNameList);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

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


    //新增交通事故
    @PostMapping("/saveSceneAccident")
    public ResponseBodyVO<String> saveSceneAccident(@RequestBody SceneAccidentParam params) {
        return sceneAccidentService.importJtSg(params);

    }

    //删除交通事故
    @PostMapping("/deleteSceneAccident")
    public ResponseBodyVO<String> deleteSceneAccident(@RequestBody SceneAccidentPO params) {
//        if (ObjectUtil.isNull(params.getAccidentId())) {
//            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "参数场景 id 不能为空，请检查。", null);
//        }
        sceneAccidentService.deleteSceneAccidentList(CollectionUtil.createArrayList(params.getSceneName()));
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }


    /**
     * 分享交通事故
     */
    @PostMapping("/instShareSceneAccident")
    public ResponseBodyVO<String> instShareSceneAccident(@RequestBody SceneAccidentPO params) {
        sceneAccidentService.instShareSceneAccident(params);

        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);

    }


}
