/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// This module gathers a collection of configs/.json and generates
// build flavors + resources for each for EN Express.

apply from: 'enexpress_play_store.gradle'
apply from: 'enexpress_play_consent.gradle'
apply from: 'enexpress_resources.gradle'
apply from: 'enexpress_config.gradle'

Map configData = loadConfigFiles()
project.ext.excludedTasks = []

// Generate a flavor for each config file.
android {
  def v2Name = "v2"
  def v3Name = "v3"
  flavorDimensions "ha", "type"
  productFlavors {
    configData.each { name, config ->
      "$name" {
        dimension "ha"
        applicationId config["healthAuthorityID"]
        manifestPlaceholders = [appLinkHost: config["appLinkHost"]]
        buildConfigField("String", "APP_LINK_HOST",
                "\"" + config["appLinkHost"] + "\"")
        buildConfigField("boolean", "PRIVATE_ANALYTICS_SUPPORTED",
                String.valueOf(config["enableENPA"]))
        buildConfigField("String", "REGION",
                "\"" + config["regionIdentifier"] + "\"")
        buildConfigField("boolean", "QUICK_RESTORE_NOTIFICATION_SUPPORTED",
                String.valueOf(!config["enableQuickRestoreNotification"] ? false :
                        config["enableQuickRestoreNotification"]))
        if (!config["enableENPA"]) {
          println "Disabling google-services plugin tasks for " + name
          project.excludedTasks += [
              'process' + name.capitalize() + 'V2ReleaseGoogleServices',
              'process' + name.capitalize() + 'V2DebugGoogleServices',
              'process' + name.capitalize() + 'V3ReleaseGoogleServices',
              'process' + name.capitalize() + 'V3DebugGoogleServices',
              ]
        }
        buildConfigField("String", "TYPE_V2", "\"$v2Name\"")
        buildConfigField("String", "TYPE_V3", "\"$v3Name\"")
      }
    }
    "$v2Name" {
      dimension "type"
    }
    "$v3Name" {
      dimension "type"
      applicationIdSuffix ".v3"
      versionNameSuffix "-v3"
    }
  }

  applicationVariants.all { variant ->
    // Check v3 flavour
    if (variant.flavorName.endsWith(v3Name.capitalize())) {
      // Extract the HA name
      def ha = variant.flavorName.substring(0, variant.flavorName.size() - v3Name.size())
      // Override the applicationId if the override config field exists
      if (configData.get(ha).containsKey("v3ApplicationIdOverride")) {
        variant.mergedFlavor.setApplicationId(configData.get(ha).get("v3ApplicationIdOverride"))
      }
    }
  }
}

generateResources(configData)
generatePlayStorePublish(configData)
generatePlayStoreConsent(configData)
