# 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 :ios

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

  desc "Runs all the tests"
  lane :test do
    scan
  end

  desc "Deploy a new version to the App Store"
  lane :update_match do
    register_devices(
      devices_file: "./fastlane/devices.txt"
    )

    match(
      app_identifier: "com.EverFox.AnimeTrakr",
      git_url: "https://github.com/hummingbird-me/certificates.git",
      type: "development",
      force_for_new_devices: true
    )

    match(
      app_identifier: "com.EverFox.AnimeTrakr",
      git_url: "https://github.com/hummingbird-me/certificates.git",
      type: "adhoc",
      force_for_new_devices: true
    )

    match(
      app_identifier: "com.EverFox.AnimeTrakr",
      git_url: "https://github.com/hummingbird-me/certificates.git",
      type: "appstore"
    )

  end

  desc "Generate a new Build"
  lane :build do
    # Before calling match, we make sure all our devices are registered on the Apple Developer Portal
    create_keychain(
      name: ENV["MATCH_KEYCHAIN_NAME"],
      password: ENV["MATCH_KEYCHAIN_PASSWORD"],
      default_keychain: true,
      unlock: true,
      timeout: 3600,
      add_to_search_list: true
    )

    unlock_keychain(
			path: ENV["MATCH_KEYCHAIN_NAME"],
			add_to_search_list: :replace,
      password: ENV['MATCH_KEYCHAIN_PASSWORD']
		)

    register_devices(
      devices_file: "./fastlane/devices.txt"
    )

    # After registering the new devices, we'll make sure to update the provisioning profile if necessary
    # options for type are "adhoc", "development", "appstore"
    match(
      app_identifier: "com.EverFox.AnimeTrakr",
      type: "development",
      force_for_new_devices: !is_ci, # update only forced on non-CI systems, and only if there is a new device
      readonly: true,  #readonly mode to be sure it won't create any new certificates or profiles.
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"]
    )

    match(
      app_identifier: "com.EverFox.AnimeTrakr",
      type: "appstore",
      # clone_branch_directly: true,
      readonly: true,  #readonly mode to be sure it won't create any new certificates or profiles.
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
      verbose: true
    )

    # Change build number (requirement of Testflight).
    # We don't need to push this to github because iOS versions are not dependant on the build number
    # Unlike android which has its versioning system dependant on it
    increment_build_number(
      build_number: ENV["TRAVIS_BUILD_NUMBER"]
    )

    # Get pods
    cocoapods

    # Build a release
    gym(
      scheme: "kitsu_mobile",
      clean: true,
      configuration: "Release",
      # project: "./kitsu_mobile.xcodeproj", # if not using pods
      workspace: "./kitsu_mobile.xcworkspace", 
      xcargs: "PROVISIONING_PROFILE_SPECIFIER='match AppStore com.EverFox.AnimeTrakr'"
    )
  end

  desc "Submit a new Alpha/Beta Build to Testflight"
  lane :alpha_testflight do
    build()

    # Auto submit for alpha testers
    pilot()
  end

  desc "Deploy a new version to the App Store"
  lane :release do
    build()
    deliver(
      force: true,
      skip_metadata: true,
      skip_app_version_update: true,
      skip_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
