# 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 "Clean build directory"
  lane :clean do
    gradle(task: "clean")
  end

  desc "Ktlint"
  lane :ktlint do
    gradle(
      task: "ktlintCheck"
    )
  end

  desc "Ktlint Format"
  lane :ktlintFormat do
    gradle(
      task: "ktlintFormat"
    )
  end

  desc "Lint"
  lane :lint do
    gradle(
      task: "lint"
    )
  end

  desc "Test"
  lane :test do
    gradle(
      task: "test"
    )
  end

  desc "Assemble"
  lane :assemble do
    gradle(
      task: "assemble",
      flavor: "Full",
      build_type: "Release"
    )
  end

  desc "Bundle"
  lane :bundle do
   gradle(
     task: "bundle",
     flavor: "GooglePlay",
     build_type: "Release"
   )
  end

  desc "Deploy to GitHub"
  lane :github do
    version_name = get_version_name(app_project_dir: '**/app')
    version_code = get_version_code(app_project_dir: '**/app')
    sh("cd ../app/build/outputs/apk/full/release/ && shasum -a 1 app-full-release.apk > app-full-release.sha1")
    sh("cd ../app/build/outputs/apk/full/release/ && shasum -a 256 app-full-release.apk > app-full-release.sha256")
    sh("cd ../app/build/outputs/apk/full/release/ && shasum -a 512 app-full-release.apk > app-full-release.sha512")
    set_github_release(
      repository_name: "lneugebauer/nextcloud-cookbook",
      api_token: ENV["GITHUB_TOKEN"],
      name: "v" + version_name + "+" + version_code,
      tag_name: "v" + version_name + "+" + version_code,
      is_prerelease: true,
      description: (File.read("metadata/android/en-US/changelogs/" + version_code + ".txt") rescue "No changelog provided"),
      commitish: "main",
      upload_assets: ["app/build/outputs/apk/full/release/app-full-release.apk", "app/build/outputs/apk/full/release/app-full-release.sha1", "app/build/outputs/apk/full/release/app-full-release.sha256", "app/build/outputs/apk/full/release/app-full-release.sha512"]
    )
  end

  desc "Deploy to GPS"
  lane :playstore do
    upload_to_play_store(
      track: "internal",
      skip_upload_apk: true,
      skip_upload_metadata: true,
      skip_upload_images: true,
      skip_upload_screenshots: true
    )
  end

  desc "Deploy to GPS"
  lane :playstoreMetadata do
    upload_to_play_store(
      skip_upload_apk: true,
      skip_upload_aab: true,
      version_code: get_version_code(app_project_dir: '**/app')
    )
  end

  desc "Test and build"
  lane :build do
    clean
    ktlint
    lint
    test
    assemble
  end

  desc "Test and release"
  lane :deploy do
    clean
    ktlint
    lint
    test
    assemble
    github
    bundle
    playstore
    playstoreMetadata
  end

  desc "Generate screenshots"
  lane :screenshots do
    gradle(task: "assembleDebug assembleAndroidTest")
    # TODO: Use github matrix to create devices and/or start/stop devices with shell scripts to get rid of specific_device parameter
    # Get emulator ids by running $ adb devices -l
    screengrab(device_type: "phone", specific_device: "emulator-5554")
    screengrab(device_type: "sevenInch", specific_device: "emulator-5558")
    screengrab(device_type: "tenInch", specific_device: "emulator-5556")
  end
end
