package com.limap.common.utils.poi.parser;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;
import java.util.Objects;

/**
 * <p>Word内容解析器</p>
 *
 * @author Allen Yang
 * @datetime 2021/11/18 14:46
 */
@Setter
@Getter
@Slf4j
public abstract class WordContentParser {

    public static final String ERR_MSG_DOC_LOAD_FAIL = "文档加载失败";
    public static final String ERR_MSG_DOC_OUTPUT_FAIL = "文档导出失败";
    public static final String ERR_MSG_DOC_NOT_LOADED = "文档未加载";
    public static final String ERR_MSG_PARAM_NOT_INITIALIZED = "替换参数未初始化";
    public static final String ERR_MSG_ROW_ELEMENT_MISMATCH = "下标${index}的行值元素不匹配";
    public static final String ERR_MSG_TABLE_NOT_EXIST = "不存在下标${index}的表格";
    public static final String ERR_MSG_CELLS_NOT_UNANIMOUS = "下标${index}的表格值和单元格个数不一致";
    public static final String ERR_MSG_TEMPLATE_CHART_NOT_EXIST = "不存在下标${index}的图表";
    public static final String ERR_MSG_PIC_NOT_ADD = "下标${index}的图片替换失败";
    public static final String ERR_MSG_PIC_NOT_EXIST = "模板中${type}下标${index}的图片不存在替换值";

    //文档对象
    private XWPFDocument doc;

    public WordContentParser(InputStream in) {
        try {
            this.doc = new XWPFDocument(in);
        } catch (IOException e) {
            log.error("{}：{}", ERR_MSG_DOC_LOAD_FAIL, e.getMessage());
            throw new RuntimeException(ERR_MSG_DOC_LOAD_FAIL);
        }
    }

    public WordContentParser(XWPFDocument doc) {
        this.doc = doc;
    }

    public WordContentParser(String path) {
        this.doc = loadXWPFDocument(path);
    }

    public static XWPFDocument loadXWPFDocument(String path) {
        InputStream in = null;
        XWPFDocument doc = null;
        try {
            in = new FileInputStream(path);
            //in = new ClassPathResource(path).getInputStream(); spring-boot项目打成jar部署到Linux需要改成这个加载方式，path用相对路径
            doc = new XWPFDocument(in);
        } catch (Exception e) {
            log.error("{}：{}", ERR_MSG_DOC_LOAD_FAIL, e.getMessage());
            throw new RuntimeException(ERR_MSG_DOC_LOAD_FAIL);
        } finally {
            if (Objects.nonNull(in)) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return doc;
        }
    }

    public File outputFile(String path) {
        if (Objects.isNull(doc)) {
            throw new RuntimeException(ERR_MSG_DOC_LOAD_FAIL);
        }

        File file = null;
        FileOutputStream fos = null;
        try {
            file = new File(path);
            if (file.exists()) {
                file.delete();
            }
            fos = new FileOutputStream(path);
            doc.write(fos);
        } catch (Exception e) {
            log.error("{}：{}", ERR_MSG_DOC_OUTPUT_FAIL, e.getMessage());
            throw new RuntimeException(ERR_MSG_DOC_OUTPUT_FAIL);
        } finally {
            if (Objects.nonNull(fos)) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return file;
        }
    }

    public abstract XWPFDocument replace();
}
