buildscript {
  // Buildscript is evaluated before everything else so we can't use getExtOrDefault
  def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["GiphyReactNativeSDK.kotlinVersion"]

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:8.12.0"
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

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

import groovy.json.JsonSlurper

def reactNativeVersion = {
  def reactNativeManifest = file("$projectDir/../node_modules/react-native/package.json").exists()
    ? file("$projectDir/../node_modules/react-native/package.json") // developer mode, to run example app
    : file("$projectDir/../../../react-native/package.json")
  new JsonSlurper().parseText(reactNativeManifest.text).version as String
}()

def isNewArchitectureEnabled() {
  return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }

if (isNewArchitectureEnabled()) {
  apply plugin: "com.facebook.react"
}

def getExt(name, prefix = 'GiphyReactNativeSDK.') {
  return rootProject.ext.has(prefix + name) ? rootProject.ext.get(prefix + name) : null
}

def getExtOrDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["GiphyReactNativeSDK." + name]
}

def getExtOrIntegerDefault(name) {
  return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["GiphyReactNativeSDK." + name]).toInteger()
}

android {
  buildFeatures {
    buildConfig = true
  }

  namespace "com.giphyreactnativesdk"

  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault("minSdkVersion")
    targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
  }

  buildTypes {
    release {
      minifyEnabled false
    }
  }

  lintOptions {
    disable "GradleCompatible"
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_17
    targetCompatibility JavaVersion.VERSION_17
  }

  sourceSets {
    main {
      if (isNewArchitectureEnabled()) {
        java.srcDirs += [
          "src/newarch",
          // This is needed to build Kotlin project with NewArch enabled
          "${project.buildDir}/generated/source/codegen/java"
        ]
      } else {
        java.srcDirs += ["src/oldarch"]
      }
    }
  }
}

repositories {
  mavenCentral()
  google()
}

def kotlin_version = getExtOrDefault("kotlinVersion")

task configureGiphyVideoPlayerAdapter {
  String sdkRoot = new File(projectDir, '..')
  String targetFolder = new File(sdkRoot, 'android/src/main/java/com/giphyreactnativesdk/videoplayeradapter')
  String sdkResources = new File(sdkRoot, 'android/resources')

  // copy the default implementation
  apply from: new File(sdkResources, 'videoplayeradapter/dependencies.gradle')
  project.delete(targetFolder)
  project.copy {
    from new File(sdkResources, 'videoplayeradapter')
    include '**/*.*'
    exclude 'dependencies.gradle'
    into targetFolder
  }

  // replace with a custom adapter
  String videoPlayerAdapter = getExt('videoPlayerAdapter')
  if (!videoPlayerAdapter) {
    return
  }
  String videoPlayerAdaptersPath = getExt('videoPlayerAdaptersPath') ?: sdkResources
  File videoPlayerAdapterFullPath = new File(videoPlayerAdaptersPath, videoPlayerAdapter)
  if (!videoPlayerAdapterFullPath.exists()) {
    throw new GradleException("The video player adapter \"${videoPlayerAdapter}\" " +
      "cannot be found in this \"${videoPlayerAdaptersPath}\" folder.")
  }
  apply from: new File(videoPlayerAdapterFullPath, 'dependencies.gradle')
  project.copy {
    from videoPlayerAdapterFullPath
    include '**/*.*'
    exclude 'dependencies.gradle'
    into targetFolder
  }
}

preBuild.dependsOn configureGiphyVideoPlayerAdapter

dependencies {
  implementation "com.facebook.react:react-android:$reactNativeVersion"
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation 'com.giphy.sdk:ui:2.5.1'
}

if (isNewArchitectureEnabled()) {
  react {
    jsRootDir = file("../src/")
    libraryName = "GiphyReactNativeSDK"
    codegenJavaPackageName = "com.giphyreactnativesdk"
  }
}
