package com.zeroq6.moehelper.test;

import com.zeroq6.moehelper.test.help.ArrangeHelper;
import com.zeroq6.moehelper.utils.MyLogUtils;
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
     * oldDir中被替换回来的，会放置在oldDir同级目录下的，oldDir_yyyyMMddHHmmss_to_delete，确认无误，手动删除即可
     *
     * @throws Exception
     */
    @Test
    public void sync() throws Exception {
        // 最近更新的pool
        File newDir = new File("H:\\yande.re\\yande.re_-_Pool_20190521");
        // 以前更新的pool，如果是最终移动到Pool_Packages，需要设定为Pool_Packages目录
        File oldDir = new File("H:\\yande.re\\Pool_Packages");

        // 最终移动到Pool_Packages时设置为true
        boolean finalToPoolPackages = true;
        ///////////
        String[] suffix = new String[]{"zip"};
        if (!oldDir.isDirectory() || !newDir.isDirectory()) {
            throw new RuntimeException("新旧文件都必须为目录");
        }
        // 校验文件名
        String reg = "yande[.]re_[-]_Pool_\\d{8}";
        if (!Pattern.matches(reg, newDir.getName())) {
            throw new RuntimeException("新目录名必须满足正则" + reg);
        }
        if (finalToPoolPackages) {
            String poolPackages = "Pool_Packages";
            if (!oldDir.getName().equals(poolPackages)) {
                throw new RuntimeException("旧目录名必须为" + poolPackages);
            }
        } else {
            if (!Pattern.matches(reg, oldDir.getName())) {
                throw new RuntimeException("旧目录名必须满足正则" + reg);
            }
            if (newDir.getName().compareTo(oldDir.getName()) <= 0) {
                throw new RuntimeException("新目录比较必须大于旧目录");
            }
        }
        // 校验空间
        if (FileUtils.sizeOfDirectory(newDir) >= oldDir.getFreeSpace() - (FileUtils.ONE_GB * 10)) {
            throw new RuntimeException("目标空间不足");
        }
        Map<String, List<File>> newMap = transferIdFileMap(FileUtils.listFiles(newDir, suffix, true));
        Map<String, List<File>> oldMap = transferIdFileMap(FileUtils.listFiles(oldDir, suffix, true));
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        // 更新和新增
        for (Map.Entry<String, List<File>> item : newMap.entrySet()) {
            List<File> old = oldMap.get(item.getKey());
            if (null == old) {
                for (File f : item.getValue()) {
                    FileUtils.moveFileToDirectory(f, finalToPoolPackages ? genMoveToDirById(Integer.valueOf(item.getKey()), oldDir) : oldDir, true);
                    MyLogUtils.stdOut("new新增: " + f.getAbsolutePath());
                }
            } else {
                // 先移除, 后新增, 避免文件名同步无法新增或错误覆盖
                for (File f : old) {
                    MyLogUtils.stdOut("update移除: " + f.getAbsolutePath());
                    FileUtils.moveFileToDirectory(f, new File(oldDir.getParentFile().getCanonicalPath() + File.separator + oldDir.getName() + "_" + time + "_to_delete"), true);
                }
                for (File f : item.getValue()) {
                    MyLogUtils.stdOut("update新增: " + f.getAbsolutePath());
                    FileUtils.moveFileToDirectory(f, finalToPoolPackages ? genMoveToDirById(Integer.valueOf(item.getKey()), oldDir) : oldDir, true);
                }
            }
        }


    }


    @Test
    public void arrangeDirTest() throws Exception{
        File parentDir = new File("I:\\yande.re\\Pool_Packages");
        for(File item : FileUtils.listFiles(parentDir, null, true)){
            String id = ArrangeHelper.getPoolId(item.getName());
            File toDir = genMoveToDirById(Integer.valueOf(id), parentDir);
            FileUtils.moveFileToDirectory(item, toDir, true);
        }

    }

    private File genMoveToDirById(int id, File toDir) throws Exception {
        if (id < 1) {
            throw new RuntimeException("id非法, " + id);
        }
        int numSliptDir = 500;
        int index = id % numSliptDir == 0 ? id / numSliptDir : id / numSliptDir + 1;
        int a = (index - 1) * numSliptDir + 1;
        int b = index * numSliptDir;
        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();
        Map<String, List<File>> result = new HashMap<String, List<File>>();
        while (iterator.hasNext()) {
            File file = iterator.next();
            String name = file.getName();
            String id = ArrangeHelper.getPoolId(name);
            if (null == result.get(id)) {
                result.put(id, new ArrayList<File>());
            }
            result.get(id).add(file);
        }
        return result;
    }
}

@@ -3530,16 +3530,17 @@
 xception
+ 
 %7B%0A      
@@ -3610,16 +3610,17 @@
      for
+ 
 (File it
@@ -3667,16 +3667,17 @@
 , true))
+ 
 %7B%0A      
@@ -4348,17 +4348,17 @@
 x + %22%22, 
-2
+3
 , %220%22), 

测试pool包文件夹序列号使用3位