// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    dependencies {
        classpath 'io.github.flyjingfish:androidaop-plugin:'+TestVersion
        classpath 'org.jetbrains.kotlin:kotlin-serialization:1.5.0'
//        classpath 'io.github.flyjingfish:easyregister:1.1.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.google.devtools.ksp' version '1.9.23-1.0.20' apply false
    id 'io.github.flyjingfish.easyregister' version '1.1.0' apply false
    id 'com.gradle.plugin-publish' version '1.2.1' apply false
    alias(libs.plugins.android.dynamic.feature) apply false
//    id "kotlinx-serialization" version "1.5.0"
//    id "io.github.FlyJingFish.AndroidAop.android-aop" version "1.7.2" apply true
}
apply plugin: "android.aop"
//apply plugin: "android.aop.clean"
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)
        updateREADME("docs/getting_started.md",oldVersionName,newVersionName)
        updateREADME("docs/android_aop_extra.md",oldVersionName,newVersionName)
        updateREADME("docs/zh/getting_started.md",oldVersionName,newVersionName)
        updateREADME("docs/zh/android_aop_extra.md",oldVersionName,newVersionName)



//        version = newVersionName
//        rootProject.subprojects { subproject ->
//            subproject.version = newVersionName
//        }


        exec {
            workingDir projectDir
            commandLine './gradlew', 'publishMavenPublicationToMavenLocal'
        }

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


        gradleFile.write(text2)

        println("升级版本号完成，versionName = "+newVersionName)
    }
}


task aaBumpVersion1() {
    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)
        updateREADME("docs/getting_started.md",oldVersionName,newVersionName)
        updateREADME("docs/android_aop_extra.md",oldVersionName,newVersionName)
        updateREADME("docs/zh/getting_started.md",oldVersionName,newVersionName)
        updateREADME("docs/zh/android_aop_extra.md",oldVersionName,newVersionName)



    }
}


task aaBumpVersion2() {
    doLast {
        def versionPropsFile = file('gradle.properties')
        def versionProps = new Properties()
        versionProps.load(new FileInputStream(versionPropsFile))
        def oldVersionName = versionProps['TestVersion']

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


        gradleFile.write(text2)

        println("升级版本号完成，versionName = "+newVersionName)
    }
}

//subprojects { subproject ->
//    subproject.afterEvaluate {
//        def list = new ArrayList<String>()
//        subproject.tasks.forEach { task ->
//            if (task.name.contains("publish")){
//                println("task.name="+task.name)
//            }
//
//            if ("publishAllPublicationsToMavenCentralRepository" == task.name){
//                list.add("${subproject.path}:publishMavenPublicationToMavenLocal")
//            }
//        }
//        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:androidaop-plugin:"+oldVersionName,"io.github.flyjingfish:androidaop-plugin:"+newVersionName)
    text = text.replace("io.github.flyjingfish:androidaop-core:"+oldVersionName,"io.github.flyjingfish:androidaop-core:"+newVersionName)
    text = text.replace("io.github.flyjingfish:androidaop-annotation:"+oldVersionName,"io.github.flyjingfish:androidaop-annotation:"+newVersionName)
    text = text.replace("io.github.flyjingfish:androidaop-extra:"+oldVersionName,"io.github.flyjingfish:androidaop-extra:"+newVersionName)
    text = text.replace("io.github.flyjingfish:androidaop-apt:"+oldVersionName,"io.github.flyjingfish:androidaop-apt:"+newVersionName)
    text = text.replace("id \"io.github.flyjingfish.androidaop\" version \""+oldVersionName+"\"","id \"io.github.flyjingfish.androidaop\" version \""+newVersionName+"\"")
    text = text.replace("id(\"io.github.flyjingfish.androidaop\") version \""+oldVersionName+"\"","id(\"io.github.flyjingfish.androidaop\") version \""+newVersionName+"\"")
    configFile.write(text)
}

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


task runPublishScript(type: Exec) {
    group = "publishing"
    description = "Runs the publish.sh shell script"
    workingDir = projectDir
    commandLine './publish.sh'
}
/**
 chmod +x publish.sh
 ./publish.sh
 */
