package xyz.wbsite.frame.utils;

import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Resource资源文件工具
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
public class ResourceUtil extends ResourceUtils {

    /**
     * 获取资源目录下所有文件名 如: /modules/dir/
     *
     * @param resourcePath 路径
     * @return
     */
    public static List<String> getResourceFiles(String resourcePath) {
        List<String> result = new ArrayList<>();
        File applicationHome = getApplicationHome();
        if (applicationHome.getAbsolutePath().endsWith(".jar")) {
            try {
                Pattern pattern = Pattern.compile(".*" + resourcePath + "(.+\\..+)");
                JarFile jarFile = new JarFile(applicationHome);
                Enumeration<JarEntry> entries = jarFile.entries();
                while (entries.hasMoreElements()) {
                    JarEntry jarEntry = entries.nextElement();
                    String name = jarEntry.getName();

                    if (name.matches(".*" + resourcePath + "(.+\\..+)")) {
                        Matcher matcher = pattern.matcher(name);
                        if (matcher.find()) result.add(matcher.group(1));
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                ClassPathResource cpr = new ClassPathResource(resourcePath);
                File in = cpr.getFile();
                for (File file : in.listFiles()) {
                    result.add(file.getName());
                }
                return result;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    /**
     * 获取当前运行jar文件
     *
     * @return jar路径
     */
    public static File getApplicationHome() {
        ApplicationHome home = new ApplicationHome(ResourceUtil.class);
        return home.getSource();
    }

    /**
     * 获取resource下文件
     *
     * @return 文件路径
     */
    public static InputStream getResourceInput(String resource) {
        try {
            ClassPathResource classPathResource = new ClassPathResource(resource);
            return classPathResource.getInputStream();
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * 复制jar下的资源文件到指定文件
     *
     * @return 成功或失败情况
     */
    public static boolean copyResource2File(String resource, File file) {
        InputStream resourceInput = getResourceInput(resource);
        FileOutputStream fileOutputStream = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            FileUtil.copy(resourceInput, fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (fileOutputStream != null) fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * 获取资源的的字节数组
     *
     * @return 字节数组
     */
    public static byte[] getResourceBytes(String resource) {
        InputStream is = null;
        byte[] result = null;
        try {
            is = getResourceInput(resource);
            result = new byte[is.available()];
            is.read(result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
