import com.android.Version

buildscript {
    ext.getExtOrDefault = {name ->
        return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['TrackPlayer_' + name]
    }

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.12.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.10"
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

if (!rootProject.ext.has("native")) {
    apply plugin: 'com.facebook.react'
}

def getExtOrIntegerDefault(name, defaultInt) {
    try {
        return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['RNTP_' + name]).toInteger()
    } catch (ignored) {
        return defaultInt
    }
}

android {
    compileSdkVersion getExtOrIntegerDefault('compileSdkVersion', 35)

    def agpVersion = Version.ANDROID_GRADLE_PLUGIN_VERSION
    if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
      namespace "com.doublesymmetry.trackplayer"
    }
    defaultConfig {
        minSdkVersion getExtOrIntegerDefault('minSdkVersion', 23) // RN's minimum version
        targetSdkVersion getExtOrIntegerDefault('targetSdkVersion', 34)


        consumerProguardFiles 'proguard-rules.txt'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    if (rootProject.ext.has("native")) {
        kotlinOptions {
            // HACK: does RN auto sets to 17? or am i crazy?
            jvmTarget = "1.8"
        }
    }

    sourceSets {
        main {
            java.srcDirs += ["build/generated/source/codegen"]
        }
    }
}

repositories {
    mavenLocal()
    maven {
        // As RN is not updated in jcenter anymore, we'll have to grab it from npm
        // Make sure you have installed the react-native npm package before compiling
        url '../node_modules/react-native/android'
    }

    mavenCentral()
    google()
}

dependencies {
    //noinspection GradleDynamicVersion
    if (rootProject.ext.has("native")) {
        implementation "com.facebook.react:react-android:+"
    } else {
        implementation "com.facebook.react:react-native:+"
    }
    // Make sure we're using androidx
    implementation "androidx.core:core-ktx:1.17.0"
    implementation "androidx.localbroadcastmanager:localbroadcastmanager:1.1.0"
    implementation "androidx.lifecycle:lifecycle-process:2.9.3"
    implementation 'androidx.media:media:1.7.1'

    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.9.3")
    implementation("androidx.activity:activity-compose:1.10.1")
    implementation("androidx.compose.ui:ui:1.9.0")
    implementation("androidx.compose.ui:ui-graphics:1.9.0")

    implementation("androidx.media3:media3-exoplayer:1.8.0")
    implementation("androidx.media3:media3-session:1.8.0")
    implementation("androidx.media3:media3-ui:1.8.0")
    implementation("androidx.media3:media3-exoplayer-hls:1.8.0")
    implementation("androidx.media3:media3-exoplayer-dash:1.8.0")
    implementation("androidx.media3:media3-exoplayer-smoothstreaming:1.8.0")
    implementation("androidx.media3:media3-common:1.8.0")
    implementation("org.jellyfin.media3:media3-ffmpeg-decoder:1.8.0+1")

    implementation "com.google.guava:guava:33.4.8-android"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-guava:1.10.2"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2"

    implementation 'io.coil-kt:coil:2.7.0'
    api 'com.jakewharton.timber:timber:5.0.1'

    implementation 'androidx.test:rules:1.7.0'
    implementation 'jp.wasabeef.transformers:coil:1.0.6'
}
