package com.goafanti.common.utils.pdf;

import com.goafanti.RD.bo.OutWordRdDetails;
import com.goafanti.common.utils.excel.FileUtils;
import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;

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

public class PDFUtils {


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

    }

    private  void addPDFDocument(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);
    }

    private void addPDFContent(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);
    }


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

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

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


    /**
     *
     * @param size  大小
     * @return
     */
    public static Font setFont(Integer size,Integer style){
            BaseFont baseFont = null;
            Font font=null;
        try {
            baseFont=BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            font=new Font(baseFont,size,style);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return font;
    }
}
