default_platform(:android)

platform :android do

  desc "Sanity check to make sure the example app compiles"
  lane :compile_example do
    gradle(task: 'assemble')
  end

  desc "Deploy a new version to Google Play internal test"
  lane :deploy_example do
    sh("sh", "./replace-placeholders.sh", ENV["EX_APPCUES_ACCOUNT_ID"], ENV["EX_APPCUES_APPLICATION_ID"], ENV["EX_GOOGLE_SERVICES_KEY"])

    keystorePath = File.join(Dir.pwd, "..", "appcues.keystore")

    increment_version_code(
      gradle_file_path: "./app/build.gradle",
      version_code: ENV["CIRCLE_BUILD_NUM"].to_i
    )

    increment_version_name(
      gradle_file_path: "./app/build.gradle",
      version_name: "1.0.0-#{ENV["CIRCLE_BUILD_NUM"]}"
    )

    gradle(
      task: 'bundle',
      build_type: 'Release',
      print_command: false,
      properties: {
        "android.injected.signing.store.file" => keystorePath,
        "android.injected.signing.store.password" => ENV["KEYSTORE_PASSWORD"],
        "android.injected.signing.key.alias" => ENV["KEYSTORE_ALIAS"],
        "android.injected.signing.key.password" => ENV["KEYSTORE_PASSWORD"]
      }
    )

    upload_to_play_store(
      aab: '../build/app/outputs/bundle/release/app-release.aab',
      track: 'internal', 
      skip_upload_apk: true, 
      json_key_data: ENV["PLAY_STORE_API_KEY"],
      rollout: '1'
    )
  end

end
