import org.yaml.snakeyaml.Yaml
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.io.Output

apply plugin: 'com.android.application'

buildscript {
    repositories {
        google()
        jcenter()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }
    dependencies {
        // buildSrc trick to get around this egg-chicken problem
        // classpath 'com.eyeem.router:router-validator:0.0.7-SNAPSHOT'
        classpath 'com.esotericsoftware.kryo:kryo:2.20' // serializeRouter dependency
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.eyeem.routervalidator'

android {
    compileSdkVersion 25
    buildToolsVersion "${build_tools_version}"

    defaultConfig {
        applicationId "com.eyeem.nanorouter"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
    dataBinding {
        enabled = true
    }
}

router {
    path = "app/src/main/assets/server.yaml"
    packageName = "com.eyeem.nanorouter"
    decoratorsPackageName = "com.eyeem.nanorouter.moustache.decorator"
    resourcePackageName = "com.eyeem.nanorouter"
    configFor = "com.eyeem.nanorouter.nano.ResponseWrapper"
}

task serializeRouterMap {
    doFirst {
        String yamlString = new File(router.path).getText('UTF-8')
        LinkedHashMap<String, Object> routerMap = new LinkedHashMap<>(
                (Map<String, Object>) new Yaml().load(yamlString)
        ); // using linked hash map to preserve order

        try {
            File e = new File(router.path + ".kryo")
            Kryo kryo = new Kryo()
            Output output = new Output(new FileOutputStream(e))
            kryo.writeObject(output, routerMap)
            output.close()
            println "[serialize router] OK"
        } catch (Throwable t) {
            println "[serialize router] FAIL..." + t.toString()
        }
    }
}

// add serialize Router dependency
if (isAndroidStudio()) { // Android Studio needs this
    tasks.whenTaskAdded { task ->
        switch (task) {
            case "generateDebugAssets":
            case "generateReleaseAssets":
                task.dependsOn(compileRouter)
                break
        }
    }
}
else { // gradle needs this
    project.afterEvaluate {
        generateDebugAssets.dependsOn(serializeRouterMap)
        generateReleaseAssets.dependsOn(serializeRouterMap)
    }
}



def isAndroidStudio() {
    def sysprops = System.getProperties()
    if (sysprops['idea.platform.prefix'] != null) {
        return true;
    } else {
        return false;
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile "com.android.support:appcompat-v7:${compat_version}"
    compile "com.android.support:design:${compat_version}"
    compile "com.android.support:recyclerview-v7:${compat_version}"
    compile "com.android.support:cardview-v7:${compat_version}"
    compile 'com.esotericsoftware.kryo:kryo:2.20'
    compile 'com.nanohttpd:nanohttpd-webserver:2.1.1'
    compile 'com.github.jknack:handlebars:4.0.5'
    compile 'com.eyeem.decorator:annotation:0.0.1-SNAPSHOT'
    annotationProcessor 'com.eyeem.decorator:processor:0.0.1-SNAPSHOT'
    compile project(':router')
}
