// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-serialization:1.5.0'
    }
}
plugins {
    id 'com.android.application' version '8.2.2' apply false
    id 'com.android.library' version '8.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.9.23' apply false
    id 'org.jetbrains.kotlin.jvm' version '1.9.23' apply false
    id "com.vanniktech.maven.publish" version "0.33.0" apply false
    id 'com.gradle.plugin-publish' version '1.2.1' apply false
}
ext {
    sdkVersion = 31
    minSdkVersion = 21
}

def synchronized getVersionProperty(propName, defValue) {
    def file = file("version.properties")
    def ret = defValue
    if (file.exists() && file.canRead()) {
        FileInputStream input = new FileInputStream(file)
        Properties props = new Properties()
        props.load(input)
        ret = props.get(propName);
        input.close()
    }
    return ret
}


def getAppVersionName() {
    String versionName = getVersionProperty("PROJ_VERSION", "1.0.0")
    return versionName
}


def getAppVersionCode() {
    String versionName = getAppVersionName()
    def versions = versionName.split("\\.")
    def updateVersionString = ""
    for (int i = 0; i < versions.size(); i++) {
        def subString = versions[i]
        if (i == 0) {
            updateVersionString += subString
            continue
        } else if (i >= 3) {
            break
        }
        def subNumber = Integer.parseInt(subString)
        updateVersionString += String.format("%01d", subNumber)
    }
    return Integer.parseInt(updateVersionString)
}

task aaBumpVersion() {
    doLast {
        def versionName = getAppVersionCode() + 1
        def str = versionName.toString()
        def length = str.length()
        def newVersionName = ""
        for (int i = 0; i < length; i++) {
            newVersionName += str.charAt(i)
            if (i < 2) {
                newVersionName += "."
            }
        }

        def versionPropsFile = file('version.properties')
        def versionProps = new Properties()
        versionProps.load(new FileInputStream(versionPropsFile))
        def oldVersionName = versionProps['PROJ_VERSION']
        versionProps['PROJ_VERSION'] = newVersionName
        versionProps.store(versionPropsFile.newWriter(), null)

        updateREADME("README.md",oldVersionName,newVersionName)
        updateREADME("README-zh.md",oldVersionName,newVersionName)

        File gradleFile = new File("gradle.properties")
        String gradleText = gradleFile.text
        String text2 = gradleText.replace("TestVersion = "+oldVersionName,"TestVersion = "+newVersionName)


        gradleFile.write(text2)
        version = newVersionName
        rootProject.subprojects { subproject ->
            subproject.version = newVersionName
        }
        println("升级版本号完成，versionName = "+newVersionName)
    }
}

subprojects { subproject ->
    subproject.afterEvaluate {
        def list = new ArrayList<String>()
        subproject.tasks.forEach { task ->
            if ("publishAllPublicationsToNexusRepository" == task.name){
                list.add("${subproject.path}:publishMavenJavaPublicationToMavenLocal")
            }
        }
        aaBumpVersion.finalizedBy(list.toArray())
    }
}
def updateREADME(readme,oldVersionName,newVersionName) {
    File configFile = new File(readme)
    String exportText = configFile.text
    String text = exportText.replace("io.github.flyjingfish:fasttransform:"+oldVersionName,"io.github.flyjingfish:fasttransform:"+newVersionName)
    text = text.replace("id \"io.github.flyjingfish.fasttransform\" version \""+oldVersionName+"\"","id \"io.github.flyjingfish.fasttransform\" version \""+newVersionName+"\"")
    configFile.write(text)
}

def appVersionName = getAppVersionName()
group = PROJ_GROUP
version = appVersionName