package com.jtyjy.pdf2pptx.support;

import com.jtyjy.pdf2pptx.converter.Pdf2Pptx;
import com.jtyjy.pdf2pptx.converter.Png2Pptx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PdfTask implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(PdfTask.class);

    private List<String> allDirPathName;

    private String path;

    private RestTemplate restTemplate;

    @Override
    public void run() {
        for (String dirPathName : allDirPathName) {
            File dir = new File(dirPathName);
            //判断ppt是否存在
            if (dir.getName().endsWith("_图片")) {
                List<String> allPptPathFileName = new ArrayList<>();
                FileUtil.getAllFilePathNameRe(path, allPptPathFileName);
                if (!allPptPathFileName.contains(dir.getAbsolutePath().replace("_图片", ".pptx"))) {
                    List<String> filePathNames = FileUtil.getAllFilePathName(dir.getAbsolutePath());
                    //转换
                    logger.info("开始转换图片：{} 为pptx", dir.getName());
                    long begin = System.currentTimeMillis();
                    Png2Pptx.createPicturePptx(filePathNames, dir);
                    long end = System.currentTimeMillis();
                    logger.info("转换图片：{} 为pptx完成,耗时：{}ms", dir.getName(), end - begin);
                } else {
                    logger.info("文件：{} 已经存在", dir.getName() + ".pptx");
                }
                Pdf2Pptx.finishPdf++;
                //发送消息
                try {
                    Double message = (Double.valueOf(Pdf2Pptx.finishPdf) / (Double.valueOf(Pdf2Pptx.totalPdf) * Double.valueOf(2)))*Double.valueOf(100);
                    restTemplate.getForObject( "http://localhost:8080/checkcenter/socket/push/1994?message="+message, Object.class);
                } catch (Exception e) {
                    logger.error("推送消息出错：", e);
                }
            }

        }

    }

    public PdfTask(){

    }


    public PdfTask(List<String> allDirPathName, String path,RestTemplate restTemplate) {
        this.allDirPathName = allDirPathName;
        this.path = path;
        this.restTemplate = restTemplate;
    }
}
