plugins {
    id 'com.android.application'
    id 'com.google.protobuf'
}

def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

def appName() {
    return "WebRTCControlled"
}

android {
    namespace 'com.ttstd.controlled'
    compileSdk 34

    defaultConfig {
        applicationId "com.ttstd.controlled"
        minSdk 24
        targetSdk 34
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildFeatures {
        dataBinding true
        buildConfig true
        aidl true
    }

    sourceSets {
        zhanRuiDebug.manifest.srcFile 'src/system/AndroidManifest.xml'
        zhanRuiRelease.manifest.srcFile 'src/system/AndroidManifest.xml'
        CrosshatchDebug.manifest.srcFile 'src/system/AndroidManifest.xml'
        CrosshatchRelease.manifest.srcFile 'src/system/AndroidManifest.xml'
    }

    signingConfigs {
        keypub {
            storeFile file(rootProject.ext.signingConfigs.keypub.storeFile)
            storePassword rootProject.ext.signingConfigs.keypub.storePassword
            keyAlias rootProject.ext.signingConfigs.keypub.keyAlias
            keyPassword rootProject.ext.signingConfigs.keypub.keyPassword
        }

        crosshatch {
            storeFile file(rootProject.ext.signingConfigs.crosshatch.storeFile)
            storePassword rootProject.ext.signingConfigs.crosshatch.storePassword
            keyAlias rootProject.ext.signingConfigs.crosshatch.keyAlias
            keyPassword rootProject.ext.signingConfigs.crosshatch.keyPassword
        }

        zhanxun {
            storeFile file(rootProject.ext.signingConfigs.zhanxun.storeFile)
            storePassword rootProject.ext.signingConfigs.zhanxun.storePassword
            keyAlias rootProject.ext.signingConfigs.zhanxun.keyAlias
            keyPassword rootProject.ext.signingConfigs.zhanxun.keyPassword
        }
    }

    buildTypes {
        zhanRuiDebug.initWith(debug)
        zhanRuiDebug {
            debuggable true
            signingConfig signingConfigs.zhanxun
        }

        zhanRuiRelease.initWith(release)
        zhanRuiRelease {
            signingConfig signingConfigs.zhanxun
        }

        CrosshatchDebug.initWith(debug)
        CrosshatchDebug {
            debuggable true
            signingConfig signingConfigs.crosshatch
        }

        CrosshatchRelease.initWith(release)
        CrosshatchRelease {
            signingConfig signingConfigs.crosshatch
        }

        debug {
            versionNameSuffix "_debug"
            debuggable true
            minifyEnabled false
            //Zipalign优化
            zipAlignEnabled true
            signingConfig signingConfigs.keypub
        }

        release {
            minifyEnabled false
            //Zipalign优化
            zipAlignEnabled true
            //前一部分代表系统默认的android程序的混淆文件，该文件已经包含了基本的混淆声明，后一个文件是自己的定义混淆文件
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            //签名
            signingConfig signingConfigs.keypub
        }
    }

    // Add application variant configuration here instead
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def buildType = variant.buildType.name
            def fileName = ""

            if (buildType.contains("debug")) {
                fileName = "${appName()}_V${defaultConfig.versionName}_${releaseTime()}.apk"
            } else {
                fileName = "${appName()}_${variant.versionCode}_V${variant.versionName}_${releaseTime()}_${buildType}.apk"
            }

            output.outputFileName = fileName
        }
    }

}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.25.1'
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {}
            }
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.11.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    // WebRTC
    implementation 'io.github.webrtc-sdk:android:144.7559.09'
//    implementation 'org.webrtc:google-webrtc:1.0.32006'
    // OkHttp for WebSocket
    implementation 'com.squareup.okhttp3:okhttp:4.12.0'
    // Gson for JSON（信令消息仍使用 JSON）
    implementation 'com.google.code.gson:gson:2.10.1'
    // Protobuf（DataChannel 控制指令二进制）
    implementation 'com.google.protobuf:protobuf-java:3.25.1'
}
