import java.security.MessageDigest

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar','*.aar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:design:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}


def paddleLiteLibs = 'https://bj.bcebos.com/paddlex/deploy/lite/paddle_lite_version_11cbd50e.tar.gz'
task downloadAndExtractPaddleLiteLibs(type: DefaultTask) {
    doFirst {
        println "Downloading and extracting Paddle Lite libs"
    }
    doLast {
        // Prepare cache folder for libs
        if (!file("cache").exists()) {
            mkdir "cache"
        }
        // Generate cache name for libs
        MessageDigest messageDigest = MessageDigest.getInstance('MD5')
        messageDigest.update(paddleLiteLibs.bytes)
        String cacheName = new BigInteger(1, messageDigest.digest()).toString(32)
        // Download libs
        if (!file("cache/${cacheName}.tar.gz").exists()) {
            ant.get(src: paddleLiteLibs, dest: file("cache/${cacheName}.tar.gz"))
        }
        // Unpack libs
        copy {
            from tarTree("cache/${cacheName}.tar.gz")
            into "cache/${cacheName}"
        }
        // Copy PaddlePredictor.jar
        if (!file("libs/PaddlePredictor.jar").exists()) {
            copy {
                from "cache/${cacheName}/PaddlePredictor.jar"
                into "libs"
            }
        }
        // Copy libpaddle_lite_jni.so for armeabi-v7a and arm64-v8a
        if (!file("src/main/jniLibs/armeabi-v7a/libpaddle_lite_jni.so").exists()) {
            copy {
                from "cache/${cacheName}/libs/armeabi-v7a/"
                into "src/main/jniLibs/armeabi-v7a"
            }
        }
        if (!file("src/main/jniLibs/arm64-v8a/libpaddle_lite_jni.so").exists()) {
            copy {
                from "cache/${cacheName}/libs/arm64-v8a/"
                into "src/main/jniLibs/arm64-v8a"
            }
        }
    }
}
preBuild.dependsOn downloadAndExtractPaddleLiteLibs

def snakeYamlLibs = 'https://bj.bcebos.com/paddlex/deploy/lite/snakeyaml-1.18-android.tar.gz'
task downloadAndExtractSnakeYamlLibs(type: DefaultTask) {
    doFirst {
        println "Downloading and extracting snake yaml sdk"
    }
    doLast {
        // Prepare cache folder for sdk
        if (!file("cache").exists()) {
            mkdir "cache"
        }
        // Generate cache name for sdk
        MessageDigest messageDigest = MessageDigest.getInstance('MD5')
        messageDigest.update(snakeYamlLibs.bytes)
        String cacheName = new BigInteger(1, messageDigest.digest()).toString(32)
        // Download libs
        if (!file("cache/${cacheName}.tar.gz").exists()) {
            ant.get(src: snakeYamlLibs, dest: file("cache/${cacheName}.tar.gz"))
        }
        // Unpack libs
        copy {
            from tarTree("cache/${cacheName}.tar.gz")
            into "cache/${cacheName}"
        }
        // Copy .jar
        if (!file("libs/snakeyaml-1.18-android.jar").exists()) {
            copy {
                from "cache/${cacheName}/snakeyaml-1.18-android.jar"
                into "libs"
            }
        }
    }
}
preBuild.dependsOn downloadAndExtractSnakeYamlLibs

def opencvLibs = 'https://bj.bcebos.com/paddlex/deploy/lite/opencv-3.4.6-android.tar.gz'
task downloadAndExtractOpencvLibs(type: DefaultTask) {
    doFirst {
        println "Downloading and extracting opencv sdk"
    }
    doLast {
        // Prepare cache folder for sdk
        if (!file("cache").exists()) {
            mkdir "cache"
        }
        // Generate cache name for sdk
        MessageDigest messageDigest = MessageDigest.getInstance('MD5')
        messageDigest.update(opencvLibs.bytes)
        String cacheName = new BigInteger(1, messageDigest.digest()).toString(32)
        // Download libs
        if (!file("cache/${cacheName}.tar.gz").exists()) {
            ant.get(src: opencvLibs, dest: file("cache/${cacheName}.tar.gz"))
        }
        // Unpack libs
        copy {
            from tarTree("cache/${cacheName}.tar.gz")
            into "cache/${cacheName}"
        }
        // Copy .jar
        if (!file("libs/opencv346.jar").exists()) {
            copy {
                from "cache/${cacheName}/opencv346.jar"
                into "libs"
            }
        }
        // Copy .so for armeabi-v7a and arm64-v8a
        if (!file("src/main/jniLibs/armeabi-v7a/libopencv_java3.so").exists()) {
            copy {
                from "cache/${cacheName}/libs/armeabi-v7a/"
                into "src/main/jniLibs/armeabi-v7a"
            }
        }
        if (!file("src/main/jniLibs/arm64-v8a/libopencv_java3.so").exists()) {
            copy {
                from "cache/${cacheName}/libs/arm64-v8a/"
                into "src/main/jniLibs/arm64-v8a"
            }
        }
    }
}

preBuild.dependsOn downloadAndExtractOpencvLibs
