require 'yaml'
require_relative 'changelog'

before_all do
  xcversion(version: "~> 14.1")

  ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "180"
  ENV["FASTLANE_XCODE_LIST_TIMEOUT"] = "180"
end

lane :alpha do
  app_store_connect_api_key(
    key_id: ENV["APPSTORECONNECT_KEY_ID"],
    issuer_id: ENV["APPSTORECONNECT_KEY_ISSUER_ID"],
    key_content: ENV["APPSTORECONNECT_KEY_CONTENT"]
  )

  config_xcodegen_alpha()

  code_signing_identity = "Apple Distribution: Vector Creations Limited (7J4U792NQT)"
  
  app_provisioning_profile_name = "ElementX PR Ad Hoc"
  app_bundle_identifier = "io.element.elementx.pr"

  nse_provisioning_profile_name = "ElementX NSE PR Ad Hoc"
  nse_bundle_identifier = "io.element.elementx.pr.nse"

  update_code_signing_settings(
    targets: ["ElementX"],
    use_automatic_signing: false,
    bundle_identifier: app_bundle_identifier,
    profile_name: app_provisioning_profile_name,
    code_sign_identity: code_signing_identity
  )

  get_provisioning_profile(
    app_identifier: app_bundle_identifier,
    provisioning_name: app_provisioning_profile_name,
    ignore_profiles_with_different_name: true,
    adhoc: true 
  )

  update_code_signing_settings(
    targets: ["NSE"],
    use_automatic_signing: false,
    bundle_identifier: nse_bundle_identifier,
    profile_name: nse_provisioning_profile_name,
    code_sign_identity: code_signing_identity
  )

  get_provisioning_profile(
    app_identifier: nse_bundle_identifier,
    provisioning_name: nse_provisioning_profile_name,
    ignore_profiles_with_different_name: true,
    adhoc: true 
  )

  build_ios_app(
    scheme: "ElementX",
    clean: true,
    export_method: "ad-hoc",
    output_directory: "build",
    export_options: {
      provisioningProfiles: { 
        app_bundle_identifier => app_provisioning_profile_name,
        nse_bundle_identifier => nse_provisioning_profile_name
      }
    }
  )

  upload_to_diawi()
end

lane :app_store_release do
  build_release()

  release_to_github()

  prepare_next_release()

  upload_dsyms_to_sentry(dsym_path: './build/ElementX.app.dSYM.zip')
end

lane :build_release do
  bump_build_number()

  build_ios_app(
    scheme: "ElementX",
    clean: true,
    export_method: "app-store",
    output_directory: "build",
    xcargs: "-allowProvisioningUpdates",
  )
end

lane :unit_tests do
  run_tests(
    scheme: "UnitTests"
  )

  slather(
    cobertura_xml: true,
    output_directory: "./fastlane/test_output",
    proj: "ElementX.xcodeproj",
    scheme: "UnitTests",
  )
end

lane :ui_tests do 
  run_tests(
    scheme: "UITests",
    devices: ["iPhone 14", "iPad (9th generation)"],
    ensure_devices_found: true,
    prelaunch_simulator: true,
    result_bundle: true
  )

  slather(
    cobertura_xml: true,
    output_directory: "./fastlane/test_output",
    proj: "ElementX.xcodeproj",
    scheme: "UITests",
    binary_basename: "ElementX.app"
  )
end

lane :integration_tests do
  run_tests(
    scheme: "IntegrationTests",
    devices: ["iPhone 13 Pro"],
    ensure_devices_found: true,
  )

  slather(
    cobertura_xml: true,
    output_directory: "./fastlane/test_output",
    proj: "ElementX.xcodeproj",
    scheme: "IntegrationTests",
  )
end

lane :config_nightly do
  target_file_path = "../project.yml"
  data = YAML.load_file target_file_path
  data["settings"]["BASE_APP_GROUP_IDENTIFIER"] = "io.element.nightly"
  data["settings"]["BASE_BUNDLE_IDENTIFIER"] = "io.element.elementx.nightly"
  File.open(target_file_path, 'w') { |f| YAML.dump(data, f) }

  xcodegen(spec: "project.yml")

  # Automatically done by Xcode Cloud. Cannot override
  # https://developer.apple.com/documentation/xcode/setting-the-next-build-number-for-xcode-cloud-builds
  # bump_build_number()

  release_version = get_version_number(target: "ElementX")

  update_app_icon(caption_text: "Nightly #{release_version}", modulate: "100,20,100")
end

lane :upload_dsyms_to_sentry do |options|
  auth_token = ENV["SENTRY_AUTH_TOKEN"]
  UI.user_error!("Invalid Sentry Auth token.") unless !auth_token.to_s.empty?

  dsym_path = options[:dsym_path]
  UI.user_error!("Invalid DSYM path.") unless !dsym_path.to_s.empty?

  sentry_upload_dif(
    auth_token: auth_token,
    org_slug: 'element',
    project_slug: 'elementx',
    url: 'https://sentry.tools.element.io/',
    path: dsym_path,
  )
end

lane :release_to_github do
  api_token = ENV["GITHUB_TOKEN"]
  UI.user_error!("Invalid GitHub API token.") unless !api_token.to_s.empty?

  # Get the Diawi link from Diawi action shared value
  diawi_link = lane_context[SharedValues::UPLOADED_FILE_LINK_TO_DIAWI]

  release_version = get_version_number(target: "ElementX")

  changes = export_version_changes(version: release_version)

  description = ""
  if diawi_link.nil?
    description = "#{changes}"
  else
    # Generate the Diawi QR code file link
    diawi_app_id = URI(diawi_link).path.split('/').last
    diawi_qr_code_link = "https://www.diawi.com/qrcode/link/#{diawi_app_id}"

    "[iOS AdHoc Release - Diawi Link](#{diawi_link})
    ![QR code](#{diawi_qr_code_link})
    #{changes}"
  end

  github_release = set_github_release(
    repository_name: "vector-im/element-x-ios",
    api_token: api_token,
    name: release_version,
    tag_name: release_version,
    is_generate_release_notes: false,
    description: description
  )

end

lane :prepare_next_release do
  api_token = ENV["GITHUB_TOKEN"]
  UI.user_error!("Invalid GitHub API token.") unless !api_token.to_s.empty?

  target_file_path = "../project.yml"
  xcode_project_file_path = "../ElementX.xcodeproj"

  data = YAML.load_file target_file_path
  current_version = data["settings"]["MARKETING_VERSION"]
  new_version = current_version.next
  
  # Bump the patch version. The empty string after -i is so that sed doesn't 
  # create a backup file on macOS
  sh("sed -i '' 's/#{current_version}/#{new_version}/g' #{target_file_path}")

  xcodegen(spec: "project.yml")

  sh("git config --global user.name 'Element CI'")
  sh("git config --global user.email 'ci@element.io'")

  sh("git add #{target_file_path} #{xcode_project_file_path}")

  sh("git commit -m 'Prepare next release'")

  # Get repo url path, without `http` or `git@` prefixes
  repo_url = sh("git ls-remote --get-url origin | sed 's#http://##g' | sed 's#https:\/\/##g' | sed 's#git@##g\'")

  # Use the access token for write access
  sh("git push https://#{api_token}@#{repo_url}")
end

private_lane :config_xcodegen_alpha do
  target_file_path = "../project.yml"
  data = YAML.load_file target_file_path
  data["settings"]["BASE_APP_GROUP_IDENTIFIER"] = "io.element.pr"
  data["settings"]["BASE_BUNDLE_IDENTIFIER"] = "io.element.elementx.pr"
  File.open(target_file_path, 'w') { |f| YAML.dump(data, f) }

  xcodegen(spec: "project.yml")

  version = ENV["GITHUB_PR_NUMBER"]

  update_app_icon(caption_text: "PR #{version}", modulate: "100,100,200")

  bump_build_number()
end

private_lane :upload_to_diawi do
  api_token = ENV["DIAWI_API_TOKEN"]
  UI.user_error!("Invalid Diawi API token.") unless !api_token.to_s.empty?

  # Upload to Diawi
  diawi(
    token: api_token,
    wall_of_apps: false,
    file: lane_context[SharedValues::IPA_OUTPUT_PATH]
  )

  # Get the Diawi link from Diawi action shared value
  diawi_link = lane_context[SharedValues::UPLOADED_FILE_LINK_TO_DIAWI]
  UI.command_output("Diawi link: " + diawi_link.to_s)

  # Generate the Diawi QR code file link
  diawi_app_id = URI(diawi_link).path.split('/').last
  diawi_qr_code_link = "https://www.diawi.com/qrcode/link/#{diawi_app_id}"

  # Set "DIAWI_FILE_LINK" to GitHub environment variables for Github actions
  sh("echo DIAWI_FILE_LINK=#{diawi_link} >> $GITHUB_ENV")
  sh("echo DIAWI_QR_CODE_LINK=#{diawi_qr_code_link} >> $GITHUB_ENV")
end

private_lane :bump_build_number do
  # Increment build number to current date
  build_number = Time.now.strftime("%Y%m%d%H%M")
  increment_build_number(build_number: build_number)
end

private_lane :export_version_changes do |options|
  Dir.chdir("..") do
    Changelog.update_topmost_section(version: options[:version], additional_entries: {})
    Changelog.extract_first_section
  end
end

private_lane :update_app_icon do |options|
  caption_text = options[:caption_text]
  UI.user_error!("Invalid caption text.") unless !caption_text.to_s.empty?

  modulate = options[:modulate]
  UI.user_error!("Invalid icon modulate parameters.") unless !modulate.to_s.empty?

  Dir.glob("../ElementX/Resources/Assets.xcassets/AppIcon.appiconset/**/*.png") do |file_name|
    # Change the icons color
    sh("convert '#{file_name}' -modulate #{modulate} '#{file_name}'")

    caption_width = sh("identify -format %w '#{file_name}'")
    caption_height = file_name.end_with?("@2x.png") ? 60 : 30
    if caption_width.to_i > caption_height*2 then

      # Add a label on top
      sh("convert -background '#0008' -fill white -gravity center -size '#{caption_width}'x'#{caption_height}' caption:'#{caption_text}' '#{file_name}' +swap -gravity south -composite '#{file_name}'")
    end
  end
end
