package com.tuoheng.admin.service.airData.report;

import com.lowagie.text.*;
import com.tuoheng.admin.entity.domain.AirDataType;
import com.tuoheng.admin.entity.request.report.ExportReportRequest;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.utils.WordUtils;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
public class GenerateReportWordService {

    @Autowired
    private UserMapper userMapper;

    /**
     * @param filepath
     * @param request
     * @param airDataType
     * @param detectionTime
     * @param equipmentMountCode
     *
     * @return
     */
    public JsonResult buildWord(String filepath, ExportReportRequest request, AirDataType airDataType, String detectionTime,String equipmentMountCode) {
        WordUtils wordUtils = new WordUtils();
        wordUtils.getDocument().setPageSize(PageSize.A4);
        wordUtils.getDocument().setMargins(71f, 71f, 72f, 72f);
        try {
            wordUtils.openDocument(filepath);
            this.editDataTypeName(airDataType);
            // 标题
//            String titleName = airDataType.getDataType() + "报告";
//            wordUtils.insertTitlePattern(titleName, wordUtils.rtfGsBt1);

            wordUtils.insertContext(airDataType.getDataType() + "浓度分布", 14, Font.BOLD, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext("检测时间:" + detectionTime, 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext("监测设备:" + equipmentMountCode, 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext("检测方式:" + airDataType.getDetection(), 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext("采样点数量:" + request.getSamplingPointsNum(), 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext("检测区域网格平均尺寸:" + request.getAverageGridSize(), 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext("总检测区域网格面积:" + request.getGridArea(), 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext("检测区域中心点经纬度:" + request.getCenterPointLongitudeAndLatitude(), 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext(airDataType.getDataType() + "检测区域平均浓度:" + request.getAverageConcentration(), 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext(airDataType.getDataType() + "网格浓度最高值:" + request.getGridConcentrationMax(), 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext(airDataType.getDataType() + "网格浓度最小值:" + request.getGridConcentrationMix(), 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext(airDataType.getDataType() + "单点浓度最高值:" + request.getSinglePointConcentrationMax(), 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertContext(airDataType.getDataType() + "单点浓度最低值:" + request.getSinglePointConcentrationMin(), 10, Font.NORMAL, Element.ALIGN_LEFT, 20, 2, 2);
            wordUtils.insertImg(request.getOssUrl(), Image.ALIGN_CENTER, 500, 400, 100, 100, 100, 0);
            wordUtils.insertImg(airDataType.getReferenceIndicatorHorizontalImage(), Image.ALIGN_CENTER, 500, 10, 100, 10, 10, 0);
        } catch (DocumentException e) {
            log.info("生成报告word异常");
            throw new RuntimeException(e);
        } catch (IOException e) {
            log.info("生成报告word异常");
            throw new RuntimeException(e);
        } finally {
            try {
                wordUtils.closeDocument();
            } catch (DocumentException e) {
                throw new RuntimeException(e);
            }
        }
        return JsonResult.success();
    }

    /**
     * 修改pm2.5和pm10的标题字段
     * @param airDataType
     * @return
     */
    public String editDataTypeName(AirDataType airDataType){
        if("pm_10".equals(airDataType.getDataType())){
            airDataType.setDataType("pm10");
        }else if ("pm_2_5".equals(airDataType.getDataType())){
            airDataType.setDataType("pm2.5");
        }
        return airDataType.getDataType();
    }
}
