package com.njcn.gather.comservice.filepreview;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.http.MediaType;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class FilePreviewServiceTest {

    @Test
    public void previewPdfReturnsInlinePdfContent() throws Exception {
        Path file = Files.createTempFile("common-preview", ".pdf");
        byte[] content = "%PDF-1.4".getBytes(StandardCharsets.UTF_8);
        Files.write(file, content);
        FilePreviewService service = new FilePreviewService();

        FilePreviewService.PreviewContent previewContent = service.preview(file, "sample.pdf");

        Assert.assertEquals(MediaType.APPLICATION_PDF_VALUE, previewContent.getContentType());
        Assert.assertArrayEquals(content, previewContent.getContent());
    }

    @Test
    public void previewDocxReturnsHtmlContent() throws Exception {
        Path file = Files.createTempFile("common-preview", ".docx");
        try (XWPFDocument document = new XWPFDocument();
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            document.createParagraph().createRun().setText("第一行");
            document.createParagraph().createRun().setText("第二行<tag>");
            document.write(outputStream);
            Files.write(file, outputStream.toByteArray());
        }
        FilePreviewService service = new FilePreviewService();

        FilePreviewService.PreviewContent previewContent = service.preview(file, "sample.docx");
        String html = new String(previewContent.getContent(), StandardCharsets.UTF_8);

        Assert.assertEquals(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8", previewContent.getContentType());
        Assert.assertTrue(html.contains("第一行"));
        Assert.assertTrue(html.contains("第二行&lt;tag&gt;"));
    }

    //@Test
    //public void previewDocReturnsHtmlContent() throws Exception {
    //    Path file = Files.createTempFile("common-preview", ".doc");
    //    try (HWPFDocument document = new HWPFDocument();
    //         ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
    //        Range range = document.getRange();
    //        range.insertAfter("DOC 正文");
    //        document.write(outputStream);
    //        Files.write(file, outputStream.toByteArray());
    //    }
    //    FilePreviewService service = new FilePreviewService();
    //
    //    FilePreviewService.PreviewContent previewContent = service.preview(file, "sample.doc");
    //    String html = new String(previewContent.getContent(), StandardCharsets.UTF_8);
    //
    //    Assert.assertEquals(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8", previewContent.getContentType());
    //    Assert.assertTrue(html.contains("DOC 正文"));
    //}

    @Test(expected = IllegalArgumentException.class)
    public void previewRejectsUnsupportedExtension() throws Exception {
        Path file = Files.createTempFile("common-preview", ".txt");
        Files.write(file, "text".getBytes(StandardCharsets.UTF_8));
        FilePreviewService service = new FilePreviewService();

        service.preview(file, "sample.txt");
    }
}
