import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.aspectj:aspectjtools:1.8.9'
        classpath 'org.aspectj:aspectjweaver:1.8.9'
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

Properties properties = new Properties()
properties.load(project.rootProject.file('api.properties').newDataInputStream())
String pack = properties.getProperty("pack", "test")
String serverurl = properties.getProperty("serverurl." + pack)
String actionurl = properties.getProperty("actionurl." + pack)
String h5url = properties.getProperty("h5url." + pack)
String mh5url = properties.getProperty("mh5url." + pack)
String ydlh5url = properties.getProperty("ydlh5url." + pack)
String weburl = properties.getProperty("weburl." + pack)
String javaurl = properties.getProperty("javaurl." + pack)
String javatempurl = properties.getProperty("javatempurl." + pack)
String javaapi = properties.getProperty("javaapi." + pack)
String javacouponurl = properties.getProperty("javacouponurl." + pack)

//测试环境所有url
String testServerurl = properties.getProperty("serverurl.test")
String testActionurl = properties.getProperty("actionurl.test")
String testH5url = properties.getProperty("h5url.test")
String testydlh5url = properties.getProperty("ydlh5url.test")
String testweburl = properties.getProperty("weburl.test")
String testJavaurl = properties.getProperty("javaurl.test")
String testJavatempurl = properties.getProperty("javatempurl.test")
String testJavaapi = properties.getProperty("javaapi.test")
String testJavacouponurl = properties.getProperty("javacouponurl.test")

//预发布环境所有url
String preServerurl = properties.getProperty("serverurl.pre")
String preH5url = properties.getProperty("h5url.pre")

//线上环境所有url
String releaseServerurl = properties.getProperty("serverurl.prod")
String releaseActionurl = properties.getProperty("actionurl.prod")
String releaseH5url = properties.getProperty("h5url.prod")
String releaseydlh5url = properties.getProperty("ydlh5url.prod")
String releaseWEBurl = properties.getProperty("weburl.prod")
String releaseJavaurl = properties.getProperty("javaurl.prod")
String releaseJavatempurl = properties.getProperty("javatempurl.prod")
String releaseJavaapi = properties.getProperty("javaapi.prod")
String releaseJavacouponurl = properties.getProperty("javacouponurl.prod")

kapt {
    arguments {
        arg("AROUTER_MODULE_NAME", project.getName())
    }
}
android {
    compileSdkVersion rootProject.ext.compileSdkVersion


    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"

        multiDexEnabled true

        buildConfigField "String", "SERVER_JAVA_API_URL", "\"$javaapi\""
        buildConfigField "String", "SERVER_JAVA_URL", "\"$javaurl\""
        buildConfigField "String", "SERVER_COUPON_JAVA_URL", "\"$javacouponurl\""
        buildConfigField "String", "SERVER_TEMP_JAVA_URL", "\"$javatempurl\""
        buildConfigField "String", "SERVER_URL", "\"$serverurl\""
        buildConfigField "String", "ACTION_URL", "\"$actionurl\""
        buildConfigField "String", "H5_URL", "\"$h5url\""
        buildConfigField "String", "MH5_URL", "\"$mh5url\""
        buildConfigField "String", "YDL_H5_URL", "\"$ydlh5url\""
        buildConfigField "String", "WEB_URL", "\"$weburl\""
        buildConfigField "String", "pack", "\"$pack\""

        //测试url
        buildConfigField "String", "TEST_SERVER_URL", "\"$testServerurl\""
        buildConfigField "String", "TEST_ACTION_URL", "\"$testActionurl\""
        buildConfigField "String", "TEST_H5_URL", "\"$testH5url\""
        buildConfigField "String", "TEST_YDL_H5_URL", "\"$testydlh5url\""
        buildConfigField "String", "TEST_WEB_URL", "\"$testweburl\""
        buildConfigField "String", "TEST_SERVER_JAVA_URL", "\"$testJavaurl\""
        buildConfigField "String", "TEST_SERVER_TEMP_JAVA_URL", "\"$testJavatempurl\""
        buildConfigField "String", "TEST_SERVER_JAVA_API_URL", "\"$testJavaapi\""
        buildConfigField "String", "TEST_SERVER_COUPON_JAVA_URL", "\"$testJavacouponurl\""

        //预发布url
        buildConfigField "String", "PRE_SERVER_URL", "\"$preServerurl\""
        buildConfigField "String", "PRE_H5_URL", "\"$preH5url\""

        //线上url
        buildConfigField "String", "RELEASE_SERVER_URL", "\"$releaseServerurl\""
        buildConfigField "String", "RELEASE_ACTION_URL", "\"$releaseActionurl\""
        buildConfigField "String", "RELEASE_YDL_H5_URL", "\"$releaseydlh5url\""
        buildConfigField "String", "RELEASE_H5_URL", "\"$releaseH5url\""
        buildConfigField "String", "RELEASE_WEB_URL", "\"$releaseWEBurl\""
        buildConfigField "String", "RELEASE_SERVER_JAVA_URL", "\"$releaseJavaurl\""
        buildConfigField "String", "RELEASE_SERVER_TEMP_JAVA_URL", "\"$releaseJavatempurl\""
        buildConfigField "String", "RELEASE_SERVER_JAVA_API_URL", "\"$releaseJavaapi\""
        buildConfigField "String", "RELEASE_SERVER_COUPON_JAVA_URL", "\"$releaseJavacouponurl\""

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        flavorDimensions "versionCode"

        //解决64位手机无法加载x5 webview内核
        ndk{
            abiFilters "armeabi-v7a"
        }
    }

    lintOptions {
        abortOnError false
        disable 'MissingTranslation'
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    sourceSets {
        main {
            res.srcDirs = [
                    'src/main/res/layouts/share',
                    'src/main/res/layouts/music_player',//课程音频播放器
                    'src/main/res/layouts/confideredpacket',
                    'src/main/res/layouts/couponhint',
                    'src/main/res/layouts/redpacket',
                    'src/main/res/layouts/swipe',
                    'src/main/res/layouts',
                    'src/main/res'
            ]
            jniLibs.srcDirs = ['src/main/jniLibs']
            java.srcDirs += 'src/main/kotlin'
        }
    }

}

dependencies {
    api fileTree(dir: 'libs', include: ['*.jar','*.aar'])
    api project(':ydl-utils')

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    //noinspection GradleCompatible
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    api 'com.android.support:cardview-v7:26.1.0'
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
    api 'com.github.bumptech.glide:glide:4.3.1'
    kapt 'com.github.bumptech.glide:compiler:4.3.1'
    implementation 'com.github.bumptech.glide:annotations:4.3.1'
    api 'jp.wasabeef:glide-transformations:3.0.1'
    api 'com.android.support.constraint:constraint-layout:1.0.2'
    api 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar'
    api 'com.hannesdorfmann.mosby3:mvp:3.0.4'
    api 'com.hannesdorfmann.mosby3:mvp-nullobject-presenter:3.0.4'
    api 'in.srain.cube:cube-sdk:1.0.44@aar'
    api 'com.android.support:multidex:1.0.2'

    //视频播放器
    api 'com.dou361.ijkplayer-armv5:jjdxm-ijkplayer-armv5:1.0.0'
    api 'com.dou361.ijkplayer-arm64:jjdxm-ijkplayer-arm64:1.0.0'
    api 'com.dou361.ijkplayer-x86:jjdxm-ijkplayer-x86:1.0.0'
    api 'com.dou361.ijkplayer-x86_64:jjdxm-ijkplayer-x86_64:1.0.0'
    api('com.dou361.ijkplayer:jjdxm-ijkplayer:1.0.6') {
        exclude group: 'com.android.support', module: 'appcompat-v7'
    }

    api 'com.meituan.robust:robust:0.4.87'
    api 'com.meituan.android.walle:library:1.1.5'
//    api 'cn.jiguang.sdk:jpush:3.2.0'
//    api 'cn.jiguang.sdk:jcore:1.2.7'
    api 'de.greenrobot:eventbus:2.4.0'
    api 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34'
    api 'cn.lankton:flowlayout:3.1.0'
    api 'com.daimajia.androidanimations:library:2.3@aar'
    api 'com.umeng.sdk:common:1.5.1'
    api 'com.umeng.sdk:analytics:7.5.1'
    api 'com.umeng.sdk:share-core:6.9.1'
//    api 'com.umeng.sdk:share-qq:6.9.1.1'

    api 'com.umeng.sdk:shareboard-widget:6.9.1'
    api 'com.umeng.sdk:debug:1.0.0'
    api 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
    api 'com.facebook.stetho:stetho:1.5.0'
    api 'com.facebook.stetho:stetho-okhttp3:1.4.1'
    api 'org.aspectj:aspectjrt:1.8.9'
    //bugly sdk 其中latest.release指代最新Bugly SDK版本号，也可以指定明确的版本号，例如2.2.0
    api 'com.tencent.bugly:crashreport:2.8.6.0'
    //bugly ndk 其中latest.release指代最新Bugly NDK版本号，也可以指定明确的版本号，例如3.0
    compile 'com.tencent.bugly:nativecrashreport:3.6.0.1'
    //图片组件
    api('com.ydl:ydl-image:1.0.7-SNAPSHOT@aar') {
        transitive = true
    }
    //统计埋点
    api ('com.ydl:ydl-pushagent:0.1.0')
//    api project(':ydl-pay')
//
//    api ('com.ydl:ydl-js:1.0.7-SNAPSHOT@aar'){
//        transitive = false
//    }
    //路由组件
    api 'com.ydl:ydl-router:1.3.0-SNAPSHOT@aar'
    //xrecyclerview组件
    api 'com.ydl:xrecyclerview:1.0.0-SNAPSHOT@aar'
    compile "com.alibaba:arouter-api:$arouter_api"
    //    注意此处的依赖方式：kotlin中使用和java中使用方式有不同
    kapt "com.alibaba:arouter-compiler:$arouter_compiler"
    //    //课程模块音频播放 依赖
    //    api 'com.github.harvie1208:HMusicPlayer:1.0.4'
    implementation('com.google.android.exoplayer:exoplayer:2.9.0') {
        exclude group: 'com.android.support', module: 'support-compat'
    }
    implementation 'me.weishu:free_reflection:2.0.0'
    api 'com.ydl:imagepicker:1.0.6'
    //应用安全
    api 'com.ydl:protector:1.0.1-SNAPSHOT@aar'
    api 'com.umeng.sdk:share-wechat:6.9.1'

    api(rootProject.ext.dependencies["rxlifecycle2"]) {
        exclude module: 'rxjava'
        exclude module: 'jsr305'
    }
    api(rootProject.ext.dependencies["rxlifecycle2-android"]) {
        exclude module: 'support-annotations'
        exclude module: 'rxjava'
        exclude module: 'rxandroid'
        exclude module: 'rxlifecycle'
    }

    api project(':ydl-net')
}


final def log = project.logger
final def variants = project.android.libraryVariants

variants.all { variant ->
    if (!variant.buildType.isDebuggable()) {
        log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
        return;
    }

    JavaCompile javaCompile = variant.javaCompile
    javaCompile.doLast {
        String[] args = ["-showWeaveInfo",
                         "-1.8",
                         "-inpath", javaCompile.destinationDir.toString(),
                         "-aspectpath", javaCompile.classpath.asPath,
                         "-d", javaCompile.destinationDir.toString(),
                         "-classpath", javaCompile.classpath.asPath,
                         "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)]
        log.debug "ajc args: " + Arrays.toString(args)

        MessageHandler handler = new MessageHandler(true);
        new Main().run(args, handler);
        for (IMessage message : handler.getMessages(null, true)) {
            switch (message.getKind()) {
                case IMessage.ABORT:
                case IMessage.ERROR:
                case IMessage.FAIL:
                    log.error message.message, message.thrown
                    break;
                case IMessage.WARNING:
                    log.warn message.message, message.thrown
                    break;
                case IMessage.INFO:
                    log.info message.message, message.thrown
                    break;
                case IMessage.DEBUG:
                    log.debug message.message, message.thrown
                    break;
            }
        }
    }
}