# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:android)

platform :android do

  desc "Runs tests"
  lane :check do
    gradle(tasks: ["check","koverXmlReportDebug"])
  end

  desc "Apply build version information"
  fastlane_require "time"
  lane :setBuildVersionInfo do |options|

    # Read-in app build config file.
    buildConfigPath = "../app/build.gradle.kts"
    buildConfigFile = File.open(buildConfigPath)
    buildConfigText = buildConfigFile.read
    buildConfigFile.close

    if options[:versionName].nil? or options[:versionName].to_s.empty?
        # Use the latest version name in Firebase as the default version name.
        latestRelease = firebase_app_distribution_get_latest_release(
          app: "1:867301491091:android:50b626dba42a361651e866",
          service_credentials_file:options[:serviceCredentialsFile]
        )
        nextVersionName = latestRelease[:displayVersion]
    else
        nextVersionName = options[:versionName].to_s
    end

    # Replace version information.
    currentVersionCode = buildConfigText.match(/versionCode = (\d+)/).captures[0]
    currentVersionName = buildConfigText.match(/versionName = "(.+)"/).captures[0]
    puts "Setting version code to #{options[:versionCode]}."
    buildConfigText.gsub!("versionCode = #{currentVersionCode}", "versionCode = #{options[:versionCode]}")
    puts "Setting version name to #{nextVersionName}."
    buildConfigText.gsub!("versionName = \"#{currentVersionName}\"", "versionName = \"#{nextVersionName}\"")

    # Save changes
    File.open(buildConfigPath, "w") { |buildConfigFile| buildConfigFile << buildConfigText }
  end

  desc "Assemble debug variants"
  lane :buildDebug do
    gradle(
      task: "assemble",
      build_type: "Debug",
      print_command: false,
    )
  end

  desc "Assemble and sign release APK"
  lane :buildRelease do |options|
    gradle(
      task: "assemble",
      build_type: "Release",
      properties: {
        "android.injected.signing.store.file" => options[:storeFile],
        "android.injected.signing.store.password" => options[:storePassword],
        "android.injected.signing.key.alias" => options[:keyAlias],
        "android.injected.signing.key.password" => options[:keyPassword]
      },
      print_command: false,
    )
  end

  desc "Bundle and sign release AAB"
  lane :bundleRelease do |options|
    gradle(
        task: "bundle",
        build_type: "Release",
        properties: {
          "android.injected.signing.store.file" => options[:storeFile],
          "android.injected.signing.store.password" => options[:storePassword],
          "android.injected.signing.key.alias" => options[:keyAlias],
          "android.injected.signing.key.password" => options[:keyPassword]
        },
        print_command: false,
    )
  end

  desc "Publish release to Firebase"
  lane :distributeReleaseToFirebase do |options|
    release_notes = changelog_from_git_commits(
      commits_count: 1,
      pretty: "- %s"
    )

    puts "Release notes #{release_notes}"

    firebase_app_distribution(
      app: "1:867301491091:android:50b626dba42a361651e866",
      android_artifact_type: "APK",
      android_artifact_path: "app/build/outputs/apk/release/com.bitwarden.authenticator-release.apk",
      service_credentials_file: options[:serviceCredentialsFile],
      groups: "internal-prod-group",
      release_notes: release_notes,
    )
  end

    desc "Publish release AAB to Firebase"
    lane :distributeReleaseBundleToFirebase do |options|
      release_notes = changelog_from_git_commits(
        commits_count: 1,
        pretty: "- %s"
      )

      puts "Release notes #{release_notes}"

      firebase_app_distribution(
        app: "1:867301491091:android:50b626dba42a361651e866",
        android_artifact_type: "AAB",
        android_artifact_path: "app/build/outputs/bundle/release/com.bitwarden.authenticator-release.aab",
        service_credentials_file: options[:serviceCredentialsFile],
        groups: "internal-prod-group",
        release_notes: release_notes,
      )
    end

  desc "Publish release to Google Play Store"
  lane :publishReleaseToGooglePlayStore do |options|
    upload_to_play_store(
      json_key: options[:serviceCredentialsFile],
      track: "internal",
      aab: "app/build/outputs/bundle/release/com.bitwarden.authenticator-release.aab",
      mapping: "app/build/outputs/mapping/release/mapping.txt",
    )
  end
end
