// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
	repositories {
		jcenter()
		mavenCentral()
		google()
	}
	dependencies {
		classpath 'com.android.tools.build:gradle:3.5.0'
		// NOTE: Do not place your application dependencies here; they belong
		// in the individual module build.gradle files

		classpath 'org.ajoberstar:grgit:1.1.0'

		// https://github.com/Triple-T/gradle-play-publisher#usage
		classpath 'com.github.triplet.gradle:play-publisher:2.4.1'
	}
}

plugins {
	// obtain revision from git
	id 'org.ajoberstar.grgit' version '1.6.0'
}

allprojects {
	repositories {
		mavenLocal()
		google()
		jcenter()
	}
}

/////////////// yaxim specific build rules ///////////////
apply plugin: 'com.android.application'
apply plugin: 'com.github.triplet.play'

// gradle rule to download sources for AS (because AS can't on its own)
// run as: `gradle cleanIdea idea`
apply plugin: 'idea'
idea {
    module {
        downloadJavadoc = true
        downloadSources = true
    }
}

ext {
	build_date = "${new Date().format('yyyy-MM-dd')}"
	build_revision = "${grgit.describe()}"
	build_version = "yaxim ${build_revision} ${build_date}"
}

task buildVersion (type: Copy) {
	from("template") {
		include "version.xml"
		expand( build_date: build_date,
			build_revision: build_revision,
			build_version: build_version)
	}
	into("res/values")
	outputs.upToDateWhen {
		false
	}
}

android {
	compileSdkVersion 28
	buildToolsVersion "28.0.3"
	useLibrary 'org.apache.http.legacy'
	flavorDimensions ("style", "distribution")
	defaultConfig {
		applicationId "org.yaxim"
		minSdkVersion 14
		//noinspection ExpiredTargetSdkVersion
		targetSdkVersion 28
		versionName "$build_revision"
		vectorDrawables.useSupportLibrary = true
	}

	signingConfigs {
		release {
			storeFile file(RELEASE_STORE_FILE)
			storePassword RELEASE_STORE_PASSWORD
			keyAlias RELEASE_KEY_ALIAS
			keyPassword RELEASE_KEY_PASSWORD
		}
	}

	buildTypes {
		debug {
			minifyEnabled true
			proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
		}
		release {
			minifyEnabled true
			proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
			signingConfig signingConfigs.release
		}
	}
	productFlavors {
		yaxim {
			dimension "style"
			applicationIdSuffix "androidclient"
		}
		bruno {
			dimension "style"
			applicationIdSuffix "bruno"
		}
        play {
			dimension "distribution"
		}
	}

	sourceSets {
		main {
			manifest.srcFile 'AndroidManifest.xml'
			java.srcDirs = ['src']
			resources.srcDirs = ['src']
			aidl.srcDirs = ['src']
			renderscript.srcDirs = ['src']
			res.srcDirs = ['res']
			assets.srcDirs = ['assets']
		}
        yaxim {
            java.srcDirs = ['yaxim/src']
            res.srcDirs = ['yaxim/res']
            assets.srcDirs = ['yaxim/assets']
        }
		bruno {
			java.srcDirs = ['bruno/src']
			res.srcDirs = ['bruno/res']
			assets.srcDirs = ['bruno/assets']
		}
		play {
			manifest.srcFile 'play/AndroidManifest.xml'
			java.srcDirs = ['play/src']
			res.srcDirs = ['play/res']
			assets.srcDirs = ['play/assets']
		}
	}
	lintOptions {
		disable 'MissingTranslation'
	}
}
play {
	serviceAccountCredentials = file('play-publish-credentials.json')
	track = 'beta'
}

dependencies {
	def SMACK_VERSION = "4.3.5-SNAPSHOT"
	// Google Push via FCM
	playImplementation('com.google.firebase:firebase-messaging:17.3.4') {
		exclude group: 'com.google.firebase', module: 'firebase-core'
		exclude group: 'com.google.firebase', module: 'firebase-analytics'
		exclude group: 'com.google.firebase', module: 'firebase-measurement-connector'
	}
	implementation 'com.android.support:appcompat-v7:28.0.0'
	// implicit compile 'com.android.support:support-v4:25.+'
	implementation project('MemorizingTrustManager')
	api fileTree(include: ['*.jar'], dir: 'libs')
	api fileTree(include: ['*.aar'], dir: 'libs')
	implementation 'me.leolin:ShortcutBadger:1.1.22@aar'
	implementation 'org.hsluv:hsluv:0.2'
	implementation 'com.koushikdutta.urlimageviewhelper:urlimageviewhelper:1.0.4'
	implementation 'com.google.zxing:core:3.3.3'

	// play referrer
	implementation 'com.android.installreferrer:installreferrer:1.1'
	// smack4
	//compile project("Smack:smack-android-extensions")
	//compile project("Smack:smack-tcp")
	//compile project("Smack:smack-im")
	//// for Carbons, and other
	//compile project("Smack:smack-experimental")
	implementation 'org.jxmpp:jxmpp-jid:0.6.4-SNAPSHOT'
	implementation "org.igniterealtime.smack:smack-android-extensions:$SMACK_VERSION"
	implementation "org.igniterealtime.smack:smack-tcp:$SMACK_VERSION"
	implementation "org.igniterealtime.smack:smack-im:$SMACK_VERSION"
	// for Carbons, and other
	implementation "org.igniterealtime.smack:smack-experimental:$SMACK_VERSION"
}

// for smack4
configurations {
	all*.exclude group: 'xpp3', module: 'xpp3'
}

preBuild.dependsOn buildVersion

// required to fill applicationId into prefs XML files
afterEvaluate {
	android.applicationVariants.all { variant ->
		variant.resValue 'string', 'application_id', variant.applicationId
	}
}
