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

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.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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆证件信息（行驶证、运输证）数据提供者
 *
 * @author shihongwei
 */
@Component
@RequiredArgsConstructor
public class VehicleLicenseProvider implements IReportDataProvider {

    private final IHotVehicleService hotVehicleService;
    private final OssService ossService;

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

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

    @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 = hotVehicleService.queryById(vehicleId);
        if (vehicle == null) {
            return new HashMap<>();
        }

        Map<String, Object> ctx = new HashMap<>();
        ctx.put("vehicle", vehicle);

        // Images
        Map<String, List<String>> images = new HashMap<>();
        // 主页
        images.put("dlMain", splitUrls(ossService.selectUrlByIds(vehicle.getDrivingLicenseMainUrls())));
        // 副页
        images.put("dlAux", splitUrls(ossService.selectUrlByIds(vehicle.getDrivingLicenseAuxUrls())));
        images.put("dlOther", splitUrls(ossService.selectUrlByIds(vehicle.getDrivingLicenseOtherUrl())));

        // Transport License Images
        images.put("tl", splitUrls(ossService.selectUrlByIds(vehicle.getTransportLicenseUrls())));

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