buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "com.mobidevelop.robovm:robovm-gradle-plugin:$robovm_version"
    }
}

plugins {
    id 'java'
    id 'kotlin'
}

apply plugin: "robovm"

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

task convertStrings {
    def source = '../android/build/intermediates/incremental/ergomainnetDebug/mergeErgomainnetDebugResources/merged.dir/values{l}/values{l}.xml'
    def dest = 'resources/i18n/strings{l}.properties'
    def dest2 = '../common-jvm/src/main/java/org/ergoplatform/uilogic/StringResources.kt'

    ext.changePlaceHolders = { stringText ->
        return stringText.replace("%1\$d", "{0}").replace("%1\$s", "{0}")
                .replace("%2\$d", "{1}").replace("%2\$s", "{1}")
                .replace("%3\$d", "{2}").replace("%3\$s", "{2}")
                .replace("%4\$d", "{3}").replace("%4\$s", "{3}")
    }

    ext.addEndLineChar = { line ->
        if (!line.endsWith("\\n"))
            return line + " \\"
        else
            return line + "\\"
    }

    ext.convertFile = { language ->
        def primaryLanguage = language == ""

        def output = project.file(dest.replace("{l}", primaryLanguage ? "" : "_" + language))
        def output2 = project.file(dest2)
        def input = project.file(source.replace("{l}", primaryLanguage ? "" : "-" + language.replace("_", "-r")))

        try {
            if (!output.parentFile.exists())
                output.parentFile.mkdirs()

            if (!output2.parentFile.exists())
                output2.parentFile.mkdirs()

            if (!input.exists())
                logger.error("Android merged values file not found. Please run gradlew clean convertStrings")

            def endTag = "</string>"

            def lines = input.text.readLines()
            def strings = new ArrayList<String>()
            def names = new ArrayList<String>()
            names.add("package org.ergoplatform.uilogic")
            names.add("// Auto-generated by gradlew ios:convertStrings")
            def isInString = false

            for (line in lines) {
                line = line.trim()
                if (isInString) {
                    def value
                    if (line.indexOf(endTag) >= 0) {
                        value = line.substring(0, line.indexOf(endTag))
                        isInString = false
                    } else
                        value = addEndLineChar(line)

                    strings.add(changePlaceHolders(value))
                } else if (line.startsWith("<string name=")) {
                    def name = line.substring(14)
                    name = name.substring(0, name.indexOf('"'))

                    if (line.indexOf("/>") < 0) {
                        def value = line.substring(line.indexOf('>') + 1)
                        if (value.indexOf(endTag) >= 0) {
                            value = value.substring(0, value.indexOf(endTag))
                        } else {
                            value = addEndLineChar(value)
                            isInString = true
                        }
                        strings.add(name + "=" + changePlaceHolders(value))
                        names.add("const val STRING_" + name.toUpperCase() + " = \"" + name + "\"")
                    }
                }
            }

            output.write(strings.join('\n'))
            if (primaryLanguage) output2.write(names.join('\n'))
            logger.info("Updated strings")
        } catch (ex) {
            throw new GradleException('Exception writing strings file', ex)
        }
    }

    doFirst {
        convertFile("")
        convertFile("zh")
        convertFile("es")
        convertFile("pt")
        convertFile("pt_PT")
        convertFile("it")
        convertFile("tr")
        convertFile("ru")
        convertFile("in")
    }
}

dependencies {
    implementation(project(path: ':common-jvm')){
        // normal compile no change iOS: 90 sec
        // exclude kiama, sigma-state, ergo-wallet: 20 sec
        // exclude group: 'org.scorexfoundation', module: 'sigma-state_2.11'
        //exclude group: 'org.ergoplatform', module: 'ergo-wallet_2.11'
    }

    implementation project(path: ':sqldelight')

    implementation "com.squareup.sqldelight:sqlite-driver:$sqldelight_version"
    implementation "com.mobidevelop.robovm:robovm-rt:${robovm_version}"
    implementation "com.mobidevelop.robovm:robovm-cocoatouch:${robovm_version}"

    // Mosaik
    implementation "com.github.MrStahlfelge.mosaik:common-abstractui:$mosaik_version"
    implementation "org.jetbrains:markdown-jvm:0.3.1"
}
