buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://plugins.gradle.org/m2/' }
    }
    dependencies {
        classpath group: 'com.google.code.gson', name: 'gson', version: '2.8.1'
    }
}

plugins {
    id 'checkstyle'
}

group 'org.jackhuang'
version '3.0'

subprojects {
    apply plugin: 'java'
    apply plugin: 'idea'
    apply plugin: 'maven-publish'
    apply plugin: 'checkstyle'

    repositories {
        flatDir name: 'libs', dirs: rootProject.file('lib')

        mavenCentral()

        maven { url 'https://jitpack.io' }
    }

    checkstyle {
        sourceSets = []
    }

    tasks.withType(Checkstyle) {
        exclude 'de/javawi/jstun'
    }

    sourceCompatibility = 1.8
    compileJava.options.encoding = "UTF-8"
    compileTestJava.options.encoding = "UTF-8"

    dependencies {
        testImplementation group: 'junit', name: 'junit', version: '4.12'
    }

    publishing {
        publications {
            maven(MavenPublication) {
                from components.java
            }
        }
        repositories {
            mavenLocal()
        }
    }
}

defaultTasks 'clean', 'build'

var jfxModules = ['base', 'graphics', 'controls', 'fxml', 'media', 'web']
var jfxArches = ['linux', 'linux-arm32-monocle', 'linux-aarch64', 'mac', 'mac-aarch64', 'win', 'win-x86']
var jfxVersion = "17"
var jfxRepos = ['https://repo1.maven.org/maven2', 'https://maven.aliyun.com/repository/central']
var jfxDependenciesFile = file('HMCL/src/main/resources/assets/openjfx-dependencies.json')
var jfxUnsupported = [
        'linux-arm32-monocle': ['media', 'web']
]

var jfxInClasspath = false

try {
    Class.forName("javafx.application.Application", false, this.getClass().getClassLoader())
    jfxInClasspath = true
} catch (Throwable ignored) {
}

if (!jfxInClasspath && JavaVersion.current() >= JavaVersion.VERSION_11) {
    String os = null
    String arch = null

    String osName = System.getProperty("os.name").toLowerCase(Locale.US)
    if (osName.contains("win"))
        os = 'win'
    else if (osName.contains("mac"))
        os = 'mac'
    else if (osName.contains("solaris") || osName.contains("linux") || osName.contains("unix") || osName.contains("sunos"))
        os = 'linux'
    else
        os = null

    String archName = System.getProperty("os.arch").toLowerCase(Locale.US)
    switch (archName) {
        case "x86_64":
        case "x86-64":
        case "amd64":
            arch = ''
            break
        case "x86":
        case "x86_32":
        case "x86-32":
        case "i386":
        case "i486":
        case "i586":
        case "i686":
        case "i86pc":
            arch = '-x86'
            break
        case "aarch64":
            arch = '-aarch64';
            break
    }
    String platform = "$os$arch"

    if (os != null && arch != null && jfxArches.contains(platform)) {
        var jfxDependencies = jfxModules.collect { module -> "org.openjfx:javafx-$module:$jfxVersion:$platform" }

        subprojects {
            repositories {
                mavenCentral()
            }
            dependencies {
                jfxDependencies.forEach {
                    compileOnly it
                }
            }
        }
    }
}

import com.google.gson.*

task 'generateOpenJFXDependencies' {
    doLast {
        var jfxDependencies = new JsonArray()

        jfxModules.forEach { module ->
            JsonObject m = new JsonObject()
            m.addProperty("module", "javafx.$module")
            m.addProperty("groupId", "org.openjfx")
            m.addProperty("artifactId", "javafx-$module")
            m.addProperty("version", jfxVersion)

            var sha1 = new JsonObject()
            jfxArches.forEach { arch ->
                if (jfxUnsupported.getOrDefault(arch, []).contains(module)) {
                    return
                }

                sha1.addProperty(
                        arch,
                        new URL("${jfxRepos.head()}/org/openjfx/javafx-$module/$jfxVersion/javafx-$module-$jfxVersion-${arch}.jar.sha1").getText("UTF-8")
                )
            }
            m.add("sha1", sha1)

            jfxDependencies.add(m)
        }

        jfxDependenciesFile.text = new GsonBuilder().setPrettyPrinting().create().toJson(jfxDependencies)
    }
}

// Ensure that the mirror repository caches files
task 'preTouchOpenJFXDependencies' {
    doLast {
        jfxRepos.tail().forEach { repo ->
            jfxModules.forEach { module ->
                jfxArches.forEach { arch ->
                    if (jfxUnsupported.getOrDefault(arch, []).contains(module)) {
                        return
                    }
                    var jarUrl = "$repo/org/openjfx/javafx-$module/$jfxVersion/javafx-$module-$jfxVersion-${arch}.jar"

                    [jarUrl, jarUrl + ".sha1"].forEach { url ->
                        try {
                            new URL(url).getBytes()
                            System.out.println(url)
                        } catch (Throwable ignored) {
                            ignored.printStackTrace()
                        }
                    }
                }
            }
        }
    }
}

task 'checkTranslations' {
    group 'verification'
    doLast {
        var en = new Properties()
        var zh = new Properties()
        var zh_CN = new Properties()

        file("HMCL/src/main/resources/assets/lang/I18N.properties").withInputStream { en.load(it) }
        file("HMCL/src/main/resources/assets/lang/I18N_zh.properties").withInputStream { zh.load(it) }
        file("HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties").withInputStream { zh_CN.load(it) }

        boolean success = true

        zh_CN.forEach { k, v ->
            if (!en.containsKey(k)) {
                project.logger.log(LogLevel.WARN, "I18N.properties missing key '$k'")
                success = false
            }
        }

        zh_CN.forEach { k, v ->
            if (!zh.containsKey(k)) {
                project.logger.log(LogLevel.WARN, "I18N_zh.properties missing key '$k'")
                success = false
            }
        }

        if (!success) {
            throw new GradleException("Part of the translation is missing")
        }
    }
}