import groovy.json.JsonSlurper

/*
 * Copyright (c) 2017, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

buildscript {
    apply from: 'forceVersions.gradle'
    forceVersions(configurations)

    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath('com.android.tools.build:gradle:8.4.1')
    }
}

plugins {
    id('maven-publish')
}

apply plugin: 'com.android.library'

def safeExtGet(prop, fallback) {
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def DEFAULT_COMPILE_SDK_VERSION = 34
def DEFAULT_MIN_SDK_VERSION = 23
def DEFAULT_TARGET_SDK_VERSION = 34

android {
  namespace "com.oktareactnative"
  compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)

  defaultConfig {
      minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
      targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
      versionCode 1
      versionName "1.0"
      manifestPlaceholders = [
              "appAuthRedirectScheme": ""
      ]
  }
  lintOptions {
    abortOnError false
  }
}

allprojects {
    forceVersions(configurations)

    repositories {
        mavenCentral()
        google()
        maven { url 'https://www.jitpack.io' }
    }
}

dependencies {
    implementation "com.facebook.react:react-android:+"
    implementation 'com.okta.android:okta-oidc-android:1.3.4'
    implementation 'com.squareup.okhttp3:okhttp:4.12.0'
    implementation 'com.squareup.okio:okio:3.5.0'
    implementation 'com.squareup.okio:okio-jvm:3.5.0'
}

def configureReactNativePom(def pom) {
    def packageJson = new JsonSlurper().parseText(file('../package.json').text)

    pom.project {
        name packageJson.title
        artifactId packageJson.name
        version = packageJson.version
        group = "com.oktareactnative"
        description packageJson.description
        url packageJson.repository.baseUrl

        licenses {
            license {
                name packageJson.license
                url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename
                distribution 'repo'
            }
        }
    }
}

afterEvaluate { project ->

    task androidSourcesJar(type: Jar) {
        archiveClassifier = 'sources'
        from android.sourceSets.main.java.srcDirs
        include '**/*.java'
    }

    android.libraryVariants.all { variant ->
        def name = variant.name.capitalize()
        def javaCompileTask = variant.javaCompileProvider.get()

        task "jar${name}"(type: Jar, dependsOn: javaCompileTask) {
            from javaCompileTask.destinationDir
        }
    }

    artifacts {
        archives androidSourcesJar
    }

    publishing {
        publications {
            maven(MavenPublication) {
                artifact androidSourcesJar
            }
        }
    }
}
