package com.zeroq6.moehelper.test;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * Created by yuuki asuna on 2017/6/21.
 */
public class PoolSyncTest {


    /**
     * 同步pool
     * 删除的手动处理
     * @throws Exception
     */
    @Test
    public void sync() throws Exception{
        File fromDir = new File("D:\\1");
        File toDir = new File("D:\\Pool_Packages");
        ///////////
        String[] suffix = new String[]{"zip"};
        final String toDirName = "Pool_Packages";
        if (!fromDir.isDirectory()) {
            throw new RuntimeException("来源非法");
        }
        if (!toDir.getName().equals(toDirName) || !toDir.isDirectory()) {
            throw new RuntimeException("目标非法");
        }
        if(FileUtils.sizeOfDirectory(fromDir) >= toDir.getFreeSpace() + FileUtils.ONE_GB){
            throw new RuntimeException("目标空间不足");
        }
        Map<String, List<File>> fromMap = transferIdFileMap(FileUtils.listFiles(fromDir, suffix, true));
        Map<String, List<File>> toMap = transferIdFileMap(FileUtils.listFiles(toDir, suffix, true));
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        // 更新和新增
        for (Map.Entry<String, List<File>> item : fromMap.entrySet()) {
            List<File> to = toMap.get(item.getKey());
            if (null == to) {
                for(File f : item.getValue()){
                    FileUtils.moveFileToDirectory(f, genMoveToDirById(Integer.valueOf(item.getKey()), toDir), true);
                    System.out.println("new新增: " + f.getAbsolutePath());
                }
            } else {
                for(File f : item.getValue()){
                    System.out.println("update新增: " + f.getAbsolutePath());
                    FileUtils.moveFileToDirectory(f, genMoveToDirById(Integer.valueOf(item.getKey()), toDir), true);
                }
                for(File f : to){
                    System.out.println("update移除: " + f.getAbsolutePath());
                    FileUtils.moveFileToDirectory(f, new File(toDir.getCanonicalPath() + File.pathSeparator + time + ".updated"), true);
                }
            }
        }


    }

    private File genMoveToDirById(int id, File toDir) throws Exception{
        if (id < 1) {
            throw new RuntimeException("id非法, " + id);
        }
        int index = id % 600 == 0 ? id / 600 : id / 600 + 1;
        int a = (index - 1) * 600 + 1;
        int b = index * 600;
        String name = String.format("Pool_Packages-%s(id=%s-%s)", StringUtils.leftPad(index+"",2,"0"), a + "", b + "");
        return new File(toDir.getCanonicalPath() + File.separator + name);

    }

    private Map<String, List<File>> transferIdFileMap(Collection<File> fileCollection) {
        Iterator<File> iterator = fileCollection.iterator();
        Pattern pattern = Pattern.compile("\\[[0-9]{4}\\].*[.]zip");
        Map<String, List<File>> result = new HashMap<String, List<File>>();
        while (iterator.hasNext()) {
            File file = iterator.next();
            String name = file.getName();
            if (!pattern.matcher(name).matches()) {
                throw new RuntimeException("非法文件名, " + name);
            }
            String id = Integer.valueOf(name.substring(1, 5)) + "";
            if(null == result.get(id)){
                result.put(id, new ArrayList<File>());
            }
            result.get(id).add(file);
        }
        return result;
    }
}

@@ -483,13 +483,28 @@
 le(%22
-D:%5C%5C1
+F:%5C%5C%E5%9B%BE%5C%5Cyande.re.pool
 %22);%0A
@@ -538,10 +538,20 @@
 le(%22
-D:
+F:%5C%5Cyande.re
 %5C%5CPo
@@ -1822,39 +1822,26 @@
 or(File f : 
-item.getValue()
+to
 )%7B%0A         
@@ -1877,18 +1877,18 @@
 (%22update
-%E6%96%B0%E5%A2%9E
+%E7%A7%BB%E9%99%A4
 : %22 + f.
@@ -1964,62 +1964,78 @@
 (f, 
-genMoveToDirById(Integer.valueOf(item.getKey()), toDir
+new File(toDir.getCanonicalPath() + File.separator + time + %22.updated%22
 ), t
@@ -2079,34 +2079,47 @@
    for(File f : 
-to
+item.getValue()
 )%7B%0A             
@@ -2143,34 +2143,34 @@
 .println(%22update
-%E7%A7%BB%E9%99%A4
+%E6%96%B0%E5%A2%9E
 : %22 + f.getAbsol
@@ -2238,82 +2238,62 @@
 (f, 
-new File(toDir.getCanonicalPath() + File.pathSeparator + time + %22.updated%22
+genMoveToDirById(Integer.valueOf(item.getKey()), toDir
 ), t

同步Pool压缩包可能有相同文件名，故先移动，后加入