import java.text.SimpleDateFormat

tasks.register("incrementVersionMajor") {
    doLast {
        incrementVersion true, false, false
    }
}

tasks.register("incrementVersionMinor") {
    doLast {
        incrementVersion false, true, false
    }
}

tasks.register("incrementVersionPatch") {
    doLast {
        incrementVersion false, false, true
    }
}

tasks.register("enableDatedVersionName") {
    enableDatedVersionName()
}

def incrementVersion(maj, min, patch) {
    def versionMajor = getProperty('version.versionMajor').toInteger()
    def versionMinor = getProperty('version.versionMinor').toInteger()
    def versionPatch = getProperty('version.versionPatch').toInteger()

    if(maj){
        versionMajor++
        versionMinor = 0
        versionPatch = 0
    }

    if(min){
        versionMinor++
        versionPatch = 0
    }

    if(patch){
        versionPatch++
    }

    def file = file('gradle.properties')
    def sb = new StringBuilder()

    file.eachLine {line ->
        if(line.startsWith('version.versionMajor=')){
            sb.append("version.versionMajor=${versionMajor}")
        }else if(line.startsWith('version.versionMinor=')){
            sb.append("version.versionMinor=${versionMinor}")
        }else if(line.startsWith('version.versionPatch=')){
            sb.append("version.versionPatch=${versionPatch}")
        }else{
            sb.append(line)
        }

        sb.append(System.getProperty("line.separator"))
    }

    file.text = sb.toString()
}

def enableDatedVersionName() {
    def file = file('gradle.properties')
    def sb = new StringBuilder()

    def versionMajor = getProperty('version.versionMajor').toInteger()
    def versionMinor = getProperty('version.versionMinor').toInteger()
    def versionPatch = getProperty('version.versionPatch').toInteger()

    file.eachLine {line ->
        if(line.startsWith('version.versionMajor=')){
            sb.append("version.versionMajor=${versionMajor}")
        }else if(line.startsWith('version.versionMinor=')){
            sb.append("version.versionMinor=${versionMinor}")
        }else if(line.startsWith('version.versionPatch=')){
            sb.append("version.versionPatch=${versionPatch}")
        } else if(line.startsWith('version.useDatedVersionName=false')){
            sb.append("version.useDatedVersionName=true")
        }else{
            sb.append(line)
        }

        sb.append(System.getProperty("line.separator"))
    }

    file.text = sb.toString()
}

ext.getBuildVersionName = {
    def versionMajor = getProperty('version.versionMajor').toInteger()
    def versionMinor = getProperty('version.versionMinor').toInteger()
    def versionPatch = getProperty('version.versionPatch').toInteger()
    def isDatedVersionNameEnabled = getProperty('version.useDatedVersionName').toBoolean()
    if (isDatedVersionNameEnabled) {
        def date = getCurrentDate()
        return "${versionMajor}.${versionMinor}.${versionPatch}-${date}"
    } else {
        return "${versionMajor}.${versionMinor}.${versionPatch}-alpha"
    }
}

ext.getBuildVersionCode = {
    def versionMajor = getProperty('version.versionMajor').toInteger()
    def versionMinor = getProperty('version.versionMinor').toInteger()
    def versionPatch = getProperty('version.versionPatch').toInteger()
    return versionMajor * 1000 + versionMinor * 100 + versionPatch
}

static def getCurrentDate() {
    def date = new Date()
    def sdf = new SimpleDateFormat("dd.MM.yy")
    return sdf.format(date)
}