// Copyright 2019 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.

buildscript {
  repositories {
    google()
    mavenCentral()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:7.4.2'
    classpath 'com.google.gms:google-services:4.4.1'
    // This uses Flatbuffers at 1.9 because the 1.10 version added a feature
    // that requires using a newer version of the JDK and at least Android N.
    // This has already been fixed at head, but a tagged release is not yet
    // available with that fix.
    classpath 'com.google.flatbuffers:flatbuffers-java:1.12.0'
  }
}
allprojects {
  repositories {
    google()
    mavenCentral()
  }
}

apply plugin: 'com.android.library'

android {
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
  }
  compileSdkVersion 34
  buildToolsVersion '32.0.0'

  defaultConfig {
    targetSdkVersion 34
    minSdkVersion 23
  }

  sourceSets {
    main {
      manifest.srcFile '../src/android/java/LibraryManifest.xml'
      java {
        srcDirs = ['../src/android/java/com/google/firebase/messaging',
                   "$buildDir/flatbuffer_generated"]
      }
    }
  }
}

dependencies {
  implementation platform('com.google.firebase:firebase-bom:34.2.0')
  implementation 'com.google.firebase:firebase-analytics'
  implementation 'com.google.firebase:firebase-messaging'
  implementation 'com.google.flatbuffers:flatbuffers-java:1.12.0'
}

afterEvaluate {
  generateReleaseBuildConfig.enabled = false

  // Define a Task that will add a Flatbuffers dependency, downloading
  // the source from GitHub if needed.
  task generateFlatbufferFiles {
    // Locate or download flatbuffers.
    def flatbuffersDir = "$buildDir/flatbuffers"
    def flatbuffersFolder = new File(flatbuffersDir)
    if (!flatbuffersFolder.exists()) {
      exec {
        executable 'git'
        args 'clone',
             '--branch',
             'v1.12.0',
             '--depth',
             '1',
             'https://github.com/google/flatbuffers.git',
             flatbuffersDir
      }
      exec {
        executable 'git'
        args 'apply',
             '../../scripts/git/patches/flatbuffers/0001-remove-unused-var.patch',
             '--verbose',
             '--directory',
             'messaging/messaging_java/build/flatbuffers'
        }
    }

    // Locate or build flatc.
    String flatcDirPath = "$flatbuffersDir/flatc_build"
    def flatcDir = new File(flatcDirPath)
    flatcDir.mkdir()

    String flatcExecDirPath = flatcDirPath
    String flatcFilename = "flatc"
    if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
      flatcFilename += ".exe"
      flatcExecDirPath = "$flatcDirPath/Debug"
    }

    def flatcExecDir = new File(flatcExecDirPath)

    def flatcExec = new File(flatcExecDir, flatcFilename)
    if (!flatcExec.exists()) {
      exec {
        executable 'cmake'
        args '..'
        workingDir "${flatcDir.getPath()}"
      }
      exec {
        executable 'cmake'
        args '--build',
             '.',
             '--target',
             'flatc'
        workingDir "${flatcDir.getPath()}"
      }
    }

    // Generate the java files from the schema files.
    def schemaFile = new File("../src/android/schemas/messaging.fbs")
    exec {
      executable "${flatcExec.getPath()}"
      args '--java',
           '-o',
           "$buildDir/flatbuffer_generated",
           "${schemaFile.getPath()}"
    }
  }
  preBuild.dependsOn generateFlatbufferFiles
}
