package com.zksy.WaterSupply.PipeNetworkStatistics.service.impl;

import com.zksy.WaterSupply.PipeNetworkStatistics.dto.MetricItemDTO;
import com.zksy.WaterSupply.PipeNetworkStatistics.dto.PipeNetworkStatisticsDashboardDTO;
import com.zksy.WaterSupply.PipeNetworkStatistics.service.PipeNetworkStatisticsExportService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class PipeNetworkStatisticsExportServiceImpl implements PipeNetworkStatisticsExportService {
    private static final String[] HEADERS = {"指标", "分组", "数值", "单位"};

    @Override
    public void write(OutputStream outputStream, PipeNetworkStatisticsDashboardDTO dashboard) throws IOException {
        try (Workbook workbook = new XSSFWorkbook()) {
            writeOverview(workbook.createSheet("综合概览"), dashboard);
            writeMapSheet(workbook.createSheet("管网统计"), dashboard.getPipeNetwork());
            writeMapSheet(workbook.createSheet("水厂统计"), dashboard.getWaterPlant());
            writeMapSheet(workbook.createSheet("泵站统计"), dashboard.getPumpStation());
            writeMapSheet(workbook.createSheet("水源地统计"), dashboard.getWaterSource());
            writeMapSheet(workbook.createSheet("大用水户统计"), dashboard.getWaterUser());
            workbook.write(outputStream);
        }
    }

    private void writeOverview(Sheet sheet, PipeNetworkStatisticsDashboardDTO dashboard) {
        header(sheet);
        int rowIndex = 1;
        if (dashboard.getFacilityOverview() != null) {
            for (var item : dashboard.getFacilityOverview()) {
                Row row = sheet.createRow(rowIndex++);
                row.createCell(0).setCellValue("设施总数");
                row.createCell(1).setCellValue(item.getFacilityTypeName());
                row.createCell(2).setCellValue(item.getTotalCount() == null ? 0 : item.getTotalCount());
                row.createCell(3).setCellValue("项");
            }
        }
    }

    private void writeMapSheet(Sheet sheet, Map<String, Object> values) {
        header(sheet);
        if (values == null) return;
        int rowIndex = 1;
        for (Map.Entry<String, Object> entry : values.entrySet()) {
            Object value = entry.getValue();
            if (value instanceof List) {
                for (Object item : (List<?>) value) {
                    Row row = sheet.createRow(rowIndex++);
                    if (item instanceof MetricItemDTO) {
                        MetricItemDTO metric = (MetricItemDTO) item;
                        row.createCell(0).setCellValue(entry.getKey());
                        row.createCell(1).setCellValue(metric.getName());
                        setValue(row, 2, metric.getValue());
                        row.createCell(3).setCellValue(metric.getUnit());
                    }
                }
            } else {
                Row row = sheet.createRow(rowIndex++);
                row.createCell(0).setCellValue(entry.getKey());
                row.createCell(1).setCellValue("");
                setValue(row, 2, value);
                row.createCell(3).setCellValue("");
            }
        }
    }

    private void header(Sheet sheet) {
        Row row = sheet.createRow(0);
        for (int i = 0; i < HEADERS.length; i++) row.createCell(i).setCellValue(HEADERS[i]);
    }

    private void setValue(Row row, int index, Object value) {
        if (value instanceof Number) row.createCell(index).setCellValue(((Number) value).doubleValue());
        else if (value instanceof BigDecimal) row.createCell(index).setCellValue(((BigDecimal) value).doubleValue());
        else row.createCell(index).setCellValue(value == null ? "" : String.valueOf(value));
    }
}
