buildscript {
    // The Android Gradle plugin is only required when opening the android folder stand-alone.
    // This avoids unnecessary downloads and potential conflicts when the library is included as a
    // module dependency in an application project.
    if (project == rootProject) {
        repositories {
            google()
            mavenCentral()
            maven {
                url 'https://maven.google.com/'
                name 'Google'
            }
        }
        def buildGradleVersion = ext.has('buildGradlePluginVersion') ? ext.get('buildGradlePluginVersion') : '4.2.0'

        dependencies {
            classpath "com.android.tools.build:gradle:$buildGradleVersion"
        }
    }
}
def isNewArchitectureEnabled() {
    // To opt-in for the New Architecture, you can either:
    // - Set `newArchEnabled` to true inside the `gradle.properties` file
    // - Invoke gradle with `-newArchEnabled=true`
    // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
    return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

def getReactAndroidDir() {
    def reactAndroidDir = project.hasProperty("REACT_ANDROID_DIR") ? REACT_ANDROID_DIR : "$rootDir/../node_modules/react-native/ReactAndroid"
    return reactAndroidDir
}

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

apply plugin: 'com.android.library'

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

android {
    compileSdkVersion safeExtGet('compileSdkVersion', 34)

    namespace = "com.simi.pictureselector"
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', 24)
        targetSdkVersion safeExtGet('targetSdkVersion', 34)

        buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
        
        if (isNewArchitectureEnabled()) {
            externalNativeBuild {
                cmake {
                    arguments "-DPROJECT_BUILD_DIR=$buildDir",
                            "-DREACT_ANDROID_DIR=${getReactAndroidDir()}",
                            "-DANDROID_STL=c++_shared"
                }
            }
        }
    }
    
    if (isNewArchitectureEnabled()) {
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
    
    lintOptions {
        warning 'InvalidPackage', 'MissingPermission'
        abortOnError false
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {
        buildConfig = true
    }

    sourceSets.main {
        java {
            if (!isNewArchitectureEnabled()){
                srcDirs += 'src/paper/java'
            }
        }
    }

    packagingOptions {
        pickFirst '**/libc++_shared.so'
        pickFirst '**/libjsc.so'
    }
}
repositories {
    mavenCentral()
    google()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$rootDir/../node_modules/react-native/android"
    }
}

dependencies {
    implementation "com.facebook.react:react-native:${safeExtGet('reactNativeVersion', '+')}"
    
    def appCompatVersion = safeExtGet('appCompatVersion', '1.3.1')
    def recyclerViewVersion = safeExtGet('recyclerViewVersion', '1.2.1')
    def activityVersion = safeExtGet('activityVersion', '1.3.1')
    def fragmentVersion = safeExtGet('fragmentVersion', '1.3.1')
    def exifInterfaceVersion = safeExtGet('exifInterfaceVersion', '1.1.0')
    def viewPager2Version = safeExtGet('viewPager2Version', '1.0.0')
    def constraintLayoutVersion = safeExtGet('constraintLayoutVersion', '2.1.4')
    def glideVersion = safeExtGet('glideVersion', '4.15.1')
    def media3Version = safeExtGet('media3Version', '1.4.1')
    
    implementation "androidx.appcompat:appcompat:$appCompatVersion"
    implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"
    implementation "androidx.activity:activity:$activityVersion"
    implementation "androidx.fragment:fragment:$fragmentVersion"
    implementation "androidx.exifinterface:exifinterface:$exifInterfaceVersion"
    implementation "androidx.viewpager2:viewpager2:$viewPager2Version"
    implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
    implementation "com.github.bumptech.glide:glide:$glideVersion"
    
    //图片压缩、剪裁
    implementation 'io.github.lucksiege:ucrop:v3.11.2'
    implementation 'id.zelory:compressor:3.0.1'

    //视频压缩
    implementation "androidx.media3:media3-transformer:$media3Version"
    implementation "androidx.media3:media3-effect:$media3Version"
    implementation "androidx.media3:media3-common:$media3Version"
}
