import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    compileSdkVersion project.COMPILE_SDK_VERSION as int
    buildToolsVersion project.BUILD_TOOLS_VERSION

    defaultConfig {
        applicationId project.APPCALITION_ID
        minSdkVersion project.MIN_SDK_VERSION as int
        targetSdkVersion project.TARGET_SDK_VERSION as int
        versionCode project.VERSION_CODE as int
        versionName project.VERSION_NAME

        externalNativeBuild {
            ndkBuild {
                if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
                    // skip the NDK Build step if PROP_NDK_MODE is none
                    targets 'cocos2djs'
                    arguments 'NDK_TOOLCHAIN_VERSION=4.9'
                    arguments 'APP_PLATFORM=android-' + project.TARGET_SDK_VERSION

                    def module_paths = [project.file("../../../cocos2d-x"),
                                        project.file("../../../cocos2d-x/cocos"),
                                        project.file("../../../cocos2d-x/external")]
                    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                        arguments 'NDK_MODULE_PATH=' + module_paths.join(";")
                    } else {
                        arguments 'NDK_MODULE_PATH=' + module_paths.join(':')
                    }

                    arguments '-j' + Runtime.runtime.availableProcessors()
                    abiFilters.addAll(project.APP_ABI.split(':').collect { it as String })
                }
            }
        }

        resValue("string", "loadModules", project.LOAD_MODULES)

        resValue("integer", "channelId", project.CHANNEL_ID)
        resValue("integer", "regId", project.REG_ID)
        resValue("integer", "payId", project.PAY_ID)
        resValue("bool", "is3rdLogin", project.IS_THIRD_LOGIN)
        resValue("bool", "is3rdPay", project.IS_THIRD_PAY)
        resValue("bool", "is3rdExit", project.IS_THIRD_EXIT)
        resValue("bool", "is3rdLogout", project.IS_THIRD_LOGOUT)

        resValue("string", "iapppayAppId", project.IAPPPAY_APP_ID)
        resValue("string", "iapppayAppvKey", project.IAPPPAY_APPV_KEY)

        if (project.LOAD_MODULES.contains("YunDun")) {
            resValue("string", "yundunAppKey", project.YUNDUN_APP_KEY)
            resValue("string", "yundunGroupId", project.YUNDUN_GROUP_ID)
        }
    }

    sourceSets.main {
        java.srcDir "src"
        res.srcDir "res"
        jniLibs.srcDir "libs"
        manifest.srcFile "AndroidManifest.xml"
    }

    externalNativeBuild {
        ndkBuild {
            if (!project.hasProperty("PROP_NDK_MODE") || PROP_NDK_MODE.compareTo('none') != 0) {
                // skip the NDK Build step if PROP_NDK_MODE is none
                path "jni/Android.mk"
            }
        }
    }

    signingConfigs {
        release {
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                storeFile file(RELEASE_STORE_FILE)
                storePassword RELEASE_STORE_PASSWORD
                keyAlias RELEASE_KEY_ALIAS
                keyPassword RELEASE_KEY_PASSWORD
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            if (project.hasProperty("RELEASE_STORE_FILE")) {
                signingConfig signingConfigs.release
            }

            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=0'
                }
            }
        }

        debug {
            externalNativeBuild {
                ndkBuild {
                    arguments 'NDK_DEBUG=1'
                }
            }
        }
    }

    productFlavors {
        common {

        }

        weixin {
            applicationId "com.phoneu.xxxxx"
            resValue("string", "weixinAppId", project.WEIXIN_APP_ID)
            resValue("string", "weixinAppSecret", project.WEIXIN_APP_SECRET)
        }
    }
}

android.applicationVariants.all { variant ->
    // delete previous files first
    delete "${buildDir}/intermediates/assets/${variant.dirName}"

    variant.mergeAssets.doLast {
        copy {
            from "${buildDir}/../../../../../res"
            into "${buildDir}/intermediates/assets/${variant.dirName}/res"
        }

        copy {
            from "${buildDir}/../../../../../src"
            into "${buildDir}/intermediates/assets/${variant.dirName}/src"
        }

        copy {
            from "${buildDir}/../../../../../main.js"
            from "${buildDir}/../../../../../project.json"
            into "${buildDir}/intermediates/assets/${variant.dirName}"
        }

        copy {
            from "../../../cocos2d-x/cocos/scripting/js-bindings/script"
            into "${buildDir}/intermediates/assets/${variant.dirName}/script"
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation project(':libcocos2dx')

    // 第三方库
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.8'
    implementation 'com.alibaba:fastjson:1.2.54'

    // 丰游SDK库
    implementation 'com.phoneu:module-iapppay:0.0.9@aar'
    implementation 'com.phoneu:phoneu-sdk:0.9.51@aar'
    implementation 'com.phoneu:phoneu-proxy-cocos:0.0.35 @aar'
    commonImplementation 'com.phoneu:sdk-common:0.2.10@aar'
    weixinImplementation 'com.phoneu:sdk-common-with-weixin:0.1.4@aar'

    if (project.LOAD_MODULES.contains("YunDun")) {
        implementation 'com.phoneu:module-yundun:0.0.4@aar'
    }
}
