package com.hotwj.platform.reportStatistics.provider.driver;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hotwj.platform.driverManagement.driver.domain.vo.HotDriverVo;
import com.hotwj.platform.driverManagement.driver.service.IHotDriverService;
import com.hotwj.platform.driverManagement.driverSkillAssessment.domain.bo.HotDriverSkillAssessmentBo;
import com.hotwj.platform.driverManagement.driverSkillAssessment.domain.vo.HotDriverSkillAssessmentVo;
import com.hotwj.platform.driverManagement.driverSkillAssessment.service.IHotDriverSkillAssessmentService;
import com.hotwj.platform.reportStatistics.service.IDriverFilePrintService;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.service.DictService;
import org.dromara.common.core.service.OssService;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 驾驶员资格审查及技能考核登记表数据提供者
 *
 * @author shihongwei
 */
@Component
@RequiredArgsConstructor
public class QualificationReviewProvider implements IReportDataProvider {

    private final IHotDriverService driverService;
    private final IHotDriverSkillAssessmentService driverSkillAssessmentService;
    private final DictService dictService;
    private final OssService ossService;

    @Override
    public String getTemplateCode() {
        return "templates/driver/qualificationReview.html.vm";
    }

    @Override
    public String getReportType() {
        return IDriverFilePrintService.TYPE_QUALIFICATION_REVIEW;
    }

    @Override
    public String getReportTitle() {
        return "驾驶员资格审查及技能考核登记表";
    }

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {
        String driverId = MapUtil.getStr(params, "driverId");
        if (StrUtil.isBlank(driverId)) {
            return MapUtil.empty();
        }

        HotDriverVo driver = driverService.queryById(driverId);
        if (driver == null) {
            return MapUtil.empty();
        }

        Map<String, Object> ctx = new HashMap<>();
        
        // 字典翻译
        String genderLabel = dictService.getDictLabel("sys_user_sex", driver.getGender());
        String nationLabel = dictService.getDictLabel("nation", driver.getNation());
        String educationLabel = dictService.getDictLabel("hot_academic", driver.getEducation());

        if (genderLabel != null) driver.setGender(genderLabel);
        if (nationLabel != null) driver.setNation(nationLabel);
        if (educationLabel != null) driver.setEducation(educationLabel);

        // 格式化日期字段为 yyyy-MM-dd
        // 注意：HotDriverVo 中的日期字段是 Date 类型，如果直接修改为 String 需要使用额外字段或者Map
        // 这里我们可以创建一个 Map 来覆盖 driver 中的日期字段
        Map<String, Object> driverMap = MapUtil.newHashMap();
        // 复制 driver 的属性到 map (这里简单起见，我们直接put需要修改的字段)
        // 或者更简单的，我们把格式化后的日期字符串放入 ctx，模板里优先使用 ctx 中的变量

        if (driver.getDriverLicenseLongTerm() != null && driver.getDriverLicenseLongTerm() == 1L) {
            ctx.put("licenseValidityStr", "长期");
        } else if (driver.getDriverLicenseExpireDate() != null) {
            ctx.put("licenseValidityStr", DateUtil.format(driver.getDriverLicenseExpireDate(), "yyyy-MM-dd"));
        }
        if (driver.getQualificationValidEndDate() != null) {
            ctx.put("qualificationValidityStr", DateUtil.format(driver.getQualificationValidEndDate(), "yyyy-MM-dd"));
        }
        
        ctx.put("driver", driver);

        // 组装数据
        HotDriverSkillAssessmentBo assessmentBo = new HotDriverSkillAssessmentBo();
        assessmentBo.setDriverId(driverId);
        assessmentBo.setIsDeleted(0L);
        List<HotDriverSkillAssessmentVo> assessments = driverSkillAssessmentService.queryList(assessmentBo);

        if (!assessments.isEmpty()) {
            HotDriverSkillAssessmentVo assessment = assessments.get(0);
            ctx.put("examVehicleModel", assessment.getExamVehicleModel());
            ctx.put("assessDate", assessment.getAssessDate()); // Let template format
            ctx.put("examRoute", assessment.getExamRoute());
            ctx.put("drivingExperience", assessment.getDrivingExperience());
            ctx.put("drivingMileage", assessment.getDrivingMileage());

            ctx.put("chiefOpinion", assessment.getChiefOpinion());
            ctx.put("invigilatorOpinion", assessment.getInvigilatorOpinion());
            ctx.put("companyLeaderOpinion", assessment.getCompanyLeaderOpinion());

            // 主考人员和监考人员签名 (转换为 URL)
            ctx.put("chiefExaminerSignature", ossService.selectUrlByIds(assessment.getChiefExaminerSignature()));
            ctx.put("invigilatorSignature", ossService.selectUrlByIds(assessment.getInvigilatorSignature()));

            ctx.put("assessment", assessment);
        }

        return ctx;
    }
}
