import groovy.transform.Field

@Field LINE_SEP = System.getProperty("line.separator")
@Field LONG_SPACE = "                                        "

task readmeTask << {
    def readmeCN = file("./utilcode/README-CN.md")
    def readmeEng = file("./utilcode/README.md")
    readmeOfUtilCode2Eng(readmeCN, readmeEng)

    readmeCN = file("./subutil/README-CN.md")
    readmeEng = file("./subutil/README.md")
    readmeOfSubUtil2Eng(readmeCN, readmeEng)
}

def readmeOfUtilCode2Eng(File readmeCN, File readmeEng) {
    format(readmeCN)
    def lines = readmeCN.readLines("UTF-8")
    def sb = new StringBuilder()
    readmeCN.eachLine { line ->
        if (line.contains("* ###")) {
            String utilsName = line.substring(line.indexOf("[") + 1, line.indexOf("Utils"))
            sb.append("* ### About ").append(utilsName).append(line.substring(line.indexOf(" -> ")))
        } else if (line.contains(": ") && !line.contains("[")) {
            sb.append(line.substring(0, line.indexOf(':')).trim())
        } else {
            sb.append(line)
        }
        sb.append(LINE_SEP)
    }
    readmeEng.write(sb.toString(), "UTF-8")
}

def readmeOfSubUtil2Eng(File readmeCN, File readmeEng) {
    format(readmeCN)
    def lines = readmeCN.readLines("UTF-8"),
        sb = new StringBuilder("## How to use" + LINE_SEP
                + LINE_SEP +
                "You should copy the following classes which you want to use in your project." + LINE_SEP),
        i = 3,
        size = lines.size()
    for (; i < size; ++i) {
        String line = lines.get(i)
        if (line.contains("* ###")) {
            String utilsName = line.substring(line.indexOf("[") + 1, line.indexOf("Utils"))
            sb.append("* ### About ").append(utilsName).append(line.substring(line.indexOf(" -> ")))
        } else if (line.contains(": ") && !line.contains("[")) {
            sb.append(line.substring(0, line.indexOf(':')).trim())
        } else {
            sb.append(line)
        }
        sb.append(LINE_SEP)
    }
    readmeEng.write(sb.toString(), "UTF-8")
}

def format(File readmeCN) {
    def sb = new StringBuilder(),
        lines = readmeCN.readLines("UTF-8"),
        i = 0,
        size = lines.size()
    for (; i < size; ++i) {
        String line = lines.get(i)
        if (line.contains("* ###")) {
            sb.append(line).append(LINE_SEP)
                    .append("```").append(LINE_SEP)
            def maxLen = 0
            line = lines.get(i += 2)
            // get the max length of space
            for (def j = i; !line.equals("```"); line = lines.get(++j)) {
                maxLen = Math.max(maxLen, line.replace(" ", "").replace(",", ", ").indexOf(':'))
            }
            line = lines.get(i)
            for (; !line.equals("```"); line = lines.get(++i)) {
                def noSpaceLine = line.replace(" ", "")
                def spaceLen = maxLen - line.replace(" ", "").replace(",", ", ").indexOf(':')
                sb.append(noSpaceLine.substring(0, noSpaceLine.indexOf(':')).replace(",", ", "))
                        .append(LONG_SPACE.substring(0, spaceLen))// add the space
                        .append(': ')
                        .append(line.substring(line.indexOf(':') + 1).trim())
                        .append(LINE_SEP)
            }
            sb.append("```")
        } else {
            sb.append(line)
        }
        sb.append(LINE_SEP)
    }
    readmeCN.write(sb.toString(), "UTF-8")
}