plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
    id 'maven-publish' // Support for maven publishing artifacts
    id 'signing' // Support for signing artifacts
}

android {
    compileSdk 33

    defaultConfig {
        minSdk 24
        targetSdk 33
        versionCode 176
        versionName "1.7.6"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    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'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose_compiler_version
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
    buildToolsVersion '30.0.3'
}

dependencies {
    implementation 'androidx.core:core-ktx:1.10.1'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
    implementation "androidx.lifecycle:lifecycle-runtime-compose:2.6.1"
    implementation 'androidx.activity:activity-compose:1.7.2'
    implementation 'androidx.preference:preference-ktx:1.2.0'
    implementation "androidx.constraintlayout:constraintlayout-compose:1.0.1"
    implementation "org.jetbrains.kotlin:kotlin-reflect:1.8.21"
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation "androidx.compose.material:material-icons-extended:1.4.3"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation "androidx.biometric:biometric-ktx:1.2.0-alpha05"
    implementation "androidx.compose.material3:material3:$material3_version"
    implementation "androidx.compose.animation:animation-graphics:$compose_version"
    implementation "androidx.compose.animation:animation:$compose_version"
    implementation "androidx.compose.animation:animation-core:$compose_version"
    implementation "com.google.accompanist:accompanist-navigation-animation:$accompanist_version"
    implementation "com.google.accompanist:accompanist-pager:$accompanist_version"
    implementation "com.google.accompanist:accompanist-pager-indicators:$accompanist_version"
    implementation "com.google.accompanist:accompanist-swiperefresh:$accompanist_version"
    implementation "com.google.accompanist:accompanist-permissions:$accompanist_version"
    implementation 'androidx.lifecycle:lifecycle-service:2.6.1'
    implementation 'com.google.code.gson:gson:2.10.1'

    // coil, like glide provides pictures asynchronously, but coil has better integration with compose
    implementation "io.coil-kt:coil-compose:$coil_version"
    implementation "io.coil-kt:coil-svg:$coil_version"
    implementation "io.coil-kt:coil-gif:$coil_version"
    implementation "io.coil-kt:coil-compose:$coil_version"

}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
    kotlinOptions {
        freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
    }
}

// Settings for publishing at mavenCentral

ext{
    publishedGroupId = "io.github.afalabarce"
    libraryName = "jetpackcompose"
    artifact = "jetpackcompose"
    libraryDescription = "Another Project for Jetpack Compose Composable Library"
    siteUrl = "https://github.com/afalabarce/jetpackcompose"
    gitUrl = "https://github.com/afalabarce/jetpackcompose.git"
    libraryVersionId = android.defaultConfig.versionCode
    libraryVersionCode = android.defaultConfig.versionName
    developerId = "afalabarce"
    developerName = "Antonio Fdez. Alabarce"
    developerEmail = "afalabarce@gmail.com"
    licenseName = "The Apache Software License, Version 2.0"
    licenseUrl = "http://www.apache.org/licenses/LICENSE-2.0.txt"
    allLicenses = ["Apache-2.0"]
}

task androidSourcesJar(type: Jar) {
    archiveClassifier = 'sources'
    from android.sourceSets.main.java.source
}

artifacts {
    archives androidSourcesJar
}

group = publishedGroupId
version = libraryVersionCode

ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.secretKeyRingFile"] = ''
ext["ossrhUsername"] = ''
ext["ossrhPassword"] = ''

File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
    println "Found secret props file, loading props"
    Properties p = new Properties()
    p.load(new FileInputStream(secretPropsFile))
    p.each { name, value ->
        println "Prop: $name -> $value"
        ext[name] = value
    }
}

signing {
    sign publishing.publications
}

publishing {
    publications {
        release(MavenPublication) {
            // The coordinates of the library, being set from variables that
            // we'll set up in a moment
            groupId publishedGroupId
            artifactId artifact
            version libraryVersionCode

            println "groupId: $publishedGroupId"
            println "Artifact: $artifact"
            println "Version: $libraryVersionCode"

            // Two artifacts, the `aar` and the sources
            artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
            artifact androidSourcesJar

            // Self-explanatory metadata for the most part
            pom {
                name = artifact
                description = libraryDescription
                // If your project has a dedicated site, use its URL here
                url = gitUrl
                licenses {
                    license {
                        name = licenseName
                        url = licenseUrl
                    }
                }
                developers {
                    developer {
                        id = developerId
                        name = developerName
                        email = developerEmail
                    }
                }
                // Version control info, if you're using GitHub, follow the format as seen here
                scm {
                    connection = 'scm:git:github.com/afalabarce/jetpackcompose.git'
                    developerConnection = 'scm:git:ssh://github.com/afalabarce/jetpackcompose.git'
                    url = 'https://github.com/afalabarce/jetpackcompose/tree/master'
                }
                // A slightly hacky fix so that your POM will include any transitive dependencies
                // that your library builds upon
                withXml {
                    def dependenciesNode = asNode().appendNode('dependencies')

                    project.configurations.implementation.allDependencies.each {
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                    }
                }
            }
        }
    }
    repositories {
        // The repository to publish to, Sonatype/MavenCentral
        maven {
            // This is an arbitrary name, you may also use "mavencentral" or
            // any other name that's descriptive for you
            name = "sonatype"
            // these urls depend on the configuration provided to the user by sonatype
            def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
            def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
            // You only need this if you want to publish snapshots, otherwise just set the URL
            // to the release repo directly
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

            // The username and password we've fetched earlier
            credentials {
                username ossrhUsername
                password ossrhPassword
            }
        }
    }
}
