package com.emr.controller;

import com.emr.entity.Archive_Detail;
import com.emr.entity.Archive_Detail_Vo;
import com.emr.entity.OffsetLimitPage;
import com.emr.service.Archive_DetailService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfReader;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
public class PdfFileIsErrorController {
    @Value("${PdfIsError}")
    private String PdfIsError;

    @Autowired
    private Archive_DetailService archive_detailService;

    @RequestMapping("toPdfFileIsError")
    public String toPdfFileIsError(){
        return "PdfFileIsError";
    }

    @RequestMapping("selectPdfFileMayError")
    @ResponseBody
    public OffsetLimitPage PdfFileMayError(HttpServletRequest request, HttpServletResponse response, Archive_Detail_Vo archive_detail_vo, Integer offset, Integer limit){
        return archive_detailService.selectPdfPathMayError(archive_detail_vo,offset,limit);
    }

    @RequestMapping("PdfFileIsError")
    @ResponseBody
    public List<String> PdfFileIsError(HttpServletRequest request, HttpServletResponse response, Archive_Detail_Vo archive_detail_vo){
        List<Archive_Detail> list = archive_detailService.selectPdfPathMayError(archive_detail_vo);

        List<String> errorlist = new ArrayList<>();

        if(list.size()>0) {
            try {
                errorlist = CheckPdfFiles(list);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return errorlist;
    }

    /**
     * 递归得到特定目录下的所有文件
     */
    /*public static void getAllPdfFile(String basedir) {
        File dir = new File(basedir);
        File[] files = dir.listFiles(new PdfFileFilter());
        if (files == null)
            return;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                getAllPdfFile(files[i].getAbsolutePath());
            } else {
                pdffiles.add(files[i].getAbsolutePath());
            }

        }

    }*/
    /**
     * 循环检测
     */
    public static List<String> CheckPdfFiles(List<Archive_Detail> list) throws Exception {
        List<String> errordetailid = new ArrayList<>();
        for (Archive_Detail archiveDetail : list) {
            boolean flag = false;
            //循环调用check 检查是否损坏
            flag = check(archiveDetail.getPdfPath());
            if(!flag){
                //若破损 则存入list 返回
                errordetailid.add(archiveDetail.getId());
            }
        }
        return errordetailid;
    }

    /**
     * 利用itext打开pdf文档
     */
    public static boolean check(String file) {
        boolean flag1 = false;
        int n = 0;
//        Document document = null;
        PdfReader reader = null;
        try {
            if (StringUtils.isNotBlank(file)){
                reader = new PdfReader(file);
//                document = new Document(reader.getPageSize(1));
//                document.open();
                n = reader.getNumberOfPages();
                if (n != 0)
                    flag1 = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            /*if (document!=null){
                document.close();
            }*/
            if (reader!=null){
                reader.close();
            }
        }
        return flag1;

    }
}
