import groovy.swing.SwingBuilder
import groovy.xml.Namespace

/*
 *     Created by Angel Leon (@gubatron), Alden Torres (aldenml)
 *     Copyright (c) 2011-2025, FrostWire(R). All rights reserved.
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.chaquo.python'
apply plugin: 'org.jetbrains.kotlin.android'

buildscript {
    repositories {
        google()
        mavenCentral()
        maven { url "https://chaquo.com/maven" }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.13.0'
        classpath 'org.codehaus.groovy:groovy-all:3.0.25'

        classpath 'net.sf.proguard:proguard-gradle:6.2.2'

        classpath 'com.google.gms:google-services:4.4.3'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.6'

        classpath 'com.chaquo.python:gradle:16.1.0'

        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.21'
    }
}

repositories {
    google()
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    maven {
        url "https://dl.frostwire.com/maven"
        content {
            includeGroup "com.frostwire"
        }
    }
}

def manifestVersionCode() {
    def manifestFile = new File(project.projectDir.absolutePath + '/AndroidManifest.xml')
    def ns = new Namespace("http://schemas.android.com/apk/res/android", "android")
    def xml = new groovy.xml.XmlParser().parse(manifestFile)
    //System.out.println("manifestVersionCode: xml.attributes()[ns.versionCode].toString()=" + xml.attributes()[ns.versionCode].toString())
    return Integer.parseInt(xml.attributes()[ns.versionCode].toString())
}

def manifestVersionName() {
    def manifestFile = file(project.projectDir.absolutePath + '/AndroidManifest.xml')
    def ns = new Namespace("http://schemas.android.com/apk/res/android", "android")
    def xml = new groovy.xml.XmlParser().parse(manifestFile)
    //System.out.println("manifestVersionName: " + xml.attributes()[ns.versionName].toString())
    return xml.attributes()[ns.versionName].toString()
}

def changeApkOutput(variant) {
    def flavor = 'plus'
    def suffix = project.ext.versionName + '-b' + project.ext.versionCode + '-' + flavor
    def name = "frostwire-android-" + variant.buildType.name + '-' + suffix

    variant.outputs.all { output ->
        outputFileName = name + '.apk'
    }
    variant.getAssembleProvider().configure() {
        it.doLast {
            assemble ->
                copy {
                    from variant.outputs*.outputFile
                    into "${project.getLayout().getBuildDirectory().getAsFile().get()}"
                }
        }
    }
}

project.ext.versionCode = manifestVersionCode()
project.ext.versionName = manifestVersionName()

boolean isAssembleRelease = gradle.startParameter.taskNames.contains("assembleRelease") || gradle.startParameter.taskNames.contains("assemblePlus1Release")

android {
    namespace "com.frostwire.android"

    compileSdk = 36  // Android 15 SnowCone

    defaultConfig {
        applicationId "com.frostwire.android"
        versionName project.ext.versionName
        minSdkVersion 26 // Android 8.0 Oreo
        targetSdkVersion 34 // Android 14 (more involved upgrade, crashes on targetsdkversion 35)
        multiDexEnabled true

        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
        }

        python {
            chaquopy {
                defaultConfig {
                    version "3.11"
                }
            }
            pip {
                install "yt_dlp==2025.09.26"
                // helps a lot with debugging
                //options "--verbose"
            }
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            res.srcDirs = ['res', 'apollo/res']
            aidl.srcDirs = ['src', 'apollo/src']
            assets.srcDirs = ['assets']
            java {
                srcDir '../common/src/main/java'
                srcDir 'apollo/src'
                srcDir 'src'
                exclude 'test/**'
            }
            jniLibs.srcDirs = ['libs']
            // Ensure Chaquopy packages our Python code from src/main/python
            python {
                srcDir 'src/main/python'
            }
        }
        test {
            java.srcDirs = ['src/test/java']
        }
    }


    signingConfigs {
        release {
            if (isAssembleRelease) {
                storeFile file("../../frostwire-tools/certs/android.keystore/frostwire.keystore")
                def sigRequester = new SignatureRequester()
                sigRequester.ask()
                storePassword sigRequester.keyPassword()
                keyAlias sigRequester.keyAlias()
                keyPassword sigRequester.keyPassword()
            } else {
                storePassword ''
                keyAlias ''
                keyPassword ''
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
            signingConfig signingConfigs.release
            multiDexKeepFile file('multidex-config.txt')
        }
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
            multiDexKeepFile file('multidex-config.txt')
        }
    }

    flavorDimensions "classicApp"

    productFlavors {
        plus1 {
            // OLD PLUS CODE VERSION SUFFIX 9070000
            dimension "classicApp"
            versionCode 9090000 + project.ext.versionCode
            resValue "string", "application_label", "FrostWire Plus"
        }
    }
    lint {
        abortOnError false
    }

    applicationVariants.configureEach { variant ->
        changeApkOutput(variant)
    }

    configurations {
        configureEach {
            exclude group: "com.google.android.gms.common", module: "play-services-measurement"
            exclude group: 'com.google.android.gms.common', module: 'play-services-base'
        }
    }
    buildFeatures {
        aidl true
        buildConfig true
    }
}

dependencies {
    // @Nullable cross-os annotation
    implementation 'org.jetbrains:annotations:26.0.2-1'

    implementation 'androidx.appcompat:appcompat:1.7.1'
    implementation 'androidx.browser:browser:1.9.0'
    implementation 'androidx.core:core:1.17.0'
    implementation 'androidx.exifinterface:exifinterface:1.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.9.4'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.preference:preference:1.2.1'
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
    implementation 'androidx.vectordrawable:vectordrawable-animated:1.2.0'
    implementation 'com.google.android.ump:user-messaging-platform:3.2.0'
    implementation 'com.google.android.flexbox:flexbox:3.0.0'
    implementation 'com.google.android.material:material:1.13.0'
    implementation 'com.google.code.gson:gson:2.13.2'
    implementation 'com.google.re2j:re2j:1.8'
    implementation 'com.squareup.okhttp3:okhttp:5.1.0'
    implementation 'com.squareup.picasso3:picasso:3.0.0-alpha06'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.2.0'
    implementation "androidx.work:work-runtime-ktx:2.10.4"

    // Firebase Crashlytics
    implementation platform('com.google.firebase:firebase-bom:34.3.0')
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-analytics'

    implementation 'androidx.media2:media2-session:1.3.0'
    implementation 'androidx.media2:media2-widget:1.3.0'
    implementation 'androidx.media2:media2-player:1.3.0'

    def jlibtorrent_version = '2.0.12.5'
    implementation 'com.frostwire:jlibtorrent:' + jlibtorrent_version
    implementation 'com.frostwire:jlibtorrent-android-arm:' + jlibtorrent_version
    implementation 'com.frostwire:jlibtorrent-android-arm64:' + jlibtorrent_version
    implementation 'com.frostwire:jlibtorrent-android-x86:' + jlibtorrent_version
    implementation 'com.frostwire:jlibtorrent-android-x86_64:' + jlibtorrent_version

    testImplementation 'junit:junit:4.13.2'
}

afterEvaluate {
    tasks.named('uploadCrashlyticsMappingFilePlus1Release').configure {
        dependsOn ':processPlus1ReleaseGoogleServices'
    }
}

class SignatureRequester {
    String myKeyAlias = ""
    String myKeyPassword = ""

    void ask() {
        System.setProperty('java.awt.headless', 'false')
        boolean useSwingDialog = true  // Force use of Swing dialog

        System.out.println("System.console()=" + System.console())

        if (useSwingDialog || System.console() == null) {
            // Force the Swing dialog to be used by always setting useSwingDialog to true
            new SwingBuilder().edt {
                dialog(modal: true,
                        title: 'Enter keystore data',
                        alwaysOnTop: true,
                        resizable: true,
                        locationRelativeTo: null,
                        show: true,
                        size: [215, 147]
                ) {
                    vbox {
                        label(text: "Key Alias:")
                        inputAlias = textField()
                        label(text: "Key Password:")
                        inputPassword = passwordField()
                        button(defaultButton: true, text: 'OK', actionPerformed: {
                            this.myKeyAlias = inputAlias.text
                            this.myKeyPassword = new String(inputPassword.password)
                            dispose()
                        })
                    }
                }
            }
        } else {
            this.myKeyAlias = new String(System.console().readPassword("\nKey Alias: "))
            this.myKeyPassword = new String(System.console().readPassword("\nKey Password: "))
        }
    }

    String keyAlias() {
        this.myKeyAlias
    }

    String keyPassword() {
        this.myKeyPassword
    }
}
