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

import cn.hutool.core.collection.CollUtil;
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.driverHealthReport.domain.bo.HotDriverHealthReportBo;
import com.hotwj.platform.driverManagement.driverHealthReport.domain.vo.HotDriverHealthReportVo;
import com.hotwj.platform.driverManagement.driverHealthReport.service.IHotDriverHealthReportService;
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.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Component;

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

/**
 * 驾驶员体检报告数据提供者
 *
 * @author shihongwei
 */
@Component
@RequiredArgsConstructor
public class MedicalExaminationReportProvider implements IReportDataProvider {

    private final IHotDriverService driverService;
    private final IHotDriverHealthReportService healthReportService;
    private final DictService dictService;
    private final OssService ossService;

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

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

    @Override
    public String getReportTitle() {
        return "驾驶员体检报告";
    }

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {
        Map<String, Object> ctx = new HashMap<>();
        ctx.put("name", "");
        ctx.put("idCardNo", "");
        ctx.put("reportDate", "");
        ctx.put("healthStatus", "");
        ctx.put("hospital", "");
        ctx.put("remark", "");
        ctx.put("imageUrls", new ArrayList<>());

        String driverId = MapUtil.getStr(params, "driverId");
        if (StrUtil.isBlank(driverId)) {
            return ctx;
        }

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

        ctx.put("name", driver.getName());
        ctx.put("idCardNo", driver.getIdCardNo());

        HotDriverHealthReportBo bo = new HotDriverHealthReportBo();
        bo.setDriverId(driverId);
        bo.setIsDeleted(0L);
        List<HotDriverHealthReportVo> list = healthReportService.queryList(bo);

        String reportDate = "";
        String healthStatusLabel = "";
        List<String> imageUrls = new ArrayList<>();
        String hospital = "";
        String remark = "";

        if (CollUtil.isNotEmpty(list)) {
            HotDriverHealthReportVo vo = list.get(0);
            reportDate = vo.getReportDate() != null ? DateUtil.format(vo.getReportDate(), "yyyy-MM-dd") : "";
            hospital = vo.getHospital();
            remark = vo.getRemark();

            if (StringUtils.isNotBlank(vo.getHealthStatus())) {
                String label = dictService.getDictLabel("hot_health", vo.getHealthStatus());
                healthStatusLabel = StringUtils.isBlank(label) ? vo.getHealthStatus() : label;
            }

            // Image handling - keeping original logic of splitUrls and ossService
            if (StringUtils.isNotBlank(vo.getPreviewImageUrl())) {
                imageUrls = splitUrls(ossService.selectUrlByIds(vo.getPreviewImageUrl()));
            } else if (StringUtils.isNotBlank(vo.getAttachmentUrl())) {
                imageUrls = splitUrls(ossService.selectUrlByIds(vo.getAttachmentUrl()));
            }
        }

        ctx.put("reportDate", reportDate);
        ctx.put("healthStatus", healthStatusLabel);
        ctx.put("hospital", hospital);
        ctx.put("remark", remark);
        ctx.put("imageUrls", imageUrls);

        return ctx;
    }

    // Helper method replicated from original service
    private List<String> splitUrls(String urls) {
        if (StrUtil.isBlank(urls)) {
            return new ArrayList<>();
        }
        List<String> list = new ArrayList<>();
        String[] parts = urls.split(",");
        for (String p : parts) {
            String v = p.trim();
            if (StrUtil.isNotBlank(v)) {
                list.add(v);
            }
        }
        return list;
    }
}
