# 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

sh("bundle install")

default_platform(:android)

desc "Create testing release"
lane :testing do
    gradle(task: "clean assembleCi")
end

desc "Create and deploy production release"
lane :production do
    prompt(text: "Did you write whats new?")

    Dir.chdir("..") do
      gradle(task: ":screenshots:recordRoborazziDebug")
      prompt(text: "Check the new screenshots")
    end

    version_code = get_properties_value(key: "VERSION_CODE", path: "./app/version.properties")
    version_name = get_properties_value(key: "VERSION_NAME", path: "./app/version.properties")

    live_version_codes = google_play_track_version_codes(track: "internal")
    latest_live_version_code = live_version_codes.max || 0

    if version_code.to_i <= latest_live_version_code
      UI.user_error!("VERSION_CODE (#{version_code}) in app/version.properties must be greater than the latest closed track version code (#{latest_live_version_code}).")
    end

#   Don't create changelog for f-droid because not committing it
#   File.write("metadata/android/en-US/changelogs/" + version_code + ".txt", whats_new)

    # Check Rust formatting and run tests
    Dir.chdir("../evdev/src/main/rust/evdev_manager") do
      sh("cargo fmt --check")
      sh("cargo test --package evdev_manager_core")
    end

    Dir.chdir("..") do
      gradle(task: "testDebugUnitTest")
    end

    github_token = prompt(
                    text: "Github token: ",
                    secure_text: true
                   )

    ENV["KEYSTORE_PASSWORD"] = prompt(
                    text: "Key store password: ",
                    secure_text: true
                   )

    ENV["KEY_PASSWORD"] = prompt(
                    text: "Key password: ",
                    secure_text: true
                   )

#   Release the free build to GitHub because billing only works if signed by Google Play
    gradle(task: "assembleRelease")

    Dir.chdir("..") do
      gradle(task: "app:bundleRelease")
    end

    apk_path_release="app/build/outputs/apk/release/keymapper-" + version_name + "-foss.apk"

    # Ask if the store listing assets should be uploaded. This is asked because even if
    # the assets didn't change, Google Play will think they did and this could lead
    # to a longer review time.
    upload_store_listing = UI.confirm("Did any Play Store listing assets change?")

    # First release to google play so any errors with the descriptions are resolved before
    # creating the git tag.
    supply(
        aab: "../app/build/outputs/bundle/release/app-release.aab",
        track: "internal",
        skip_upload_apk: true,
        # Skip uploading the title because F-droid should not have "Floating buttons" in its title.
        skip_upload_metadata: true,
        skip_upload_images: !upload_store_listing,
        skip_upload_screenshots: !upload_store_listing,
    )

    whats_new = File.read("../base/src/main/assets/whats-new.txt")

    github_release = set_github_release(
      repository_name: "keymapperorg/KeyMapper",
      api_bearer: github_token,
      name: version_name,
      tag_name: "v" + version_name,
      description: whats_new,
      commitish: "master",
      upload_assets: [apk_path_release],
      is_draft: false,
      is_prerelease: false
    )
end

desc "Create and deploy internal testing release"
lane :internal do
    version_code = get_properties_value(key: "VERSION_CODE", path: "./app/version.properties")
    version_name = get_properties_value(key: "VERSION_NAME", path: "./app/version.properties")

    live_version_codes = google_play_track_version_codes(track: "internal")
    latest_live_version_code = live_version_codes.max || 0

    if version_code.to_i <= latest_live_version_code
      UI.user_error!("VERSION_CODE (#{version_code}) in app/version.properties must be greater than the latest closed track version code (#{latest_live_version_code}).")
    end

    # Check Rust formatting and run tests
    Dir.chdir("../evdev/src/main/rust/evdev_manager") do
      sh("cargo fmt --check")
      sh("cargo test --package evdev_manager_core")
    end

    Dir.chdir("..") do
      gradle(task: "ktlintFormat")
      gradle(task: "ktlintCheck")
      gradle(task: "testDebugUnitTest")
    end

#   Don't create changelog for f-droid because not committing it
#   File.write("metadata/android/en-US/changelogs/" + version_code + ".txt", whats_new)

    ENV["KEYSTORE_PASSWORD"] = prompt(
                    text: "Key store password: ",
                    secure_text: true
                   )

    ENV["KEY_PASSWORD"] = prompt(
                    text: "Key password: ",
                    secure_text: true
                   )

    Dir.chdir("..") do
      gradle(task: "app:bundleRelease")
    end

      # First release to google play so any errors with the descriptions are resolved before
      # creating the git tag.
      supply(
          aab: "../app/build/outputs/bundle/release/app-release.aab",
          track: "internal",
          skip_upload_apk: true,
          skip_upload_metadata: true,
          skip_upload_changelogs: true,
          skip_upload_images: true,
          skip_upload_screenshots: true
      )
end

