/*
 * Copyright (c) 2021 Ubique Innovation AG <https://www.ubique.ch>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * SPDX-License-Identifier: MPL-2.0
 */

plugins {
	id 'com.android.application'
	id 'kotlin-android'
	id 'com.google.devtools.ksp' version '1.6.10-1.0.4'
	id 'org.sonarqube' version '3.0'
	id 'ch.ubique.gradle.ubdiag'
}

ext.readProperty = { paramName -> readPropertyWithDefault(paramName, null) }
ext.readPropertyWithDefault = { paramName, defaultValue ->
	if (project.hasProperty(paramName)) {
		return project.getProperties().get(paramName)
	} else {
		Properties properties = new Properties()
		if (project.rootProject.file('local.properties').exists()) {
			properties.load(project.rootProject.file('local.properties').newDataInputStream())
		}
		if (properties.getProperty(paramName) != null) {
			return properties.getProperty(paramName)
		} else {
			return defaultValue
		}
	}
}

android {
	compileSdkVersion 31

	defaultConfig {
		applicationId "ch.admin.bag.covidcertificate.wallet"
		minSdkVersion readPropertyWithDefault('minSdkVersion', '23').toInteger()
		targetSdkVersion 31
		versionCode 4_10_00_00
		versionName "4.10.0"

		// Stops the Gradle plugin’s automatic rasterization of vectors
		generatedDensities = []

		buildConfigField "long", "BUILD_TIME", readPropertyWithDefault('buildTimestamp', System.currentTimeMillis()) + 'L'

		testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
		// The following argument makes the Android Test Orchestrator run its
		// "pm clear" command after each test invocation. This command ensures
		// that the app's state is completely cleared between tests.
		testInstrumentationRunnerArguments clearPackageData: 'true'
		testOptions {
			animationsDisabled = true
			execution 'ANDROIDX_TEST_ORCHESTRATOR'
		}
	}

	productFlavors {
		// These app tokens are reserved for the official COVID Certificate app.
		// If you intend to integrate the CovidCertificate-SDK into your app, please get in touch with BIT/BAG to get a token assigned.
		dev {
			buildConfigField "String", "BASE_URL", '"https://www.cc-d.bit.admin.ch/app/wallet/v1/"'
			buildConfigField "String", "BASE_URL_DELIVERY", '"https://covidcertificate-app-d.bit.admin.ch/app/delivery/v1/"'
			buildConfigField "String", "BASE_URL_TRANSFORMATION", '"https://covidcertificate-app-d.bit.admin.ch/app/transform/v1/"'
			buildConfigField "String", "SDK_APP_TOKEN", '"c838a4c4-39e5-4bbb-8e75-e4382df2edfe"'
			applicationIdSuffix '.dev'
		}
		abn {
			buildConfigField "String", "BASE_URL", '"https://www.cc-a.bit.admin.ch/app/wallet/v1/"'
			buildConfigField "String", "BASE_URL_DELIVERY", '"https://covidcertificate-app-a.bit.admin.ch/app/delivery/v1/"'
			buildConfigField "String", "BASE_URL_TRANSFORMATION", '"https://covidcertificate-app-a.bit.admin.ch/app/transform/v1/"'
			buildConfigField "String", "SDK_APP_TOKEN", '"e9802c49-4f2b-49cc-a645-24c206366455"'
			applicationIdSuffix '.abn'
		}
		prod {
			buildConfigField "String", "BASE_URL", '"https://www.cc.bit.admin.ch/app/wallet/v1/"'
			buildConfigField "String", "BASE_URL_DELIVERY", '"https://covidcertificate-app.bit.admin.ch/app/delivery/v1/"'
			buildConfigField "String", "BASE_URL_TRANSFORMATION", '"https://covidcertificate-app.bit.admin.ch/app/transform/v1/"'
			buildConfigField "String", "SDK_APP_TOKEN", '"0795dc8b-d8d0-4313-abf2-510b12d50939"'
		}
	}

	flavorDimensions "version"

	signingConfigs {
		release {
			storeFile file(readPropertyWithDefault('keystoreFile', 'testKeystore'))
			storePassword readProperty('keystorePassword')
			keyAlias readPropertyWithDefault('keyAlias', 'keyAlias')
			keyPassword readProperty('keyAliasPassword')
		}
	}

	buildTypes {
		release {
			minifyEnabled false
			proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
			signingConfig signingConfigs.release
		}
		debug {
			minifyEnabled false
			proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
			signingConfig signingConfigs.release
		}
	}

	compileOptions {
		coreLibraryDesugaringEnabled true

		sourceCompatibility JavaVersion.VERSION_1_8
		targetCompatibility JavaVersion.VERSION_1_8
	}

	kotlinOptions {
		jvmTarget = '1.8'
	}

	buildFeatures {
		viewBinding true
	}
}

sonarqube {
	properties {
		property "sonar.projectName", "COVID Certificate App Android"
		property "sonar.projectKey", "covidcertificate-android"
		property "sonar.organization", "admin-ch"
		property "sonar.host.url", "https://sonarcloud.io"
	}
}

dependencies {
	androidTestImplementation 'androidx.test:rules:1.4.1-alpha03'
	androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.0-alpha03'
	coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

	implementation 'com.google.android.material:material:1.5.0'
	implementation project(':common')

	implementation 'androidx.core:core-ktx:1.7.0'
	implementation 'androidx.appcompat:appcompat:1.4.1'
	implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
	implementation 'androidx.fragment:fragment-ktx:1.4.0'
	implementation 'androidx.lifecycle:lifecycle-process:2.4.0'
	implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
	implementation 'androidx.viewpager2:viewpager2:1.0.0'
	implementation 'androidx.security:security-crypto:1.0.0'
	implementation 'androidx.work:work-runtime-ktx:2.7.1'

	implementation 'com.squareup.retrofit2:retrofit:2.9.0'
	implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
	implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
	implementation 'com.squareup.moshi:moshi:1.13.0'
	ksp 'com.squareup.moshi:moshi-kotlin-codegen:1.13.0'

	testImplementation 'junit:junit:4.13.2'
	androidTestImplementation 'androidx.test.ext:junit:1.1.3'
	androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
	androidTestImplementation 'androidx.test:runner:1.4.0'
	androidTestUtil 'androidx.test:orchestrator:1.4.1'
}