import java.security.MessageDigest

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.baidu.paddlex.lite.demo"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.aar'], dir: 'libs')
    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 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}


def paddlexAndroidSdk = 'https://bj.bcebos.com/paddlex/deploy/lite/paddlex_lite_11cbd50e.tar.gz'

task downloadAndExtractPaddleXAndroidSdk(type: DefaultTask) {
    doFirst {
        println "Downloading and extracting PaddleX Android 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(paddlexAndroidSdk.bytes)
        String cacheName = new BigInteger(1, messageDigest.digest()).toString(32)
        // Download sdk
        if (!file("cache/${cacheName}.tar.gz").exists()) {
            ant.get(src: paddlexAndroidSdk, dest: file("cache/${cacheName}.tar.gz"))
        }
        // Unpack sdk
        copy {
            from tarTree("cache/${cacheName}.tar.gz")
            into "cache/${cacheName}"
        }
        // Copy sdk
        if (!file("libs/paddlex.aar").exists()) {
            copy {
                from "cache/${cacheName}/paddlex.aar"
                into "libs"
            }
        }
    }
}

preBuild.dependsOn downloadAndExtractPaddleXAndroidSdk

def paddleXLiteModel = 'https://bj.bcebos.com/paddlex/deploy/lite/mobilenetv2_imagenet_lite2.6.1.tar.gz'
task downloadAndExtractPaddleXLiteModel(type: DefaultTask) {
    doFirst {
        println "Downloading and extracting PaddleX Android SDK"}

    doLast {
        // Prepare cache folder for model
        if (!file("cache").exists()) {
            mkdir "cache"
        }
        // Generate cache name for model
        MessageDigest messageDigest = MessageDigest.getInstance('MD5')
        messageDigest.update(paddleXLiteModel.bytes)
        String cacheName = new BigInteger(1, messageDigest.digest()).toString(32)
        // Download sdk
        if (!file("cache/${cacheName}.tar.gz").exists()) {
            ant.get(src: paddleXLiteModel, dest: file("cache/${cacheName}.tar.gz"))
        }

        // Unpack model
        copy {
            from tarTree("cache/${cacheName}.tar.gz")
            into "cache/${cacheName}"
        }

        // Copy model.nb
        if (!file("src/main/assets/model/model.nb").exists()) {
            copy {
                from "cache/${cacheName}/model.nb"
                into "src/main/assets/model/"
            }
        }
        // Copy config file model.yml
        if (!file("src/main/assets/config/model.yml").exists()) {
            copy {
                from "cache/${cacheName}/model.yml"
                into "src/main/assets/config/"
            }
        }
        // Copy config file model.yml
        if (!file("src/main/assets/images/test.jpg").exists()) {
            copy {
                from "cache/${cacheName}/test.jpg"
                into "src/main/assets/images/"
            }
        }
    }

}

preBuild.dependsOn downloadAndExtractPaddleXLiteModel
