package com.eversec.kit.dev;

import com.eversec.common.FileKit;
import com.jfinal.kit.Kv;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static java.util.Arrays.asList;

public class ProjectBuiler {
    public static String clazzRoot = FileKit.rootPath(ProjectBuiler.class);

    private static Kv kv = Kv.create();//准备用来渲染的基础数据

    static {
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream(new File(clazzRoot + "conf/builer.txt")));
        } catch (IOException e) {
            e.printStackTrace();
        }

        prop.forEach((k,v) -> {
            kv.put(k, v);
        });
    }

    private static List<String> tpl  = asList(".png", ".jpg", ".jpeg",
            ".npmignore",".js", ".ico", "package.json",".md");

    public static boolean endWith(String str) {
        return tpl.stream().filter(x ->  str.endsWith(x)).count() > 0;
    }

    @Test
    public void createAbc() throws IOException {
        ZipFile zipFile = new ZipFile(new File(clazzRoot + "doc/abc-front.zip"));
        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        long start = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(4979);

        List<ZipEntry> files = new ArrayList<>();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            files.add(zipEntry);
        }

        int n = 30;
        for (int i = 0; i < files.size() / n + 1; i++) {
            new Thread() {
                int i;
                CountDownLatch latch;
                public synchronized void start(int i, CountDownLatch latch) {
                    this.i = i;
                    this.latch = latch;
                    super.start();
                }

                @Override
                public void run() {
                    files.subList(n * i, Math.min(n * (i + 1), files.size())).forEach(zipEntry ->  {
                        try {
                            builerFile(zipFile, zipEntry, latch);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
                }
            }.start(i, latch);
        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        /*//依次遍历
        while (entries.hasMoreElements()){
            ZipEntry zipEntry = entries.nextElement();
            builer(zipFile, zipEntry, latch);
        }*/

        System.out.println("文件总数:" + files.size() + "" + " 耗时："
                + (System.currentTimeMillis() - start) + "ms");
    }

    /*11新语法，修改了，慎用。。*/
    public void builerFile(ZipFile zipFile, ZipEntry zipEntry, CountDownLatch latch) throws IOException {

        do {
            InputStream is = zipFile.getInputStream(zipEntry);

            File file = new File("target/pname/" + zipEntry.getName());
            System.out.println("latchCount:" + latch.getCount()/* +"--------" +zipEntry.getName()+ "----------"*/);
            if (zipEntry.isDirectory()) {
                file.mkdirs();
                latch.countDown();
                continue;
            }
            if (true || endWith(file.getName())) {
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(is.read(new byte[1024]));
                fos.close();
                latch.countDown();
                continue;
            }

            String str = FileKit.readAll(is);
            FileKit.tplRender(str, file, kv);
        } while (false);
    }

}
