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.reportStatistics.service.IDriverFilePrintService;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import com.hotwj.platform.securityManagement.violationInfo.domain.bo.HotViolationInfoBo;
import com.hotwj.platform.securityManagement.violationInfo.domain.vo.HotViolationInfoVo;
import com.hotwj.platform.securityManagement.violationInfo.service.IHotViolationInfoService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.service.DictService;
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 ViolationRecordProvider implements IReportDataProvider {

    private final IHotDriverService driverService;
    private final IHotViolationInfoService violationInfoService;
    private final DictService dictService;

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

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

    @Override
    public String getReportTitle() {
        return "驾驶员违章记录";
    }

    @Override
    public boolean isLandscape() {
        return true;
    }

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

        HotDriverVo driver = driverService.queryById(driverId);
        if (driver == null) {
            return new HashMap<>();
        }

        Map<String, Object> data = new HashMap<>();
        data.put("driverName", driver.getName());

        // Fetch violations
        HotViolationInfoBo bo = new HotViolationInfoBo();
        bo.setDriverId(driverId);
        bo.setIsDeleted(0L);
        List<HotViolationInfoVo> list = violationInfoService.queryList(bo);

        List<Map<String, Object>> violations = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            for (HotViolationInfoVo vo : list) {
                Map<String, Object> map = new HashMap<>();
                map.put("violationTime", vo.getOccurTime() != null ? DateUtil.format(vo.getOccurTime(), "yyyy-MM-dd") : "");
                map.put("vehiclePlate", vo.getPlateNumber());
                map.put("violationLocation", vo.getViolationPlace());
                map.put("violationContent", vo.getViolationContent());
                map.put("violationType", dictService.getDictLabel("hot_violation_type", String.valueOf(vo.getViolationType())));
                map.put("fineAmount", vo.getFineAmount() != null ? vo.getFineAmount() : 0);
                map.put("deductedPoints", vo.getDeductPoints() != null ? vo.getDeductPoints() : 0);
                map.put("handlingStatus", vo.getDisposalResult());
                map.put("remark", vo.getRemark());
                violations.add(map);
            }
        }
        data.put("violations", violations);

        return data;
    }
}
