package com.zd.laboratory.utils.word;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.DocxRenderData;
import com.deepoove.poi.data.Includes;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;

public class WordUtils {

    public static final String[] CN_NUM = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};

    /**
     * 生成模板一 1.隐患整改告知书模板
     * @throws IOException
     * @param word1
     * @param list
     * @param response
     */
    public static void getWord1(Word1 word1, List<Word1.Detail> list, HttpServletResponse response) throws IOException {
//        URL url = Thread.currentThread().getContextClassLoader().getResource("word/1.docx");

//        String url;
//        String os =  System.getProperty("os.name");
//        if (os.toLowerCase().startsWith("win")) {
//            url = WordUtils.class.getResource("/word/1.docx").getPath();
//        }else {
//            url = "/home/word_temp/1.docx";
//        }


        /*word1 = new Word1();
        word1.setCheck_day("12");
        word1.setCheck_dept_name("测绘学院实验中心");
        word1.setCheck_month("1");
        word1.setCheck_year("2022");
        word1.setDept_name("环境与测绘学院");

        list = new ArrayList<>();

        for (int i = 1; i < 11; i++) {
            Word1.Detail detail = word1.new Detail();
            detail.setIndex(i+"");
            detail.setSub_name("实验室" + i);
            detail.setCheck_desc("危险描述" + i);
            detail.setCheck_img("http://deepoove.com/images/icecream.png");
            detail.setCheck_option_name("危险选项" + i);
            list.add(detail);
        }*/


        Configure config = Configure.builder().bind("details", new LoopRowTableRenderPolicy()).build();

        ClassPathResource classpathResource = new ClassPathResource("word/1.docx");

//        XWPFTemplate template = XWPFTemplate.compile(url, config);
        XWPFTemplate template = XWPFTemplate.compile(classpathResource.getInputStream(), config);

        template.render(
                new HashMap<String, Object>(){{

                    Field[] fields = Word1.class.getDeclaredFields();
                    for (int i = 0; i < fields.length; i++){
                        try {
                            Field field = fields[i];
                            field.setAccessible(true);
                            String name = field.getName();
                            Object value = field.get(word1);

                            put(name, value);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    put("details",list);
                }});
//        template.writeAndClose(new FileOutputStream("E:\\out1.docx"));
        template.write(response.getOutputStream());
    }

    public static void getWord2(Word2 word2, List<Word2.Detail> list, HttpServletResponse response) throws IOException {
//        URL url = Thread.currentThread().getContextClassLoader().getResource("word/2.docx");

        /*String url, detailUrl;

        String os =  System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            url = WordUtils.class.getResource("/word/2.docx").getPath();
            detailUrl = WordUtils.class.getResource("/word/word2_detail.docx").getPath();
        }else {
            url = "/home/word_temp/2.docx";
            detailUrl = "/home/word_temp/word2_detail.docx";
        }*/

        /*word2 = new Word2();
        word2.setDept_name("环境与测绘学院");
        word2.setSub_name("实验室名称");
        word2.setCheck_year("2022");
        word2.setCheck_month("1");
        word2.setCheck_day("14");
        word2.setCode("001");
        word2.setCount(5+"");
        word2.setCheck_dept_name("环测实验中心");
        word2.setCheck_result("审核结果");
        word2.setNow_year("2022");
        word2.setNow_month("1");
        word2.setNow_day("14");
        word2.setCheck_room("103");
        word2.setCheck_date("2022-01-14 08:40");
        word2.setCheck_nick_name("张大山");

        list = new ArrayList<>();


//        WordPolicy wordPolicy = new WordPolicy();
        List<RowRenderData> rowRenderData = new ArrayList<>();
        for (int i = 1; i < 11; i++) {
            Word2.Detail detail = word2.new Detail();
            detail.setIndex(i+"");
            detail.setOption_code("0" + i);
            detail.setCheck_desc("问题描述" + i);
            detail.setCheck_img("http://deepoove.com/images/icecream.png");
            list.add(detail);

           *//* rowRenderData.addAll(Arrays.asList(
                    Rows.of(
                            detail.getIndex(),
                            detail.getOption_code(),
                            detail.getCheck_desc(),
                            detail.getCheck_img())
                            .center().create()));*//*
        }*/
//        wordPolicy.setLabors(rowRenderData);

//        Configure config = Configure.builder().bind("%", new PictureRenderPolicy()).build();

        Configure config = Configure.createDefault().plugin('%', new SignPicPolicy());

        ClassPathResource classpathResource = new ClassPathResource("word/2.docx");

        XWPFTemplate template = XWPFTemplate.compile(classpathResource.getInputStream(), config);
        template.render(
                new HashMap<String, Object>(){{

                    Field[] fields = Word2.class.getDeclaredFields();
                    for (int i = 0; i < fields.length; i++){
                        try {
                            Field field = fields[i];
                            field.setAccessible(true);
                            String name = field.getName();
                            Object value = field.get(word2);

                            put(name, value);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
//                    put("details",wordPolicy);
                    ClassPathResource classpathResource = new ClassPathResource("word/word2_detail.docx");
//                    put("details", Includes.ofLocal(classpathResource.getInputStream()).setRenderModel(list).create());
                    put("details", Includes.ofStream(classpathResource.getInputStream()).setRenderModel(list).create());
                }});
//        template.writeAndClose(new FileOutputStream("E:\\out2.docx"));
        template.write(response.getOutputStream());
    }


    public static void getWord3(Word3 word3, List<Word3.Detail> list, List<Word3.Descs> descs, HttpServletResponse response) throws IOException {
//        URL url = Thread.currentThread().getContextClassLoader().getResource("word/3.docx");
        /*String url, detailUrl;

        String os =  System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            url = WordUtils.class.getResource("/word/3.docx").getPath();
            detailUrl = WordUtils.class.getResource("/word/word3_detail.docx").getPath();
        }else {
            url = "/home/word_temp/3.docx";
            detailUrl = "/home/word_temp/word3_detail.docx";
        }*/
        /*word3 = new Word3();
        word3.setCode("001");
        word3.setDept_name("环境与测绘学院");
        word3.setCheck_nick_name("张大山");
        word3.setSub_name("实验室名称");
        word3.setCheck_room("103");
        word3.setNow_year("2022");
        word3.setNow_month("1");
        word3.setNow_day("14");
        word3.setCheck_year("2022");
        word3.setCheck_month("1");
        word3.setCheck_day("14");
        word3.setCount(5+"");
        word3.setFinish_count(3+"");
        word3.setUn_count(2+"");


        list = new ArrayList<>();

        for (int i = 1; i < 11; i++) {
            Word3.Detail detail = word3.new Detail();
            detail.setIndex(i+"");
            detail.setOption_code("0" + i);
            detail.setCheck_desc("问题描述" + i);
            //detail.setCheck_img("http://deepoove.com/images/icecream.png");
            list.add(detail);
        }

        descs = new ArrayList<>();
        for (int i = 1; i < 8; i++) {
            Word3.Descs desc = word3.new Descs();
            desc.setIndex(CN_NUM[i-1]);
            desc.setCheck_option_name("检查项" + i);
            desc.setCheck_desc("隐患描述" + i);
            desc.setCheck_cause("原因分析" + i);
            desc.setCheck_result("检查结果" + i);
            desc.setCheck_img("http://deepoove.com/images/icecream.png");
            descs.add(desc);
        }*/

        Configure config = Configure.builder().bind("details", new LoopRowTableRenderPolicy())
                .addPlugin('%', new SignPicPolicy()).build();

        ClassPathResource classpathResource = new ClassPathResource("word/3.docx");

        XWPFTemplate template = XWPFTemplate.compile(classpathResource.getInputStream(), config);
        template.render(
                new HashMap<String, Object>(){{

                    Field[] fields = Word3.class.getDeclaredFields();
                    for (int i = 0; i < fields.length; i++){
                        try {
                            Field field = fields[i];
                            field.setAccessible(true);
                            String name = field.getName();
                            Object value = field.get(word3);

                            put(name, value);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    put("details",list);
                    ClassPathResource classpathResource = new ClassPathResource("word/word3_detail.docx");
                    put("descs", Includes.ofStream(classpathResource.getInputStream()).setRenderModel(descs).create());
                }});
//        template.writeAndClose(new FileOutputStream("E:\\out3.docx"));
        template.write(response.getOutputStream());
    }

    public static void main(String[] args) throws IOException {
//        WordUtils.getWord3();

//        System.out.println(WordUtils.class.getResource("/word/1.docx").getPath());
    }
}
