plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}

apply from: '../build.dependencies.gradle'

android {
    //TODO: Change this, and the folder structure for the code files to reflect the package name
    namespace 'com.bylazar.mylibrary'

    compileSdkVersion 35

    defaultConfig {
        minSdk 24
    }


    buildTypes {
        release {
            minifyEnabled false
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = '11'
    }
}

dependencies {
    implementation project(':ftcontrol')
}

//TODO: Change this
def libName = "my-library"
def libVersion = "0.1.0"
def jarFileName = "${libName}-${libVersion}.jar"

//TODO: This task builds a jar used for sharing your plugins.
tasks.register('copyMyLibraryJar', Copy) {
    dependsOn("build")

    def libsDir = file("${rootProject.projectDir}/TeamCode/libs")
    def stashDir = file("${rootProject.projectDir}/TeamCode/.lib-versions")
    def aarPath = layout.buildDirectory.file("outputs/aar/MyLibrary-release.aar").get().asFile

    doFirst {
        libsDir.mkdirs()
        libsDir.listFiles()?.findAll {
            it.name.endsWith(".jar") && it.name.startsWith(libName)
        }?.each {
            println "Deleting old jar: ${it.name}"
            it.delete()
        }
    }

    from(zipTree(aarPath)) {
        include "classes.jar"
        rename { jarFileName }
    }

    into(libsDir)

    doLast {
        def finalJar = new File(libsDir, jarFileName)

        stashDir.mkdirs()
        def backupFile = new File(stashDir, jarFileName)
        finalJar.withInputStream { input ->
            backupFile.withOutputStream { output ->
                output << input
            }
        }

        println "Copied ${jarFileName} to libs/"
        println "Backed up as ${jarFileName} to .jar-stash/"
    }
}