ext {
    junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
    androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.6.1'
    androidxJunitVersion =  project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.5'
    androidxEspressoCoreVersion =  project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.5.1'
}

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.0'
    }
}

apply plugin: 'com.android.library'

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    namespace "app.captureid.plugins.cidprint.capacitorplugincidprint"
    compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 33
    defaultConfig {
        minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21
        targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 33
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}

repositories {
    google()
    jcenter()
    mavenCentral()
    mavenLocal()
    maven {
        url("$rootDir/../node_modules/@captureid/capacitor-cidprint/android")
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':capacitor-android')
    implementation "app.captureid:cidprinterlibrary:1.0.6"
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
    implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
    implementation 'app.captureid:supportv1:1.0.1'
}

def findNodesFolder(String searchpath, String pattern) {
    def result = []
    def f = file(searchpath)
    f.&eachDir { File path -> if ( path.name ==~ pattern ) {
        result << path
    }}
    if(result == []) result = findNodesFolder(searchpath + "/..", pattern)
    return result as String[]
}

tasks.configure {
    doFirst {
        project
                .parent
                .allprojects ({
                    getRepositories()
                    .mavenLocal({mavenRepository -> {
                        def nodesDir = findNodesFolder("$rootDir", 'node_modules').getAt(0)
                        mavenRepository.setUrl("$nodesDir/@captureid/capacitor-cidprint/android")
//                        mavenRepository.setUrl("$rootDir/../node_modules/@captureid/capacitor-cidprint/android")
                    }
                    })
                })
    }
}
