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

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.safetyInvestment.domain.vo.HotSafetyInvestmentVo;
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.HashMap;
import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class SafetyProductionInputProvider implements IReportDataProvider {

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

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

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

    @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)) {
            String year = MapUtil.getStr(params, "year");
            ctx.put("regionName", regionName);
            ctx.put("printYear", year);
        } else if ("detail".equals(mode)) {
            String year = MapUtil.getStr(params, "year");
            Long totalAmount = MapUtil.getLong(params, "totalAmount");
            List<HotSafetyInvestmentVo> records = (List<HotSafetyInvestmentVo>) params.get("records");

            if (records != null) {
                for (HotSafetyInvestmentVo record : records) {
                    if (StringUtils.isNotBlank(record.getResponsibleSignatureId())) {
                        record.setResponsibleSignatureUrl(resolveMaybeOssUrl(record.getResponsibleSignatureId()));
                    }
                }
            }

            ctx.put("printYear", year);
            ctx.put("totalAmount", totalAmount);
            ctx.put("records", records);
        } else if ("receipt".equals(mode)) {
            List<String> receiptImages = (List<String>) params.get("receiptImages");
            ctx.put("receiptImages", receiptImages);
        }

        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 (first.startsWith("http")) {
            return first;
        }
        if (first.matches("^\\d+$")) {
            String urls = ossService.selectUrlByIds(first);
            if (StringUtils.isBlank(urls)) {
                return null;
            }
            return urls.split(",")[0].trim();
        }
        return first;
    }
}
