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['EveTensorflowYolo_kotlinVersion']

  repositories {
      google()
      jcenter()
      mavenCentral()

      
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:4.1.2'
    classpath 'de.undercouch:gradle-download-task:3.4.3'
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

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

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'de.undercouch.download'


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

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

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

android {
  compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
  

  defaultConfig {
    minSdkVersion getExtOrIntegerDefault('minSdkVersion')
    targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
    buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a'
    }
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }

  aaptOptions {
        noCompress "tflite"
        
  }

  lintOptions {
   abortOnError false
  }

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}


// import DownloadModels task
project.ext.ASSET_DIR = projectDir.toString() + '/src/main/assets'
project.ext.TMP_DIR = project.buildDir.toString() + '/downloads'

repositories {
  mavenCentral()
  google()
  jcenter()
  // should be already there
  maven {         // add this repo to use snapshots
    name 'ossrh-snapshot'
    url 'https://oss.sonatype.org/content/repositories/snapshots'
  }

  def found = false
  def defaultDir = null
  def androidSourcesName = 'React Native sources'

  if (rootProject.ext.has('reactNativeAndroidRoot')) {
    defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
  } else {
    defaultDir = new File(
      projectDir,
      '/../../../node_modules/react-native/android'
    )
  }

  if (defaultDir.exists()) {
    maven {
      url defaultDir.toString()
      name androidSourcesName
    }

    logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
    found = true
  } else {
    def parentDir = rootProject.projectDir

    1.upto(5, {
      if (found) return true
      parentDir = parentDir.parentFile

      def androidSourcesDir = new File(
        parentDir,
        'node_modules/react-native'
      )

      def androidPrebuiltBinaryDir = new File(
        parentDir,
        'node_modules/react-native/android'
      )

      if (androidPrebuiltBinaryDir.exists()) {
        maven {
          url androidPrebuiltBinaryDir.toString()
          name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
        found = true
      } else if (androidSourcesDir.exists()) {
        maven {
          url androidSourcesDir.toString()
          name androidSourcesName
        }

        logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
        found = true
      }
    })
  }

  if (!found) {
    throw new FileNotFoundException(
      "${project.name}: unable to locate React Native android sources. " +
      "Ensure you have you installed React Native as a dependency in your project and try again."
    )
  }
}

def kotlin_version = getExtOrDefault('kotlinVersion')

dependencies {
    //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-native:+"
  //implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  //implementation 'androidx.appcompat:appcompat:1.0.0'
  testImplementation 'junit:junit:4.12'
  //implementation 'com.google.android.material:material:1.0.0'
  androidTestImplementation 'androidx.test:runner:1.1.0'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
  //implementation 'org.tensorflow:tensorflow-lite:2.3.0'
  
  //for yolo
  implementation 'androidx.appcompat:appcompat:1.1.0'
  implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'
  implementation 'com.google.android.material:material:1.1.0'
  //implementation 'org.tensorflow:tensorflow-lite:2.4.0'
  //implementation 'org.tensorflow:tensorflow-lite-gpu:2.4.0'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  implementation 'com.google.code.gson:gson:2.8.6'
  androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  androidTestImplementation 'com.android.support.test:rules:1.0.2'
  androidTestImplementation 'com.google.truth:truth:1.0.1'

  implementation 'org.tensorflow:tensorflow-lite:2.8.0'

  implementation 'org.tensorflow:tensorflow-lite-gpu:2.8.0'
 
  implementation 'org.tensorflow:tensorflow-lite-support:0.3.0'
// From node_modules
}

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

