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

default_platform(:ios)

TEAMID = ENV["TEAMID"]
GH_PAT = ENV["GH_PAT"]
GITHUB_WORKSPACE = ENV["GITHUB_WORKSPACE"]
GITHUB_REPOSITORY_OWNER = ENV["GITHUB_REPOSITORY_OWNER"]
FASTLANE_KEY_ID = ENV["FASTLANE_KEY_ID"]
FASTLANE_ISSUER_ID = ENV["FASTLANE_ISSUER_ID"]
FASTLANE_KEY = ENV["FASTLANE_KEY"]
DEVICE_NAME = ENV["DEVICE_NAME"]
DEVICE_ID = ENV["DEVICE_ID"]
ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "120"

platform :ios do
  desc "Build iAPS"
  lane :build_iAPS do
    setup_ci if ENV['CI']
    
    update_project_team(
      path: "#{GITHUB_WORKSPACE}/FreeAPS.xcodeproj",
      teamid: "#{TEAMID}"
    )

    api_key = app_store_connect_api_key(
      key_id: "#{FASTLANE_KEY_ID}",
      issuer_id: "#{FASTLANE_ISSUER_ID}",
      key_content: "#{FASTLANE_KEY}"
    )

    previous_build_number = latest_testflight_build_number(
      app_identifier: "ru.artpancreas.#{TEAMID}.FreeAPS",
      api_key: api_key,
    )

    current_build_number = previous_build_number + 1

    increment_build_number(
      xcodeproj: "#{GITHUB_WORKSPACE}/FreeAPS.xcodeproj",
      build_number: current_build_number
    )      
    
    match(
      type: "appstore",
      git_basic_authorization: Base64.strict_encode64("#{GITHUB_REPOSITORY_OWNER}:#{GH_PAT}"),
      app_identifier: [
        "ru.artpancreas.#{TEAMID}.FreeAPS",
        "ru.artpancreas.#{TEAMID}.FreeAPS.watchkitapp",
        "ru.artpancreas.#{TEAMID}.FreeAPS.watchkitapp.watchkitextension",
        "ru.artpancreas.#{TEAMID}.FreeAPS.LiveActivity"
      ]
    )

    previous_build_number = latest_testflight_build_number(
      app_identifier: "ru.artpancreas.#{TEAMID}.FreeAPS",
      api_key: api_key,
    )

    current_build_number = previous_build_number + 1

    increment_build_number(
      xcodeproj: "#{GITHUB_WORKSPACE}/FreeAPS.xcodeproj",
      build_number: current_build_number
    )
    
    mapping = Actions.lane_context[
      SharedValues::MATCH_PROVISIONING_PROFILE_MAPPING
    ]

    update_code_signing_settings(
      path: "#{GITHUB_WORKSPACE}/FreeAPS.xcodeproj",
      profile_name: mapping["ru.artpancreas.#{TEAMID}.FreeAPS"],
      code_sign_identity: "iPhone Distribution",
      targets: ["FreeAPS"]
    )

    update_code_signing_settings(
      path: "#{GITHUB_WORKSPACE}/FreeAPS.xcodeproj",
      profile_name: mapping["ru.artpancreas.#{TEAMID}.FreeAPS.watchkitapp.watchkitextension"],
      code_sign_identity: "iPhone Distribution",
      targets: ["FreeAPSWatch WatchKit Extension"]
    )
    
    update_code_signing_settings(
      path: "#{GITHUB_WORKSPACE}/FreeAPS.xcodeproj",
      profile_name: mapping["ru.artpancreas.#{TEAMID}.FreeAPS.watchkitapp"],
      code_sign_identity: "iPhone Distribution",
      targets: ["FreeAPSWatch"]
    )
    update_code_signing_settings(
      path: "#{GITHUB_WORKSPACE}/FreeAPS.xcodeproj",
      profile_name: mapping["ru.artpancreas.#{TEAMID}.FreeAPS.LiveActivity"],
      code_sign_identity: "iPhone Distribution",
      targets: ["LiveActivityExtension"]
    )

    gym(
      export_method: "app-store",
      scheme: "FreeAPS X",
      output_name: "iAPS.ipa",
      configuration: "Release",
      destination: 'generic/platform=iOS',
      buildlog_path: 'buildlog'
    )

    copy_artifacts(
      target_path: "artifacts",
      artifacts: ["*.mobileprovision", "*.ipa", "*.dSYM.zip"]
    )
  end

  desc "Push to TestFlight"
  lane :release do
    api_key = app_store_connect_api_key(
      key_id: "#{FASTLANE_KEY_ID}",
      issuer_id: "#{FASTLANE_ISSUER_ID}",
      key_content: "#{FASTLANE_KEY}"
    )
    
    upload_to_testflight(
      api_key: api_key,
      skip_submission: false,
      ipa: "iAPS.ipa",
      skip_waiting_for_build_processing: true,
      changelog: git_branch+" "+last_git_commit[:abbreviated_commit_hash],
    )
  end

  desc "Provision Identifiers and Certificates"
  lane :identifiers do
    setup_ci if ENV['CI']
    ENV["MATCH_READONLY"] = false.to_s
    
    app_store_connect_api_key(
      key_id: "#{FASTLANE_KEY_ID}",
      issuer_id: "#{FASTLANE_ISSUER_ID}",
      key_content: "#{FASTLANE_KEY}"
    )

    def configure_bundle_id(name, identifier, capabilities)
      bundle_id = Spaceship::ConnectAPI::BundleId.find(identifier) || Spaceship::ConnectAPI::BundleId.create(name: name, identifier: identifier)
      capabilities.each { |capability|
        bundle_id.create_capability(capability)
      }
    end

    configure_bundle_id("FreeAPS", "ru.artpancreas.#{TEAMID}.FreeAPS", [
      Spaceship::ConnectAPI::BundleIdCapability::Type::APP_GROUPS,
      Spaceship::ConnectAPI::BundleIdCapability::Type::HEALTHKIT,
      Spaceship::ConnectAPI::BundleIdCapability::Type::NFC_TAG_READING
    ])

    configure_bundle_id("FreeAPSWatch WatchKit Extension", "ru.artpancreas.#{TEAMID}.FreeAPS.watchkitapp.watchkitextension", [
      Spaceship::ConnectAPI::BundleIdCapability::Type::APP_GROUPS,
      Spaceship::ConnectAPI::BundleIdCapability::Type::HEALTHKIT
    ])
    
    configure_bundle_id("FreeAPSWatch", "ru.artpancreas.#{TEAMID}.FreeAPS.watchkitapp", [
      Spaceship::ConnectAPI::BundleIdCapability::Type::APP_GROUPS
    ])

    configure_bundle_id("LiveActivityExtension", "ru.artpancreas.#{TEAMID}.FreeAPS.LiveActivity", [
      Spaceship::ConnectAPI::BundleIdCapability::Type::APP_GROUPS
    ])
    
  end

  desc "Provision Certificates"
  lane :certs do
    setup_ci if ENV['CI']
    ENV["MATCH_READONLY"] = false.to_s
    
    app_store_connect_api_key(
      key_id: "#{FASTLANE_KEY_ID}",
      issuer_id: "#{FASTLANE_ISSUER_ID}",
      key_content: "#{FASTLANE_KEY}"
    )
    
    match(
      type: "appstore",
      force: true,
      git_basic_authorization: Base64.strict_encode64("#{GITHUB_REPOSITORY_OWNER}:#{GH_PAT}"),
      app_identifier: [
        "ru.artpancreas.#{TEAMID}.FreeAPS",
        "ru.artpancreas.#{TEAMID}.FreeAPS.watchkitapp.watchkitextension",
        "ru.artpancreas.#{TEAMID}.FreeAPS.watchkitapp",
        "ru.artpancreas.#{TEAMID}.FreeAPS.LiveActivity"
      ]
    )
  end

  desc "Validate Secrets"
  lane :validate_secrets do
    setup_ci if ENV['CI']
    ENV["MATCH_READONLY"] = true.to_s

    app_store_connect_api_key(
      key_id: "#{FASTLANE_KEY_ID}",
      issuer_id: "#{FASTLANE_ISSUER_ID}",
      key_content: "#{FASTLANE_KEY}"
    )

    def find_bundle_id(identifier)
      bundle_id = Spaceship::ConnectAPI::BundleId.find(identifier)
    end

    find_bundle_id("ru.artpancreas.#{TEAMID}.FreeAPS")

    match(
      type: "appstore",
      git_basic_authorization: Base64.strict_encode64("#{GITHUB_REPOSITORY_OWNER}:#{GH_PAT}"),
      app_identifier: [],
    )
  end

  desc "Nuke Certs"
  lane :nuke_certs do
    setup_ci if ENV['CI']
    ENV["MATCH_READONLY"] = false.to_s
    
    app_store_connect_api_key(
      key_id: "#{FASTLANE_KEY_ID}",
      issuer_id: "#{FASTLANE_ISSUER_ID}",
      key_content: "#{FASTLANE_KEY}"
    )
    
    match_nuke(
      type: "appstore",
      team_id: "#{TEAMID}",
      skip_confirmation: true,
      git_basic_authorization: Base64.strict_encode64("#{GITHUB_REPOSITORY_OWNER}:#{GH_PAT}")
    )
  end
end
