package com.css.simulation.resource.server.adapter.controller.job_manage;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.param.project.*;
import api.common.pojo.vo.project.*;
import api.common.util.StringUtil;
import com.css.simulation.resource.server.app.service.job_manage.MultiSimulationProjectService;
import com.css.simulation.resource.server.app.service.job_manage.SimulationProjectService;
import com.css.simulation.resource.server.app.service.ClusterService;
import com.github.pagehelper.PageInfo;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 工作台模块--项目运行
 */
@RestController
@RequestMapping("/simulationProject")
public class JobManageController {

  @Resource
  private SimulationProjectService simulationProjectService;
  @Resource
  private ClusterService clusterService;

  @Resource
  private MultiSimulationProjectService multiSimulationProjectService;

  //* -------------------------------- 项目运行 --------------------------------

  /**
   * 1. 手动项目：第一次运行项目 10 -> 20
   * 2. 手动项目：终止项目 20 -> 40
   * 3. 手动项目：重新运行项目 40 -> 20
   */
  @RequestMapping("updateProjectNowRunState")
  public ResponseBodyVO<String> updateProjectNowRunState(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.updateProjectNowRunState(param);
  }


  @RequestMapping("updateAutomaticRunState")
  public ResponseBodyVO<String> updateAutomaticRunState(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.updateAutomaticRunState(param);
  }


  @RequestMapping("createAutomaticSubProject")
  public ResponseBodyVO<String> createAutomaticSubProject(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.createAutomaticSubProject(param);
  }

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


  /**
   * 导出测试报告
   */
  @RequestMapping("exportProjectReportById")
  public void exportProjectReportById(@RequestBody SimulationManualProjectParam param) {
    simulationProjectService.exportProjectReport(param);
  }

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

  /**
   * 创建/修改工作
   */
  @RequestMapping("/addOrUpdateProject")
  public ResponseBodyVO<String> addOrUpdateProject(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.addOrUpdateProject(param);
  }

  /**
   * 查询项目列表
   */
  @RequestMapping("/selectProject")
  public ResponseBodyVO<PageInfo<SimulationManualProjectVO>> selectProject(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectProject(param);
  }

  /**
   * 根据id查询
   */
  @RequestMapping("/selectProjectById")
  public ResponseBodyVO<SimulationManualProjectSingleVo> selectProjectById(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectProjectById(param);
  }

  /**
   * 删除工作（支持批量删除）
   */
  @RequestMapping("/deleteProjectByids")
  public ResponseBodyVO<String> deleteProjectByIds(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.deleteProjectByIds(param);
  }


  /**
   * 获取工作详情
   */
  @RequestMapping("/selectProjectDetailsById")
  public ResponseBodyVO<ProjectDetailsVO> selectProjectDetailsById(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectProjectDetailsById(param);
  }

  /**
   * 获取工作详情
   */
  @RequestMapping("/selectProjectDetailsByIdForScheduler")
  public ResponseBodyVO<ProjectDetailsVO> selectProjectDetailsByIdForScheduler(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectProjectDetailsByIdForScheduler(param);
  }

  /**
   * 获取测试报告详情
   */
  @RequestMapping("/selectProjectReportById")
  public ResponseBodyVO<ProjectReportVO> selectProjectReportById(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectProjectReportById(param);
  }

  /**
   * 获取测试报告详情
   */
  @RequestMapping("/selectProjectReportByIdForScheduler")
  public ResponseBodyVO<ProjectReportVO> selectProjectReportByIdForScheduler(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectProjectReportByIdForScheduler(param);
  }


  /**
   * 获取任务详情信息
   */
  @RequestMapping("/selectProjectTaskById")
  public ResponseBodyVO<ProjectTaskDetailsVo> selectProjectTaskById(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectProjectTaskById(param);
  }

  /**
   * 获取添加工作时需要的下拉选项
   * 0. 算法列表
   * 1. 车辆列表
   * 2. 场景列表
   */
  @RequestMapping("/selectDropDownByType")
  public ResponseBodyVO<List<DropDownTypeVo>> selectDropDownByType(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectDropDownByType(param);
  }

  /**
   * 获取添加工作时需要的下拉选项
   * 0. 算法列表
   * 1. 车辆列表
   * 2. 场景列表
   */
  @RequestMapping("/selectDropDownByTypeNew")
  public ResponseBodyVO<List<DropDownTypeNewVo>> selectDropDownByTypeNew(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectDropDownByTypeNew(param);
  }


  /**
   * @return 人员可用最大并行度
   */
  @RequestMapping("/selectMaxParallelism")
  public ResponseBodyVO<String> selectMaxParallelism() {
    Integer licenseNum = clusterService.getLicenseNum();
    return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, null, licenseNum + "");
  }

  /**
   * 根据字典类型和
   *
   * @param param 参数
   */
  @PostMapping("/getDictName")
  public ResponseBodyVO<String> getDictName(@RequestBody DictParam param) {
    String dictName = simulationProjectService.getDictName(param.getType(), param.getCode());
    return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, null, dictName);

  }


  /**
   * 解析车辆坐标文件
   */
  @RequestMapping("/analysisVehicleCoord")
  public ResponseBodyVO<String> analysisVehicleCoord() {
    return simulationProjectService.analysisVehicleCoord();
  }


  /**
   * 查询工作任务列表
   */
  @RequestMapping("/selectProjectTaskList")
  public ResponseBodyVO<PageInfo<ManualProjectTaskVo>> selectProjectTaskList(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectProjectTaskList(param);
  }


  /**
   * 生成保存评测等级（任务结果返回时调用）
   *
   * @return 响应对象
   */
  @RequestMapping("/saveEvaluationLevel")
  public ResponseBodyVO<String> saveEvaluationLevel(@RequestBody SimulationManualProjectParam simulationManualProjectParam) {
    String id = simulationManualProjectParam.getId();
    if (StringUtil.isEmpty(id)) {
      return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "id 不能为空", "");
    } else {
      return simulationProjectService.saveEvaluationLevel(id);
    }
  }

  /**
   * 保存仿真结果到数据库（任务结果返回时调用）
   */
  @RequestMapping("/saveTaskResult")
  public ResponseBodyVO<Object> saveTaskResult(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.saveTaskResult(param);
  }

  /**
   * 导出工作任务文件包
   */
  @RequestMapping("/exportProjectTaskFileById")
  public void exportProjectTaskFileById(@RequestBody SimulationManualProjectParam param) {
    simulationProjectService.exportProjectTaskFileById(param);
  }

  /**
   * 同时导出工作报告和任务包
   */
  @RequestMapping("/exportProjectReportAndTaskFileById")
  public void exportProjectReportAndTaskFileById(@RequestBody SimulationManualProjectParam param) {
    simulationProjectService.exportProjectReportAndTaskFileById(param);
  }

  /**
   * 添加编辑自动项目
   */
  @RequestMapping("/addOrUpdateAutomaticProject")
  public ResponseBodyVO addOrUpdateAutomaticProject(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.addOrUpdateAutomaticProject(param);
  }


  /**
   * 删除自动运行工作（支持批量删除）
   */
  @RequestMapping("/deleteAutomaticProjectByids")
  public ResponseBodyVO<String> deleteAutomaticProjectByids(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.deleteAutomaticProjectByids(param);
  }


  ;

  /**
   * 查询自动运行项目列表
   */
  @RequestMapping("/selectAutomaticProject")
  public ResponseBodyVO<PageInfo<SimulationManualProjectVO>> selectAutomaticProject(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectAutomaticProject(param);
  }


  /**
   * 删除自动运行子项目（支持批量删除）
   */
  @RequestMapping("/deleteAutomaticSubProjectByIds")
  public ResponseBodyVO deleteAutomaticSubProjectByIds(@RequestBody SimulationManualProjectParam param) {

    return simulationProjectService.deleteAutomaticSubProjectByIds(param);
  }

  /**
   * 查询自动运行子工作信息
   */
  @RequestMapping("/selectSubProjectInfo")
  public ResponseBodyVO<AutomaticProjectVO> selectSubProjectInfo(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectSubProjectInfo(param);
  }


  /**
   * 查询自动运行子工作列表
   */
  @RequestMapping("/selectSubProjectList")
  public ResponseBodyVO<PageInfo<SimulationManualProjectVO>> selectSubProjectList(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectSubProjectList(param);
  }

  /**
   * 根据id查询自动运行项目信息
   */
  @RequestMapping("/selectAutomaticProjectById")
  public ResponseBodyVO<SimulationManualProjectSingleVo> selectAutomaticProjectById(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.selectAutomaticProjectById(param);
  }

  /**
   * 修改自动运行子工作运行状态
   */
  @RequestMapping("/updateAutoProjectNowRunState")
  public ResponseBodyVO<String> updateAutoProjectNowRunState(@RequestBody SimulationManualProjectParam param) {
    return simulationProjectService.updateAutoProjectNowRunState(param);
  }


  @RequestMapping("/selectMultiSimulationProject")
  public ResponseBodyVO<PageInfo<MultiSimulationProjectVO>> selectMultiSimulationProject(@RequestBody MultiSimulationProjectParam param) {
    return multiSimulationProjectService.selectMultiSimulationProject(param);
  }
  @RequestMapping("/addMultiSimulationProject")
  public ResponseBodyVO<MultiSimulationProjectDetailVO> addMultiSimulationProject(@RequestBody MultiSimulationProjectParam param) {
    return multiSimulationProjectService.addMultiSimulationProject(param);
  }

  @RequestMapping("/deleteMultiSimulationProject")
  public ResponseBodyVO<String> deleteMultiSimulationProject(@RequestBody MultiSimulationProjectParam param) {
    return multiSimulationProjectService.deleteMultiSimulationProject(param);
  }

  @RequestMapping("/updateMultiSimulationProjectStatus")
  public ResponseBodyVO updateMultiSimulationProjectStatus(@RequestBody MultiSimulationProjectParam param) {
    return multiSimulationProjectService.updateMultiSimulationProjectStatus(param);
  }

  @RequestMapping("/submitMultiSimulationProjectDetail")
  public ResponseBodyVO submitMultiSimulationProjectDetail(@RequestBody MultiSimulationProjectParam param) {
    return multiSimulationProjectService.submitMultiSimulationProjectDetail(param);
  }

  @RequestMapping("/updateMultiSimulationProjectDetail")
  public ResponseBodyVO<String> updateMultiSimulationProjectDetail(@RequestBody MultiSimulationProjectParam param) {
    return multiSimulationProjectService.updateMultiSimulationProject(param);
  }

  @RequestMapping("/getMultiSimulationProjectDetail")
  public ResponseBodyVO<MultiSimulationProjectVO> getMultiSimulationProjectDetail(@RequestBody MultiSimulationProjectParam param) {
    return multiSimulationProjectService.getMultiSimulationProjectDetail(param);
  }
  @RequestMapping("/addMultiSimulationScene")
  public ResponseBodyVO<MultiSimulationSceneVO> addMultiSimulationScene(@RequestBody MultiSimulationSceneParam param) {
    return multiSimulationProjectService.addMultiSimulationScene(param);
  }

  @RequestMapping("/deleteMultiSimulationScene")
  public ResponseBodyVO deleteMultiSimulationScene(@RequestBody MultiSimulationSceneParam param) {
    return multiSimulationProjectService.deleteMultiSimulationScene(param);
  }

  @RequestMapping("/selectMultiSimulationSceneList")
  public ResponseBodyVO<PageInfo<MultiSimulationSceneVO>> selectMultiSimulationSceneList(@RequestBody MultiSimulationSceneParam param) {
    return multiSimulationProjectService.selectMultiSimulationSceneList(param);
  }

  @RequestMapping("/getMultiSimulationSceneDetail")
  public ResponseBodyVO<MultiSimulationSceneVO> getMultiSimulationSceneDetail(@RequestBody MultiSimulationSceneParam param) {
    return multiSimulationProjectService.getMultiSimulationSceneDetail(param);
  }

  @RequestMapping("/getMultiSimulationSceneCarList")
  public ResponseBodyVO<PageInfo<MultiSimulationSceneCarVO>> getMultiSimulationSceneCarList(@RequestBody MultiSimulationSceneCarParam param) {
    return multiSimulationProjectService.getMultiSimulationSceneCarList(param);
  }

  @RequestMapping("/deleteMultiSimulationSceneCar")
  public ResponseBodyVO deleteMultiSimulationSceneCar(@RequestBody MultiSimulationSceneCarParam param) {
    return multiSimulationProjectService.deleteMultiSimulationSceneCar(param);
  }

  @RequestMapping("/addOrUpdateMultiSimulationSceneCar")
  public ResponseBodyVO<MultiSimulationSceneCarVO> addOrUpdateMultiSimulationSceneCar(@RequestBody MultiSimulationSceneCarParam param) {
    return multiSimulationProjectService.addOrUpdateMultiSimulationSceneCar(param);
  }

//  @RequestMapping("/addOrUpdateMultiSimulationSceneCarList")
//  public ResponseBodyVO<List<MultiSimulationSceneCarVO>> addOrUpdateMultiSimulationSceneCarList(@RequestBody List<MultiSimulationSceneCarParam> paramList) {
//    return multiSimulationProjectService.addOrUpdateMultiSimulationSceneCarList(paramList);
//  }

  @RequestMapping("/addOrUpdateMultiSimulationSceneCarList")
  public ResponseBodyVO<List<MultiSimulationSceneCarVO>> addOrUpdateMultiSimulationSceneCarList(@RequestBody AddOrUpdateMultiSimulationSceneCarParam param) {
    return multiSimulationProjectService.addOrUpdateMultiSimulationSceneCarList(param);
  }

  @RequestMapping("/setMultiSimulationSceneCarView")
  public ResponseBodyVO setMultiSimulationSceneCarView(@RequestBody MultiSimulationSceneCarParam param) {
    return multiSimulationProjectService.setMultiSimulationSceneCarView(param);
  }

  /**
   * 另存为
   */
  @RequestMapping("/saveAsMultiSimulationProjectDetail")
  public ResponseBodyVO<MultiSimulationProjectVO> saveAsMultiSimulationProjectDetail(@RequestBody MultiSimulationProjectParam param) {
    return multiSimulationProjectService.saveAsMultiSimulationProjectDetail(param);
  }

  /**
   * 另存为
   */
  @RequestMapping("/copyMultiSimulationScene")
  public ResponseBodyVO<MultiSimulationSceneVO> copyMultiSimulationScene(@RequestBody MultiSimulationSceneParam param) {
    return multiSimulationProjectService.copyMultiSimulationScene(param);
  }

  @RequestMapping("/viewMultiSimulationProjectResult")
  public ResponseBodyVO<MultiSimulationSceneResultVO> viewMultiSimulationProjectResult(@RequestBody MultiSimulationSceneParam param) {
    return multiSimulationProjectService.viewMultiSimulationProjectResult(param);
  }
  @GetMapping("/downloadMultiSimulationProjectResult")
  public void downloadMultiSimulationProjectResult(@RequestParam("projectId") String projectId) {
    MultiSimulationProjectParam multiSimulationProjectParam = new MultiSimulationProjectParam();
    multiSimulationProjectParam.setProjectId(projectId);
    multiSimulationProjectService.exportProjectTaskFileById(multiSimulationProjectParam);
  }
}
