repositories {
    mavenCentral()
    google()
    mavenCentral()
    flatDir {
      dirs '../../libs'
    }
    //Add Maven repo for the HMS Push Kit support
    maven {url 'https://developer.huawei.com/repo/'}
}

apply plugin: 'com.android.application'

android {
    namespace 'com.sinch.android.rtc.sample.hmspush'
    compileSdkVersion 33

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

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

    lintOptions {
        abortOnError false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

}

preBuild.doFirst {
    String[] path = project.buildscript.sourceFile.getParent().split("/")
    String taskName = path[path.size()-1]
    if (taskName.contains("-push") && !file("./agconnect-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 Huawei Mobile Services (HMS) Push Kit for delivering push notifications.'
    System.err.println 'Android Studio HMS plugin requires \'agconnect-services.json\' with correspondent \'package_name\' to be in gradle :app folder (e.g. in ./sinch-rtc-sample-hmspush/)'
    System.err.println 'Follow the HMS Push Kit manual here \'https://developer.huawei.com/consumer/en/doc/HMSCore-Guides-V5/android-config-agc-0000001050170137-V5\' to get one.'
    System.err.println 'IMPORTANT: to make HMS Push work, you have to sign the application, and provide signature fingeprint and package name in the Huawei AppGallery Connect Console.'
    System.err.println 'Also, on the first run, the application will ask to install HMS core if its not yet installed, agree, or install manually to make HMS Push work.'
    System.err.println 'For details about Sinch Managed push notifications look here \'https://developers.sinch.com/docs/voice-android-cloud-push-notifications\'.'
    throw new GradleException('agconnect-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(name:'sinch-android-rtc', version:'+', ext:'aar')
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.google.android.material:material:1.7.0'

    // HMSpush kit
    implementation 'com.huawei.hms:push:6.13.0.301'
}

apply plugin: 'com.huawei.agconnect'
