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

import cn.hutool.core.collection.CollUtil;
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.driverFamilyMember.domain.bo.HotDriverFamilyMemberBo;
import com.hotwj.platform.driverManagement.driverFamilyMember.domain.vo.HotDriverFamilyMemberVo;
import com.hotwj.platform.driverManagement.driverFamilyMember.service.IHotDriverFamilyMemberService;
import com.hotwj.platform.driverManagement.driverWorkExperience.domain.bo.HotDriverWorkExperienceBo;
import com.hotwj.platform.driverManagement.driverWorkExperience.domain.vo.HotDriverWorkExperienceVo;
import com.hotwj.platform.driverManagement.driverWorkExperience.service.IHotDriverWorkExperienceService;
import com.hotwj.platform.reportStatistics.service.IDriverFilePrintService;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.service.OssService;
import org.springframework.stereotype.Component;

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

/**
 * 驾驶员应聘表数据提供者
 *
 * @author shihongwei
 */
@Component("driverApplicationFormProvider")
@RequiredArgsConstructor
public class DriverApplicationFormProvider implements IReportDataProvider {

    private final IHotDriverService driverService;
    private final IHotDriverFamilyMemberService driverFamilyMemberService;
    private final IHotDriverWorkExperienceService workExperienceService;
    private final OssService ossService;

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

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

    @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();
        }
        if (StrUtil.isNotBlank(driver.getSignatureUrl())
                && !StrUtil.startWithAnyIgnoreCase(driver.getSignatureUrl(), "http://", "https://")) {
            String url = ossService.selectUrlByIds(driver.getSignatureUrl());
            if (StrUtil.isNotBlank(url)) {
                driver.setSignatureUrl(url);
            }
        }

        Map<String, Object> data = new HashMap<>();

        // 字典翻译交由 tool 处理，此处仅需放入原始值或对象
        data.put("driver", driver);

        // 家庭成员
        HotDriverFamilyMemberBo familyBo = new HotDriverFamilyMemberBo();
        familyBo.setDriverId(driver.getId());
        List<HotDriverFamilyMemberVo> familyMembers = driverFamilyMemberService.queryList(familyBo);

        // 补全家庭成员到2行，保持表格美观
        int familyRows = 2;
        List<Map<String, Object>> familyList = new ArrayList<>();
        if (CollUtil.isNotEmpty(familyMembers)) {
            for (HotDriverFamilyMemberVo member : familyMembers) {
                Map<String, Object> m = new HashMap<>();
                m.put("name", member.getName());
                m.put("relationship", member.getRelationship());
                m.put("birthMonth", member.getBirthMonth()); // 放入 Date 对象
                m.put("phone", member.getPhone());
                familyList.add(m);
            }
        }
        // 填充空行
        while (familyList.size() < familyRows) {
            familyList.add(new HashMap<>());
        }
        data.put("familyMembers", familyList);

        // 工作经历
        HotDriverWorkExperienceBo workBo = new HotDriverWorkExperienceBo();
        workBo.setDriverId(driverId);
        List<HotDriverWorkExperienceVo> workExperiences = workExperienceService.queryList(workBo);

        // 补全工作经历到3行
        int workRows = 3;
        List<Map<String, Object>> workList = new ArrayList<>();
        if (CollUtil.isNotEmpty(workExperiences)) {
            for (HotDriverWorkExperienceVo work : workExperiences) {
                Map<String, Object> m = new HashMap<>();
                m.put("startDate", work.getStartDate());
                m.put("endDate", work.getEndDate());
                m.put("employer", work.getEmployer());
                m.put("jobTitle", work.getJobTitle());
                m.put("description", work.getDescription()); // 增加 description 字段
                workList.add(m);
            }
        }

        // 填充空行
        while (workList.size() < workRows) {
            workList.add(new HashMap<>());
        }
        data.put("workExperiences", workList);

        return data;
    }
}
