package com.goafanti.common.utils.word;

import com.goafanti.RD.bo.OutWordRdDetails;
import com.goafanti.common.utils.excel.FileUtils;


import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.rtf.RtfWriter2;
import org.springframework.http.MediaType;



import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.*;
import java.util.Date;

public class WordUtils {




    public void pushWord(OutWordRdDetails data, HttpServletResponse response,  String uploadPath) {
        String tableName = data.getRdName();
        String realFileName = uploadPath+"/tmp/"+new Date().getTime() + ".doc";
        String attName = data.getRdName()+new Date().getTime() + ".doc";
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        Document document=new Document(PageSize.A4);
        try {
            FileUtils.setAttachmentResponseHeader(response, tableName,attName);
            FileOutputStream outputStream = new FileOutputStream(realFileName);
            RtfWriter2.getInstance(document,outputStream);
            document.open();
            Paragraph paragraph=new Paragraph(data.getRdName(),getTitleFont());
            paragraph.setAlignment(1);
            document.add(paragraph);
            addDocument(document,"项目起止时间： ",data.getStartEndTime());
            addDocument(document,"公司名称： ",data.getUserName());
            addDocument(document,"项目负责人： ",data.getConsultantName());
            addDocument(document,"技术领域： ",data.getTechnicalField());
            addDocument(document,"技术来源： ",data.getTechnologySource());
            addDocument(document,"研发费用总预计： ",data.getTotalAmount().toEngineeringString()+" 万元");
            addDocument(document,"研发目的/立项目的/实施方式",null);
            addContent(document,"        "+data.getRdObjective());
            addDocument(document,"核心技术/创新点",null);
            addContent(document,"        "+data.getCoreTechnology());
            addDocument(document,"成果",null);
            addContent(document,"        "+data.getAchieveResults());
            document.close();
            outputStream.close();
            FileUtils.writeBytes(realFileName, response.getOutputStream());
            FileUtils.deleteFile(realFileName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addContent(Document document, String value) throws DocumentException {
        Phrase phrase = new Phrase(value, getFont());
        phrase.setLeading(25);
        if(value!=null)document.add(phrase);
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
    }

    private void addDocument(Document document, String key, String value) throws DocumentException {
        if(key!=null) {
            document.add(new Chunk(key, getBigFont()));
        }
        Phrase phrase = new Phrase(value, getFont());
        phrase.setLeading(40);
        if(value!=null)document.add(phrase);
        document.add(Chunk.NEWLINE);
    }

    public Font getTitleFont() {
        return setFont(24,Font.BOLD);
    }

    public  Font getBigFont(){
        return setFont(16, Font.BOLD);
    }

    public  Font getFont(){
        Font font =setFont(12, Font.NORMAL);
        return font;
    }

    private  Font setFont(int size, int style) {
        Font font=new Font(Font.NORMAL, size, style, new Color(0, 0, 0));
        return font;
    }


    public void test(){
        // 创建word文档,并设置纸张的大小
        Document document = new Document(PageSize.A4);
        // 创建word文档
        try {
        RtfWriter2.getInstance(document, new FileOutputStream("D:/data/dbDetail.doc"));
        document.open();// 设置文档标题
//        Paragraph ph = new Paragraph();
//        Font f = new Font();
//        Paragraph p = new Paragraph("数据库表设计文档", new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)));
//        p.setAlignment(1);
//        document.add(p);
            Paragraph p=new Paragraph("data.getRdName()",new Font(Font.NORMAL, 24, Font.BOLDITALIC, new Color(0, 0, 0)));
            p.setAlignment(1);
            document.add(p);
            addDocument(document,"项目起止时间： ","data.getRdName()");
            addDocument(document,"公司名称： ","data.getUserName()");
            addDocument(document,"项目负责人： ","data.getConsultantName()");
            addDocument(document,"技术领域： ","data.getTechnicalField()");
            addDocument(document,"技术来源： ","data.getTechnologySource()");
            addDocument(document,"研发费用总预计： ","data.getTotalAmount().toEngineeringString()"+" 万元");
            addDocument(document,"研发目的/立项目的/实施方式",null);
            addContent(document,"        data.getRdObjective()");
            addDocument(document,"核心技术/创新点",null);
            addContent(document,"        data.getCoreTechnology()");
            addDocument(document,"成果",null);
            addContent(document,"        data.getAchieveResults()");
        document.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}
