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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import com.hotwj.platform.reportStatistics.service.IVehicleFilePrintService;
import com.hotwj.platform.resourceManagement.vehicleAnnualReview.domain.vo.HotVehicleAnnualReviewVo;
import com.hotwj.platform.resourceManagement.vehicleAnnualReview.service.IHotVehicleAnnualReviewService;
import com.hotwj.platform.resourceManagement.vehicleManagement.domain.vo.HotVehicleVo;
import com.hotwj.platform.resourceManagement.vehicleManagement.service.IHotVehicleService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.service.OssService;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 车辆年审报告数据提供者
 *
 * @author shihongwei
 */
@Component
@RequiredArgsConstructor
public class VehicleInspectionReportProvider implements IReportDataProvider {

    private final IHotVehicleAnnualReviewService annualReviewService;
    private final IHotVehicleService vehicleService;
    private final OssService ossService;

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

    @Override
    public String getReportType() {
        return IVehicleFilePrintService.TYPE_INSPECTION_REPORT;
    }

    @Override
    public String getReportTitle() {
        return "安全技术检测报告";
    }

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {
        Long vehicleId = MapUtil.getLong(params, "vehicleId");
        if (vehicleId == null) {
            return new HashMap<>();
        }

        HotVehicleVo vehicle = vehicleService.queryById(vehicleId);

        HotVehicleAnnualReviewVo review = null;
        Object reviewObj = params.get("review");
        if (reviewObj instanceof HotVehicleAnnualReviewVo) {
            review = (HotVehicleAnnualReviewVo) reviewObj;
        }
        if (review == null) {
            String inspectionStartDate = MapUtil.getStr(params, "inspectionStartDate");
            String inspectionEndDate = MapUtil.getStr(params, "inspectionEndDate");
            Date startDate = null;
            Date endDate = null;
            if (StrUtil.isNotBlank(inspectionStartDate)) {
                startDate = DateUtil.beginOfDay(DateUtil.parse(inspectionStartDate, "yyyy-MM-dd"));
            }
            if (StrUtil.isNotBlank(inspectionEndDate)) {
                endDate = DateUtil.endOfDay(DateUtil.parse(inspectionEndDate, "yyyy-MM-dd"));
            }

            if (startDate != null || endDate != null) {
                review = annualReviewService.queryLatestByVehicleIdAndReviewDateRange(String.valueOf(vehicleId), startDate, endDate);
            } else {
                review = annualReviewService.queryLatestByVehicleId(String.valueOf(vehicleId));
            }
        }


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

        ctx.put("vehicle", vehicle);
        ctx.put("review", review);

        List<String> images = new ArrayList<>();
        if (review != null) {
            List<String> strings = splitUrls(ossService.selectUrlByIds(review.getImageUrls()));
            images.addAll(strings);
        }
        ctx.put("images", images);

        return ctx;
    }

    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;
    }
}
