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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.hotwj.platform.common.mapper.PublicMapper;
import com.hotwj.platform.common.vo.AddressVo;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import com.hotwj.platform.resourceManagement.company.domain.vo.SysCompanyVo;
import com.hotwj.platform.resourceManagement.company.service.ISysCompanyService;
import com.hotwj.platform.securityManagement.violationInfo.domain.vo.HotViolationInfoVo;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.service.OssService;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@RequiredArgsConstructor
public class GpsViolationProvider implements IReportDataProvider {

    private final ISysCompanyService sysCompanyService;
    private final PublicMapper publicMapper;
    private final OssService ossService;

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

    @Override
    public String getReportTitle() {
        return "车辆动态监控违法行为处理记录";
    }

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {
        String mode = MapUtil.getStr(params, "mode");
        Long companyId = MapUtil.getLong(params, "companyId");

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

        SysCompanyVo company = sysCompanyService.queryById(companyId);
        String companyName = "";
        String regionName = "";
        if (company != null) {
            companyName = StringUtils.blankToDefault(company.getCompanyName(), "");
            if (StringUtils.isNotBlank(company.getRegionCode())) {
                AddressVo addressVo = publicMapper.selectAddressByCode(company.getRegionCode());
                if (addressVo != null) {
                    regionName = addressVo.getLabel();
                }
            }
        }
        ctx.put("companyName", companyName);

        if ("cover".equals(mode)) {
            ctx.put("regionName", regionName);
            ctx.put("printYear", DateUtil.format(new Date(), "yyyy"));
        } else if ("detail".equals(mode)) {
            HotViolationInfoVo info = (HotViolationInfoVo) params.get("info");
            if (info != null) {
                ctx.put("info", info);
                Date recordTime = info.getCreateTime() != null ? info.getCreateTime() : info.getOccurTime();
                ctx.put("recordTime", recordTime);
                ctx.put("responsibleSignatureUrl", resolveMaybeOssUrl(info.getResponsibleSignatureImage()));
                ctx.put("partySignatureUrl", resolveMaybeOssUrl(info.getPartySignatureImage()));
            }
        } else if ("attachment".equals(mode)) {
            HotViolationInfoVo info = (HotViolationInfoVo) params.get("info");
            if (info != null) {
                String promise = ossService.selectUrlByIds(info.getPromiseUrls());
                ctx.put("promiseImages", splitUrls(promise));
            }
        }

        return ctx;
    }

    private String resolveMaybeOssUrl(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        String first = value.split(",")[0].trim();
        if (StringUtils.isBlank(first)) {
            return null;
        }
        if (StringUtils.ishttp(first)) {
            return first;
        }
        if (first.matches("^\\d+$")) {
            String urls = ossService.selectUrlByIds(first);
            if (StringUtils.isBlank(urls)) {
                return null;
            }
            return urls.split(",")[0].trim();
        }
        return first;
    }

    private List<String> splitUrls(String urls) {
        List<String> result = new ArrayList<>();
        if (StringUtils.isBlank(urls)) {
            return result;
        }
        for (String s : urls.split(",")) {
            String t = s == null ? "" : s.trim();
            if (!t.isEmpty()) {
                result.add(t);
            }
        }
        return result;
    }
}
