package com.yaoyicloud.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

import com.yaoyicloud.config.FilerepoProperties;
import com.yaoyicloud.constant.enums.ModuleType;
import com.yaoyicloud.constant.enums.ReportType;
import com.yaoyicloud.render.AntiBriberyRender;
import com.yaoyicloud.render.AttachmentSectionRender;
import com.yaoyicloud.render.BasicInfoRender;
import com.yaoyicloud.render.ServiceProviderInfoRender;
import com.yaoyicloud.service.ReportService;
import com.yaoyicloud.template.AcademicAssociationReport;
import com.yaoyicloud.template.FoundationReport;
import com.yaoyicloud.template.PlatformCompanyReport;

import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

/**
 * 报告服务
 *
 * @author snows
 * @date 2024/10/12
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {
    private final PlatformCompanyReport platformCompanyReport;
    private final AcademicAssociationReport academicAssociationReport;
    private final FoundationReport foundationReport;
    private final FilerepoProperties filerepoProperties;

    @SuppressWarnings("checkstyle:ReturnCount")
    @Override
    public String createPlusVersionCheckReport(
        ReportType reportType, String data, byte[] templateBytes, String outputBasePath, Long relationId,
        HttpServletRequest request, ModuleType moduleType)
        throws Exception {

        // 1. 将字节流模版写入临时文件
        File tempTemplateFile = File.createTempFile("template_", ".docx");
        try (FileOutputStream fos = new FileOutputStream(tempTemplateFile)) {
            fos.write(templateBytes);
        }
        Map<String, Object> processedData = new HashMap<>();
        processedData.put("reportDate", LocalDate.now().toString());
        processedData.put("type", reportType.getDesc());
        String sessionId = request.getSession().getId();
        String reportPath = null;
        switch (moduleType) {
            case ANTIBRIBERY:
                reportPath =
                    new AntiBriberyRender("temp", filerepoProperties).renderDocx(data, processedData, templateBytes,
                        String.valueOf(relationId));
                break;
            case PLATFORM_COMPANY_BASICINFO:
                reportPath =
                    new BasicInfoRender("temp", filerepoProperties).renderDocx(data, processedData, templateBytes,
                        String.valueOf(relationId));
                break;
            case PLATFORM_COMPANY_ATTACHMENTSECTION:
                reportPath = new AttachmentSectionRender("temp", filerepoProperties).renderDocx(data, processedData,
                    templateBytes, String.valueOf(relationId));
                break;
            case COMMON:
                reportPath = new ServiceProviderInfoRender("temp", filerepoProperties).renderDocx(data, processedData,
                    templateBytes, String.valueOf(relationId));
                break;
            default:
                throw new UnsupportedOperationException("Unsupported module type: " + moduleType);

        }
        return reportPath;
    }

}
