# frozen_string_literal: true

default_platform(:android)

UI.user_error!('Please run fastlane via `bundle exec`') unless FastlaneCore::Helper.bundler?

GLOTPRESS_PROJECT_BASE_URL = 'https://translate.wordpress.com/projects/gravatar/gravatar-android-sdk'
RESOURCES_TO_TRANSLATE = {
  File.join('gravatar-ui', 'src', 'main', 'res') => "#{GLOTPRESS_PROJECT_BASE_URL}/gravatar-ui/",
  File.join('gravatar-quickeditor', 'src', 'main', 'res') => "#{GLOTPRESS_PROJECT_BASE_URL}/gravatar-quick-editor/"
}.freeze
PROTOTYPE_BUILD_DOMAIN = 'https://cdn.a8c-ci.services'

SUPPORTED_LOCALES = [
  { glotpress: 'ar',    android: 'ar'     },
  { glotpress: 'de',    android: 'de'     },
  { glotpress: 'es',    android: 'es'     },
  { glotpress: 'fr',    android: 'fr'     },
  { glotpress: 'he',    android: 'iw'     },
  { glotpress: 'id',    android: 'in'     },
  { glotpress: 'it',    android: 'it'     },
  { glotpress: 'ja',    android: 'ja'     },
  { glotpress: 'ko',    android: 'ko'     },
  { glotpress: 'nl',    android: 'nl'     },
  { glotpress: 'pt-br', android: 'pt-rBR' },
  { glotpress: 'ru',    android: 'ru'     },
  { glotpress: 'sv',    android: 'sv'     },
  { glotpress: 'tr',    android: 'tr'     },
  { glotpress: 'zh-cn', android: 'zh-rCN' },
  { glotpress: 'zh-tw', android: 'zh-rTW' }
].freeze

platform :android do
  # Download the latest app translations from GlotPress and update the strings.xml files accordingly.
  #
  # @example Running the lane
  #          bundle exec fastlane download_translations skip_commit:true
  #
  lane :download_translations do |skip_commit: false|
    RESOURCES_TO_TRANSLATE.each do |res_dir, gp_url|
      android_download_translations(
        res_dir: res_dir.to_s,
        glotpress_url: gp_url,
        locales: SUPPORTED_LOCALES,
        skip_commit: true
      )
    end

    next if skip_commit

    strings_paths = RESOURCES_TO_TRANSLATE.keys.map(&:to_s)
    git_add(path: strings_paths)
    git_commit(
      path: strings_paths,
      message: 'Update translations',
      allow_nothing_to_commit: true
    )
  end

  desc 'Builds a prototype build and uploads it to S3'
  lane :build_and_upload_prototype_build do
    UI.user_error!("'BUILDKITE_ARTIFACTS_S3_BUCKET' must be defined as an environment variable.") unless ENV['BUILDKITE_ARTIFACTS_S3_BUCKET']

    comment_on_pr(
      project: 'Automattic/Gravatar-SDK-Android',
      pr_number: Integer(ENV.fetch('BUILDKITE_PULL_REQUEST', nil)),
      reuse_identifier: 'app-prototype-build-link',
      body: '🚧 Demo app build will be available soon'
    )

    gradle(
      task: ':demo-app:assemble',
      build_type: 'release'
    )

    upload_path = upload_to_s3(
      bucket: 'a8c-apps-public-artifacts',
      key: "gravatar-demo-prototype-build-#{generate_prototype_build_number}.apk",
      file: lane_context[SharedValues::GRADLE_ALL_APK_OUTPUT_PATHS].first,
      if_exists: :skip
    )

    install_url = "#{PROTOTYPE_BUILD_DOMAIN}/#{upload_path}"

    comment_body = prototype_build_details_comment(
      app_display_name: 'Gravatar Demo',
      download_url: install_url,
      fold: true
    )

    comment_on_pr(
      project: 'Automattic/Gravatar-SDK-Android',
      pr_number: Integer(ENV.fetch('BUILDKITE_PULL_REQUEST', nil)),
      reuse_identifier: 'app-prototype-build-link',
      body: comment_body
    )
  end

  # This function is Buildkite-specific
  def generate_prototype_build_number
    if ENV['BUILDKITE']
      commit = ENV.fetch('BUILDKITE_COMMIT', nil)[0, 7]
      branch = ENV['BUILDKITE_BRANCH'].parameterize
      pr_num = ENV.fetch('BUILDKITE_PULL_REQUEST', nil)

      pr_num == 'false' ? "#{branch}-#{commit}" : "pr#{pr_num}-#{commit}"
    else
      repo = Git.open(PROJECT_ROOT_FOLDER)
      commit = repo.current_branch.parameterize
      branch = repo.revparse('HEAD')[0, 7]

      "#{branch}-#{commit}"
    end
  end
end
