package com.xhkjedu.sapi.utils;

import com.xhkjedu.sapi.config.ConfigKey;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ywx
 * @className CopyURL
 * @description
 * @date 2020/8/5 9:52
 **/
@Slf4j
public class CopyURL {

    /**
     * 把试题中的网络图片复制到本地
     * @Param [qstem]
     * @Author ywx
     * @Date 2020/8/5 11:20
     * @return java.lang.String
     **/
    /*public static String copyImg(String qstem, String questionid) {
        List<String> imgs = getImgSrc(qstem);
        for (String img : imgs) {
            String imgPath = copy(img, questionid);
            qstem = qstem.replace(img, imgPath);
        }
        return qstem;
    };*/
    
    /**
     * 获取字符串中的img标签中的src地址
     * @Param [str]
     * @Author ywx
     * @Date 2020/8/5 10:11
     * @return java.util.List<java.lang.String>
     **/
    /*public static List<String> getImgSrc(String str) {
        if( str == null ){
            return null;
        }

        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> pics = new ArrayList<>();

        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(str);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            img = img.replaceAll("\"","").replaceAll("\\\\","");
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String imgUrl = m.group(1);
                if (!imgUrl.contains("data:image/")) {//排除base64图片
                    pics.add(imgUrl);
                }
            }
        }
        return pics;
    }*/

    //获取试题图片相对地址
    public static List<String> getImgSrc2(String str) {
        if( str == null ){
            return null;
        }

        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> pics = new ArrayList<>();

        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(str);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            img = img.replaceAll("\"","").replaceAll("\\\\","");
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                String imgUrl = m.group(1);
                if (!imgUrl.contains("data:image/") && imgUrl.contains(ConfigKey.oldshowurl)) {//排除base64图片
                    pics.add(imgUrl.replaceAll(ConfigKey.oldshowurl,""));
                }
            }
        }
        return pics;
    }
    
    //保存网络图片到本地
    /*public static String copy(String imgUrl, String questionid) {
        String imgPath = imgUrl.replaceAll(ConfigKey.oldshowurl,"");
        String filepath = ConfigKey.uploadfolder + imgPath;
        String showpath = ConfigKey.showurl + imgPath;
        try {
            createFile(filepath, imgUrl);
        } catch (IOException e) {
            log.error("保存网络图片失败【"+questionid+"】",e.getMessage());
        }
        return showpath.replaceAll("\\\\", "/");
    }

    //保存网络文件到本地
    public static void copyFile(String fileUrl, String id) {
        String filepath = ConfigKey.uploadfolder + fileUrl;
        final ExecutorService exec = Executors.newFixedThreadPool(1);
        try {
            Callable<String> call = new Callable<String>() {
                @Override
                public String call() throws IOException {
                    createFile(filepath, ConfigKey.oldshowurl + fileUrl);
                    return "执行完成!";
                }
            };
            Future<String> future = exec.submit(call);
            future.get(ConfigKey.timeout, TimeUnit.MINUTES); //任务处理超时时间
        } catch (Exception e) {
            log.error("保存网络文件失败【"+id+"】",e.getMessage());
        }
        // 关闭线程池
        exec.shutdown();
    }

    *//**
     * 复制网络文件到本地
     * @Param [filepath, urlpath]
     * @Author ywx
     * @Date 2021/5/7 13:42
     * @return void
     **//*
    private static void createFile(String filepath, String urlpath) throws IOException {
        File outFile = new File(filepath);
        if (outFile.exists()) return;//存在则不复制
        File directory = new File(outFile.getParent());
        directory.setWritable(true, false);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        OutputStream os = new FileOutputStream(outFile);
        URL url = new URL(urlpath);
        InputStream is = url.openStream();
        byte[] buff = new byte[1024];
        while (true) {
            int readed = is.read(buff);
            if (readed == -1) {
                break;
            }
            byte[] temp = new byte[readed];
            System.arraycopy(buff, 0, temp, 0, readed);
            os.write(temp);
        }
        is.close();
        os.close();
    }*/

}
