buildscript {
    repositories {
        google()
        mavenCentral()
        maven {
            url "https://maven.google.com"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:9.0.0'
        classpath 'com.google.gms:google-services:4.3.10'
    }
}
apply plugin: 'com.android.application'

repositories {
    mavenCentral()
    google()
    mavenCentral()
    flatDir {
      dirs '../../libs'
        dirs '../../build/outputs/aar'
    }
}

android {
    namespace 'com.sinch.android.rtc.sample.video.push'
    compileSdkVersion 33

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 33
        applicationId 'com.sinch.android.rtc.sample.video.push'
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
            proguardFile 'proguard-project.txt'
        }
    }

    lintOptions {
        abortOnError false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

preBuild.doFirst {
    String[] path = project.buildscript.sourceFile.getParent().split("/")
    String taskName = path[path.size()-1]
    if (taskName.contains("-push") && !file("./google-services.json").exists()) {
        outputFcmBuildError()
    }
    checkCredentialsFilled()
}

def checkCredentialsFilled() {
    def sinchServiceFile
    file("./src/main/java/com/sinch/android/rtc/sample").eachFileRecurse {
        if(it.name.contains("SinchService")) {
            sinchServiceFile = it
        }
    }
    if(sinchServiceFile != null &&
            sinchServiceFile.exists() &&
            (sinchServiceFile.text.contains("enter-application-key") ||
                    sinchServiceFile.text.contains("enter-application-secret"))) {
        outputNoCredentialsError(sinchServiceFile)
    }
}

def outputFcmBuildError() {
    System.err.println 'You are building one of Sinch sample projects that uses Firebase Cloud Messaging for delivering push notifications.'
    System.err.println 'Android Studio FCM plugin requires \'google-services.json\' with correspondent \'package_name\' to be in gradle :app folder (e.g. in ./sinch-rtc-sample-push/)'
    System.err.println 'Follow the Firebase Cloud Messaging manual here \'https://firebase.google.com/docs/android/setup\' to get one.'
    System.err.println 'For details about Sinch Managed push notifications look here \'https://www.sinch.com/docs/voice/android/#pushnotifications\'.'
    throw new GradleException('google-services.json NOT FOUND!')
}

def outputNoCredentialsError(def fileName) {
    System.err.println "Sinch Sample Applications require your Sinch Credentials to be filled in $fileName."
    System.err.println "Visit dashboard.sinch.com, Voice & Video Apps, to get one."
    System.err.println "Check important comments in SinchService.java regarding safer ways of authorising your application against Sinch backend."
    throw new GradleException("Application credentials missing!")
}

dependencies {
    implementation platform('com.google.firebase:firebase-bom:29.3.0')
    implementation(name:'sinch-android-rtc', version:'+', ext:'aar')
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.android.material:material:1.7.0'
}

apply plugin: 'com.google.gms.google-services'
