package com.css.simulation.resource.server.domain.service;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.MinioParameter;
import api.common.pojo.param.algorithm.AlgorithmParameter;
import api.common.pojo.param.project.SimulationManualProjectParam;
import api.common.pojo.po.project.SimulationManualProjectPO;
import api.common.pojo.vo.algorithm.AlgorithmVO;
import api.common.pojo.vo.project.ProjectReportVO;
import com.css.simulation.resource.server.infra.feign.service.FileDownService;
import com.css.simulation.resource.server.app.service.job_manage.SimulationProjectService;
import com.css.simulation.resource.server.infra.db.entity.SimulationAutomaticProjectEntity;
import com.css.simulation.resource.server.infra.db.entity.SimulationAutomaticSubprojectEntity;
import com.css.simulation.resource.server.infra.db.entity.SimulationManualProjectEntity;
import com.css.simulation.resource.server.infra.db.mysql.mapper.*;
import lombok.extern.slf4j.Slf4j;

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

@org.springframework.stereotype.Service
@Slf4j
public class ProjectDomainService {

    @Resource
    private SimulationProjectService simulationProjectService;
    @Resource
    private SimulationManualProjectMapper simulationManualProjectMapper;
    @Resource
    private SimulationAutomaticProjectMapper simulationAutomaticProjectMapper;
    @Resource
    private SimulationAutomaticSubprojectMapper simulationAutomaticSubprojectMapper;
    @Resource
    private SimulationManualProjectTaskMapper simulationManualProjectTaskMapper;
    @Resource
    private AlgorithmMapper algorithmMapper;
    @Resource
    private AlgorithmExpandMapper algorithmExpandMapper;
    @Resource
    private FileDownService fileDownService;

    @Resource
    private ScenePackageSublistMapper scenePackageSublistMapper;

    public String getAlgorithmNameByProjectId(String projectType, String projectId) {
        String algorithmId;
        String algorithmType;
        if (DictConstants.PROJECT_TYPE_MANUAL.equals(projectType)) {
            algorithmType = simulationManualProjectMapper.selectAlgorithmTypeById(SimulationManualProjectEntity.builder().id(projectId).build());
            algorithmId = simulationManualProjectMapper.selectAlgorithmById(SimulationManualProjectEntity.builder().id(projectId).build());
        } else if (DictConstants.PROJECT_TYPE_AUTO_SUB.equals(projectType)) {
            String parentId = simulationAutomaticSubprojectMapper.selectParentIdById(SimulationAutomaticSubprojectEntity.builder().id(projectId).build());
            algorithmType = simulationAutomaticProjectMapper.selectAlgorithmTypeById(SimulationAutomaticProjectEntity.builder().id(parentId).build());
            algorithmId = simulationAutomaticProjectMapper.selectAlgorithmById();
        } else {
            throw new RuntimeException("未知项目类型：" + projectType);
        }
        if (DictConstants.ALGORITHM_UPLOAD_MODE_PLATFORM.equals(algorithmType)) {
            final List<AlgorithmVO> select = algorithmExpandMapper.select(AlgorithmParameter.builder().algorithmId(algorithmId).build());
            final AlgorithmVO algorithmVO = select.get(0);
            return algorithmVO.getAlgorithmName() + "-" + algorithmVO.getAlgorithmVersion();
        } else {
            return algorithmMapper.selectAlgorithmNameByAlgorithmId(algorithmId);
        }
    }

    public String getLastProjectIdByAlgorithmId(String algorithmId) {
        return simulationProjectService.selectLastProjectIdByAlgorithmId(algorithmId);
    }

    public String getProjectTypeByProjectId(String projectId) {
        SimulationManualProjectPO simulationManualProjectPO = simulationManualProjectMapper.selectProjectById(SimulationManualProjectParam.builder().id(projectId).build());
        if (simulationManualProjectPO != null) {
            return DictConstants.PROJECT_TYPE_MANUAL;
        } else {
            return DictConstants.PROJECT_TYPE_AUTO_SUB;
        }
    }

    public ProjectReportVO getProjectReport(SimulationManualProjectParam simulationManualProjectParam) {
        ResponseBodyVO<ProjectReportVO> vo = simulationProjectService.selectProjectReportById(simulationManualProjectParam);
        return vo.getInfo();
    }

    public List<String> getVideoList(SimulationManualProjectParam simulationManualProjectParam) {
        List<String> videoList = new ArrayList<>();
        List<String> runResultPaths = simulationManualProjectTaskMapper.selectRunResultPathByProjectId(simulationManualProjectParam);
        for (String runResultPath : runResultPaths) {
            ResponseBodyVO<List<String>> list = fileDownService.list(MinioParameter.builder().objectName(runResultPath).build());
            List<String> info = list.getInfo();
            for (String s : info) {
                if (s.contains(".mp4")) {
                    MinioParameter minioParameter = new MinioParameter();
                    minioParameter.setObjectName(s);
                    ResponseBodyVO<String> preview = fileDownService.getPreviewUrl(minioParameter);
                    videoList.add(preview.getInfo());
                }
            }
        }
        return videoList;
    }

    /**
     * v20240301 将视频列表改为只显示B卷视频
     */
    public List<String> getVideoListV20240301(SimulationManualProjectParam simulationManualProjectParam) {
        // 1 根据项目ID获取指标名称为B赛题的场景ID
        List<String> videoList = new ArrayList<>();
        List<String> runResultPaths = simulationManualProjectTaskMapper.selectRunResultPathByProjectIdV202403(simulationManualProjectParam);
        for (String runResultPath : runResultPaths) {
            ResponseBodyVO<List<String>> list = fileDownService.list(MinioParameter.builder().objectName(runResultPath).build());
            List<String> info = list.getInfo();
            for (String s : info) {
                if (!s.contains(".mp4")) {
                    continue;
                }
                MinioParameter minioParameter = new MinioParameter();
                minioParameter.setObjectName(s);
                ResponseBodyVO<String> preview = fileDownService.getPreviewUrl(minioParameter);
                videoList.add(preview.getInfo());
            }
        }
        return videoList;
    }
}
