// Copyright 2018 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
//
//      http://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.

import org.gradle.util.ConfigureUtil;

// A map of library to the dependencies that need to be added for it.
def firebaseDependenciesMap = [
  'app' : ['com.google.firebase:firebase-analytics'],
  'app_check' : ['com.google.firebase:firebase-appcheck',
                 'com.google.firebase:firebase-appcheck-debug',
                 'com.google.firebase:firebase-appcheck-playintegrity'],
  'play_services' : ['com.google.android.gms:play-services-base:18.5.0'],
  'analytics' : ['com.google.firebase:firebase-analytics'],
  'auth' : ['com.google.firebase:firebase-auth'],
  'database' : ['com.google.firebase:firebase-database'],
  'dynamic_links' : ['com.google.firebase:firebase-dynamic-links'],
  'firestore' : ['com.google.firebase:firebase-firestore'],
  'functions' : ['com.google.firebase:firebase-functions'],
  'gma' : ['com.google.android.gms:play-services-ads:23.0.0',
           'com.google.android.ump:user-messaging-platform:2.2.0'],
  'installations' : ['com.google.firebase:firebase-installations'],
  'invites' : ['com.google.firebase:firebase-invites'],
  // Messaging has an additional local dependency to include.
  'messaging' : ['com.google.firebase:firebase-messaging',
                 'com.google.firebase.messaging.cpp:firebase_messaging_cpp@aar',
                 'androidx.core:core:1.6.0-alpha03',
                 'com.google.flatbuffers:flatbuffers-java:1.12.0'],
  'performance' : ['com.google.firebase:firebase-perf'],
  'remote_config' : ['com.google.firebase:firebase-config'],
  'storage' : ['com.google.firebase:firebase-storage'],
  'testlab' : []
]

// Handles adding the Firebase C++ dependencies as properties.
class Dependencies {
  HashSet<String> libSet = new HashSet<String>()

  def getApp() {
    libSet.add('app')
    libSet.add('play_services')
  }
  def getAppWithoutPlayServices() {
    libSet.add('app')
  }
  def getAnalytics() {
    libSet.add('analytics')
  }
  def getAppCheck() {
    libSet.add('app_check')
  }
  def getAuth() {
    libSet.add('auth')
  }
  def getDatabase() {
    libSet.add('database')
  }
  def getDynamicLinks() {
    libSet.add('dynamic_links')
  }
  def getFirestore() {
    libSet.add('firestore')
  }
  def getFunctions() {
    libSet.add('functions')
  }
  def getGma() {
    libSet.add('gma')
  }
  def getInstallations() {
    libSet.add('installations')
  }
  def getInvites() {
    libSet.add('invites')
  }
  def getMessaging() {
    libSet.add('messaging')
  }
  def getPerformance() {
    libSet.add('performance')
  }
  def getRemoteConfig() {
    libSet.add('remote_config')
  }
  def getStorage() {
    libSet.add('storage')
  }
  def getTestlab() {
    libSet.add('testlab')
  }
}

// Extension to handle which Firebase C++ dependencies are being added to the
// gradle project.
// They are added via: firebaseCpp.dependencies "auth", "analytics", ...
class FirebaseCppExtension {
  Dependencies dependencies = new Dependencies()

  void dependencies(Closure c) {
    ConfigureUtil.configure(c, dependencies)
  }
  void dependencies(Action<? super Dependencies> action) {
    action.execute(dependencies)
  }
}

project.extensions.create('firebaseCpp', FirebaseCppExtension)

// Adds the proguard file for the given library.
def addProguard(String lib) {
  project.android.buildTypes.release.proguardFile file(
    "${gradle.firebase_cpp_sdk_dir}/libs/android/${lib}.pro")
}

project.afterEvaluate {
  if (!firebaseCpp.dependencies.libSet.isEmpty()) {
    // App is required, so add it if it wasn't included.
    if (!firebaseCpp.dependencies.libSet.contains('app')) {
      firebaseCpp.dependencies.libSet.add('app')
      firebaseCpp.dependencies.libSet.add('play_services')
    }

      // Add the bill-of-materials
    project.dependencies {
      implementation platform('com.google.firebase:firebase-bom:33.1.2')
    }
    for (String lib : firebaseCpp.dependencies.libSet) {
      // Messaging includes an aar, which to be depended on properly requires
      // the folder containing it to be added this way.
      if (lib == 'messaging') {
        repositories {
          flatDir {
            dirs gradle.firebase_cpp_sdk_dir + "/libs/android"
          }
        }
      }

      // Add the proguard file and dependencies for the library
      addProguard(lib)
      for (String dep : firebaseDependenciesMap.get(lib)) {
        project.dependencies {
          implementation "$dep"
        }
      }
    }
  }
}
