package com.izouma.wenlvju.service;


import com.github.kevinsawicki.http.HttpRequest;
import com.izouma.wenlvju.ApplicationTests;
import com.izouma.wenlvju.domain.Rate;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.RateRepo;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.time.LocalDateTime;
import java.util.List;

public class RateServiceTest extends ApplicationTests {
    @Autowired
    private RateService rateService;
    @Autowired
    private RateRepo    rateRepo;

    @Test
    public void test() {
        Rate rate = rateRepo.findById(467L).orElseThrow(new BusinessException("无记录"));
        rateService.upLoad(rate);
    }

    @Test
    public void test2() throws UnsupportedEncodingException {

        Rate rate = rateRepo.findById(625L).orElseThrow(new BusinessException("无记录"));
        List<InputStream> files = rateService.upLoad1(rate);

        String targetPath = "/Users/qiufangchao/Desktop/result.pdf";
        // pdf合并工具类
        PDFMergerUtility mergePdf = new PDFMergerUtility();
//        for (File f : files) {
//            if (f.exists() && f.isFile()) {
        // 循环添加要合并的pdf
//                mergePdf.addSource(f);
//            }
//        }
        mergePdf.addSources(files);
        // 设置合并生成pdf文件名称
        mergePdf.setDestinationFileName(targetPath);
        // 合并pdf
        try {
            mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
            for (InputStream is : files) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        System.out.println(writer.toString());
//        new File(targetPath);

    }

    @Test
    public void test3() throws IOException {
        String targetPath = "/Users/qiufangchao/Desktop/result.pdf";

        String uri = "http://convert.izouma.com/word2pdf";
        // 表格
        Rate rate = rateRepo.findById(625L).orElseThrow(new BusinessException("无申请"));
        String export = rateService.export(rate);
        InputStream is = new ByteArrayInputStream(export.getBytes());
        InputStream stream = HttpRequest.post(uri)
                .accept("*/*")
                .part("file", "审核材料.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", is)
                .stream();

        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(targetPath);
        while ((index = stream.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        downloadFile.close();
        stream.close();

    }

    @Test
    public void test5() {
        Rate rate = rateRepo.findById(644L).orElse(null);
        System.out.println(rateService.reviewTimeMessage(rate));
    }

    @Test
    public void test6() {
        Rate rate = rateRepo.findById(644L).orElse(null);
        System.out.println(rateService.gradeMessage(LocalDateTime.now(), rate.getExpertUserId(), rate
                .getName()));
    }

    @Test
    public void test7() throws IOException {
        Rate rate = rateRepo.findById(625L).orElse(null);
        String export = rateService.export(rate);
        InputStream is = new ByteArrayInputStream(export.getBytes());

        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream("/Users/qiufangchao/Desktop/123.doc");
        while ((index = is.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        downloadFile.close();
        is.close();
    }

    @Test
    public void test8() {

//        String img = "https://ticket-exchange.oss-cn-hangzhou.aliyuncs.com/image/2021-04-29-12-39-59rzhrQYhu.jpg";

        Rate rate = rateRepo.findById(625L).orElseThrow(new BusinessException("wu"));
        List<String> imageUrllist = rate.getBusiness();

        // PDF文件保存地址
        String mOutputPdfFileName = "/Users/qiufangchao/Desktop/test1.pdf";

        Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
        try {
            PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
            doc.open();
            for (String img : imageUrllist) {
                doc.newPage();
                Image png1 = Image.getInstance(img);
                float heigth = png1.getHeight();
                float width = png1.getWidth();
                int percent = rateService.getPercent(width);

                png1.setAlignment(Image.MIDDLE);
                png1.scalePercent(percent + 3);// 表示是原来图像的比例;
                doc.add(png1);
            }

        } catch (IOException | DocumentException e) {
            e.printStackTrace();
        } finally {
            doc.close();

        }
        File mOutputPdfFile = new File(mOutputPdfFileName);
        if (!mOutputPdfFile.exists()) {
            mOutputPdfFile.deleteOnExit();
        }
    }

    @Test
    public void test9() {
        Rate rate = rateRepo.findById(366L).orElseThrow(new BusinessException("无消息"));
        System.out.println(rateService.reviewTimesMessage(rate));
    }


    @Test
    public void test10() {
        System.out.println(rateRepo.countByExpertId(700l));
    }
}