import java.security.MessageDigest

plugins {
    id 'java'
    id 'idea'
    id 'com.github.johnrengelman.shadow' version '8.1.1'
}

String jarNameStr = "${groups}-${versions}"
String jarName = "${jarNameStr}.jar"

group = groups
version = versions


repositories {
    mavenLocal()
    maven { url 'https://maven.aliyun.com/repository/google' }
    maven { url 'https://maven.aliyun.com/repository/central' }
    maven { url 'https://maven.aliyun.com/repository/public' }
    maven { url "https://maven.aliyun.com/repository/gradle-plugin" }
    maven { url "https://maven.aliyun.com/repository/spring-plugin" }
    mavenCentral()
    gradlePluginPortal()
}


dependencies {
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
    compileOnly 'org.projectlombok:lombok:1.18.36'
    annotationProcessor 'org.projectlombok:lombok:1.18.36'
    implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.16'
    implementation 'ch.qos.logback:logback-classic:1.5.16'
    implementation 'org.slf4j:slf4j-api:2.0.16'
    implementation 'args4j:args4j:2.37'
    implementation 'com.alibaba.fastjson2:fastjson2:2.0.57'
    implementation 'commons-cli:commons-cli:1.9.0'
}

test {
    useJUnitPlatform()
}

ext.generateHash = { file, ads ->
    MessageDigest md = MessageDigest.getInstance(ads)
    file.eachByte(4096) { bytes, size ->
        md.update(bytes, 0x00, size)
    }
    return md.digest().encodeHex().toString()
}

ext.getHash = {
    File file ->
        {
            def path = rootDir.toPath().resolve("build").resolve("libs").toString()
            String md5 = generateHash(file, "MD5")
            String sha1 = generateHash(file, "SHA-1")
            String sha256 = generateHash(file, "SHA-256")
            File md5f = new File(path, file.getName() + ".MD5.txt")
            File sha1f = new File(path, file.getName() + ".SHA1.txt")
            File sha256f = new File(path, file.getName() + ".SHA256.txt")
            md5f.text = md5
            sha1f.text = sha1
            sha256f.text = sha256
            println ">>> 构建成功！"
            println ">>> MD5: ${md5}"
            println ">>> SHA1: ${sha1}"
            println ">>> SHA256: ${sha256}"
            println ">>> 输出文件: ${file.absolutePath}"
            println ">>> 输出文件: ${md5f.absolutePath}"
            println ">>> 输出文件: ${sha1f.absolutePath}"
            println ">>> 输出文件: ${sha256f.absolutePath}"
            println ">>> 文件大小: ${file.length() / 1024 / 1024} MB"
        }
}


tasks.register("addfile", Copy) {
    def path = rootDir.toPath().resolve("build").resolve("assets").toString()
    from zipTree(tasks.shadowJar.archiveFile.get())
    into path
    dependsOn tasks.shadowJar
    doFirst {
        mkdir path
    }
}

tasks.register("zipsrc", Zip) {
    def path = rootDir.toPath().resolve("build").resolve("libs").resolve(jarNameStr).toString()
    def pathsource = rootDir.toPath().resolve("src").resolve("main").resolve('java').toString()
    def pathresources = rootDir.toPath().resolve("src").resolve("main").resolve('resources').toString()
    archiveFileName = path + ".source.jar"
    entryCompression = ZipEntryCompression.DEFLATED
    from 'LICENSE' into '.'
    from 'README.md' into '.'
    from fileTree(pathresources) into '.'
    from fileTree(pathsource) into '.'
    doLast {
        getHash(new File(path + ".source.jar"))
    }
}

tasks.register("zipJar", Zip) {
    def path = rootDir.toPath().resolve("build").resolve("libs").toString()
    def jarpath = rootDir.toPath().resolve("build").resolve("libs").resolve(jarName).toString()
    def assetspath = rootDir.toPath().resolve("build").resolve("assets").toString()
    delete fileTree(path)
    archiveFileName = jarpath
    entryCompression = ZipEntryCompression.DEFLATED
    from fileTree(assetspath) into '.'
    from 'LICENSE' into '.'
    from 'README.md' into '.'
    dependsOn tasks.addfile
    dependsOn tasks.zipsrc
}

tasks.register("build-jar") {
    def path = rootDir.toPath().resolve("build").resolve("libs")
    def pathassets = rootDir.toPath().resolve("build").resolve("assets").toString()

    delete fileTree(path.toString())
    dependsOn tasks.zipJar

    doLast {
        getHash(path.resolve(jarName).toFile())
        delete pathassets
    }
}
shadowJar {
    archiveFileName = jarName
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
    minimize()
    manifest {
        attributes 'Main-Class': maimClassStr
        attributes 'Implementation-Title': groups
        attributes 'Implementation-Version': versions
        attributes 'Email': 'minglipro@163.com'
        attributes 'Implementation-Vendor': 'minglipro|Armamem0t'
        attributes 'Copyright': copyright
        attributes 'Env': 'prod'
        attributes 'LICENSE': 'Apache License 2.0'
        attributes 'Created': createdTime
        attributes 'Updated': new Date().format('yyyy-MM-dd HH:mm:ss')
    }
    from {
        project.configurations.runtimeClasspath.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    exclude '**/LICENSE'
    exclude '**/LICENSE.*'
    exclude '**/NOTICE.*'
}