# Customise this file, documentation can be found here:
# https://github.com/fastlane/fastlane/tree/master/fastlane/docs
# All available actions: https://docs.fastlane.tools/actions
# can also be listed using the `fastlane actions` command

# Change the syntax highlighting to Ruby
# All lines starting with a # are ignored when running `fastlane`

# If you want to automatically update fastlane if a new version is available:
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.53.1"

default_platform :android

platform :android do
  before_all do
    # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
  end

  lane :bump_version_code do | options|
    path = '../app/build.gradle'
    re = /versionCode\s+(\d+)/

    s = File.read(path)
    s[re, 1] = (options[:code]).to_s

    f = File.new(path, 'w')
    f.write(s)
    f.close
  end

  desc "Generate a new Build"
  lane :build do
    
    # Update the version code
    # TODO: Later for F-Droid support, we'll need to sync this with AndroidManifest.xml
    # and then commit it back in the repo.
    # The commit should only happen on the develop branch however, so we don't get out of sync branches.
    bump_version_code(code: ENV["TRAVIS_BUILD_NUMBER"])
    
    gradle(
      task: "assemble",
      build_type: "Release",
      # project_dir: "platforms/android/",
      properties: {
        'android.useDeprecatedNdk' => true,
        'android.injected.signing.store.file' => 'app/animetrackkey.jks',
        'android.injected.signing.store.password' => ENV["KITSU_RELEASE_STORE_PASSWORD"],
        'android.injected.signing.key.alias'=> ENV["KITSU_RELEASE_KEY_ALIAS"],
        'android.injected.signing.key.password' => ENV["KITSU_RELEASE_KEY_PASSWORD"]
      }
    )
  end

  desc "Submit a new Alpha Build to Crashlytics"
  lane :alpha_crashlytics do |values|
    build()

    emails = values[:test_email] ? values[:test_email] : ['is@urihere.com'] # You can list more emails here
    groups = values[:test_email] ? nil : nil # You can define groups on the web and reference them here

    crashlytics(
      api_token: 'b4ccb0473f2023c511cbd8fbd2dcef86b2cf7b00',
      build_secret: '010015018557534a1a97465380fd280881f586eec24f45bcc511520a031d50d4',
      emails: emails,
      groups: groups,
      debug: true,
      # there is a bug of crashlytics for notes (https://github.com/fastlane/fastlane/issues/6325)
      # notes: 'Distributed with fastlane', # Check out the changelog_from_git_commits action
      notifications: true  # Should this distribution notify your testers via email?
    )
  end

  desc "Submit a new Alpha Build to Google Play"
  lane :alpha_googleplay do
    build()
    supply(
      track: 'alpha',
      skip_upload_metadata: true,
      skip_upload_images: true,
      skip_upload_screenshots: true
    )
  end

  desc "Release a new version to the Google Play"
  lane :release do
    build()

    # For now this flow uses alpha since we can directly promote the app to publish from alpha
    # However in the future this probably needs a revisit so the rollout process is easier
    # TODO: Fix this flow up
    supply(
      track: 'alpha',
      skip_upload_metadata: true,
      skip_upload_images: true,
      skip_upload_screenshots: true
    )
    
    # Disabled this because it didn't seem to work :(
    # supply(
    #   skip_upload_metadata: true,
    #   skip_upload_images: true,
    #   skip_upload_screenshots: true
    # )
  end


  after_all do |lane|
    # This block is called, only if the executed lane was successful

    # slack(
    #   message: "Successfully deployed new App Update."
    # )
  end

  error do |lane, exception|
    # slack(
    #   message: exception.message,
    #   success: false
    # )
  end
end


# More information about multiple platforms in fastlane: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
# All available actions: https://docs.fastlane.tools/actions

# fastlane reports which actions are used. No personal data is recorded.
# Learn more at https://github.com/fastlane/fastlane#metrics
