plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
    id 'maven-publish'
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.0'
}

android {
    namespace 'com.bylazar.ftcontrol'
    compileSdk 35

    defaultConfig {
        minSdk 24

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = '11'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.15.0'
    implementation 'androidx.appcompat:appcompat:1.7.0'
    implementation 'com.google.android.material:material:1.12.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.2.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

    implementation 'org.firstinspires.ftc:Inspection:10.2.0'
    implementation 'org.firstinspires.ftc:Blocks:10.2.0'
    implementation 'org.firstinspires.ftc:RobotCore:10.2.0'
    implementation 'org.firstinspires.ftc:RobotServer:10.2.0'
    implementation 'org.firstinspires.ftc:OnBotJava:10.2.0'
    implementation 'org.firstinspires.ftc:Hardware:10.2.0'
    implementation 'org.firstinspires.ftc:FtcCommon:10.2.0'
    implementation 'org.firstinspires.ftc:Vision:10.2.0'

    implementation("org.nanohttpd:nanohttpd-websocket:2.3.1") {
        exclude module: "nanohttpd"
    }

    implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0'

    implementation "org.jetbrains.kotlin:kotlin-reflect:1.9.20"

}


tasks.register('installSvelteApp', Exec) {
    workingDir 'src/main/web'

    if (System.getProperty('os.name').toLowerCase().contains('windows')) {
        commandLine 'cmd', '/c', 'bun', 'install'
    } else {
        commandLine 'sh', '-c', 'bun install'
    }
}

tasks.register('buildSvelteApp', Exec) {
    dependsOn installSvelteApp
    workingDir 'src/main/web'

    if (System.getProperty('os.name').toLowerCase().contains('windows')) {
        commandLine 'cmd', '/c', 'bun', 'run', 'build'
    } else {
        commandLine 'sh', '-c', 'bun run build'
    }
}

tasks.register('clearAssets', Delete) {
    delete 'src/main/assets/web/*'
}

tasks.register('copySvelteToAssets', Copy) {
    dependsOn buildSvelteApp
    from 'src/main/web/build'
    into 'src/main/assets/web'
}

preBuild.dependsOn(clearAssets, copySvelteToAssets)

tasks.register('androidSourcesJar', Jar) {
    archiveClassifier = "sources"
    from android.sourceSets.main.java.srcDirs
}
publishing {
    repositories {
        maven {
            name = "ftcontrol"
            url = "https://mymaven.bylazar.com/releases"
            credentials(PasswordCredentials)
            authentication {
                basic(BasicAuthentication)
            }
        }
    }
    publications {
        maven(MavenPublication) {
            groupId = "com.bylazar"
            artifactId = "ftcontrol"

            version = "0.6.8-dev1"

            pom {
                description = "All in one toolbox for FTC."
                name = "FTControl"
                url = "https://ftcontrol.bylazar.com"

                licenses {
                    license {
                        name = "The MIT License"
                        url = "https://opensource.org/licenses/MIT"
                        distribution = "repo"
                    }
                }

                developers {
                    developer {
                        id = "lazar"
                        name = "Lazar Dragos George"
                        email = "hi@bylazar.com"
                    }
                }
            }

            afterEvaluate {
                from components.release
            }
        }
    }
}

