package com.ningdatech.carapi.driver.assembler;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.ningdatech.carapi.common.model.FileBasicInfo;
import com.ningdatech.carapi.driver.constant.*;
import com.ningdatech.carapi.driver.entity.TrainingMaterial;
import com.ningdatech.carapi.driver.entity.TrainingOrganization;
import com.ningdatech.carapi.driver.entity.TrainingOrganizationTrainer;
import com.ningdatech.carapi.driver.model.TrainingBasicInfo;
import com.ningdatech.carapi.driver.model.TrainingMaterialInfo;
import com.ningdatech.carapi.driver.model.TrainingMaterialOverviewInfo;
import com.ningdatech.carapi.driver.model.vo.OrganizationTrainingTrainersInfo;

/**
 * @author liuxinxin
 * @date 2022/10/22 下午3:59
 */

public class TrainingAssembler {

    public static TrainingBasicInfo buildTrainingBasicInfo(TrainingOrganization trainingOrganization) {
        // 装配培训基本信息
        TrainingBasicInfo trainingBasicInfo = new TrainingBasicInfo();
        trainingBasicInfo.setAddress(trainingOrganization.getAddress());
        trainingBasicInfo.setContactNumber(trainingOrganization.getContactNumber());
        trainingBasicInfo.setContacts(trainingOrganization.getContacts());
        trainingBasicInfo.setRemark(trainingOrganization.getRemark());
        trainingBasicInfo.setTrainingStartTime(trainingOrganization.getTrainingStartTime());
        trainingBasicInfo.setTrainingEndTime(trainingOrganization.getTrainingEndTime());
        if (Objects.nonNull(trainingOrganization.getTrainingMode())){
            trainingBasicInfo.setTrainingMode(TrainingModeEnum.valueOf(trainingOrganization.getTrainingMode()));
        }
        if (Objects.nonNull(trainingOrganization.getTrainingStatus())) {
            trainingBasicInfo.setTrainingStatus(TrainingStatusEnum.valueOf(trainingOrganization.getTrainingStatus()));
        }
        trainingBasicInfo.setTrainingTopic(trainingOrganization.getTrainingTopic());
        if (Objects.nonNull(trainingOrganization.getTrainingType())) {
            trainingBasicInfo.setTrainingType(TrainingTypeEnum.valueOf(trainingOrganization.getTrainingType()));
        }
        return trainingBasicInfo;
    }

    public static TrainingMaterialOverviewInfo buildTrainingMaterialOverviewInfo(List<TrainingMaterial> materialList) {
        List<TrainingMaterialInfo> trainingMaterialInfoList = buildTrainingMaterialInfoList(materialList);
        Map<MaterialTypeEnum, List<TrainingMaterialInfo>> trainingMaterialMap = trainingMaterialInfoList.stream()
                .collect(Collectors.groupingBy(TrainingMaterialInfo::getMaterialType));
        TrainingMaterialOverviewInfo trainingMaterialOverviewInfo = new TrainingMaterialOverviewInfo();
        trainingMaterialOverviewInfo.setRuleSystemMaterialList(trainingMaterialMap.get(MaterialTypeEnum.rule_system));
        trainingMaterialOverviewInfo.setPolicyDocumentsMaterialInfoList(trainingMaterialMap.get(MaterialTypeEnum.policy_documents));
        trainingMaterialOverviewInfo.setSafetyKnowledgeMaterialInfoList(trainingMaterialMap.get(MaterialTypeEnum.safety_knowledge));
        trainingMaterialOverviewInfo.setAccidentCaseMaterialInfoList(trainingMaterialMap.get(MaterialTypeEnum.accident_case));
        trainingMaterialOverviewInfo.setTrainingVideoMaterialInfoList(trainingMaterialMap.get(MaterialTypeEnum.training_video));
        trainingMaterialOverviewInfo.setOtherMaterialInfoList(trainingMaterialMap.get(MaterialTypeEnum.other));
        return trainingMaterialOverviewInfo;
    }

    public static OrganizationTrainingTrainersInfo buildOrganizationTrainingTrainersInfo(TrainingOrganizationTrainer trainer) {
        OrganizationTrainingTrainersInfo organizationTrainingTrainersInfo = new OrganizationTrainingTrainersInfo();
        organizationTrainingTrainersInfo.setId(trainer.getId());
        organizationTrainingTrainersInfo.setDriverId(trainer.getDriverId());
        organizationTrainingTrainersInfo.setDriverName(trainer.getDriverName());
        organizationTrainingTrainersInfo.setCompanyId(trainer.getCompanyId());
        organizationTrainingTrainersInfo.setCompanyName(trainer.getCompanyName());
        organizationTrainingTrainersInfo.setDriverId(trainer.getDriverId());
        organizationTrainingTrainersInfo.setDriverUserId(trainer.getDriverUserId());
        if (StringUtils.isNotBlank(trainer.getExaminationStatus())) {
            organizationTrainingTrainersInfo.setExaminationStatus(ExaminationStatusEnum.valueOf(trainer.getExaminationStatus()));
        }
        organizationTrainingTrainersInfo.setExamPaperId(trainer.getExamPaperId());
        organizationTrainingTrainersInfo.setExamPaperName(trainer.getExamPaperName());
        organizationTrainingTrainersInfo.setExamRegistrationNo(trainer.getExamRegistrationNo());
        organizationTrainingTrainersInfo.setScore(trainer.getScore());
        organizationTrainingTrainersInfo.setIsQualified(trainer.getIsQualified());
        return organizationTrainingTrainersInfo;
    }

    public static List<TrainingMaterialInfo> buildTrainingMaterialInfoList(List<TrainingMaterial> materialList) {
        return materialList.stream().map(r -> {
            TrainingMaterialInfo trainingMaterialInfo = new TrainingMaterialInfo();
            String appendixListJson = r.getAppendixListJson();
            List<FileBasicInfo> appendixList = JSONObject.parseArray(appendixListJson,FileBasicInfo.class);
            trainingMaterialInfo.setTitle(r.getTitle());
            trainingMaterialInfo.setContent(r.getContent());
            trainingMaterialInfo.setMaterialType(MaterialTypeEnum.valueOf(r.getMaterialType()));
            trainingMaterialInfo.setTrainingMaterialId(r.getId());
            trainingMaterialInfo.setAppendixList(appendixList);
            return trainingMaterialInfo;
        }).collect(Collectors.toList());
    }
}
