desc "Sync Android version of the app with the package.json file"
lane :set_android_version do
  package_json = load_json(json_path: "./package.json")
  version = package_json["version"]

  if File.directory?("./android")
    increment_version_name(
      gradle_file_path: "./android/app/build.gradle",
      version_name: version
    )
  else
    # handle expo managed projects
    app_json = load_json(json_path: "./app.json")

    app_json["expo"]["version"] = version

    File.open("../app.json", "w") do |f|
      f.write(JSON.pretty_generate(app_json))
    end
  end
end

desc "Sync iOS version of the app with the package.json file"
lane :set_ios_version do
  package_json = load_json(json_path: "./package.json")
  version = package_json["version"]

  if File.directory?("./ios")

    xcodeproj_path = "./ios/" + package_json["name"] + ".xcodeproj"

    increment_version_number(
      xcodeproj: xcodeproj_path,
      version_number: version
    )
  else
    # handle expo managed projects
    app_json = load_json(json_path: "./app.json")

    app_json["expo"]["version"] = version

    File.open("../app.json", "w") do |f|
      f.write(JSON.pretty_generate(app_json))
    end
  end
end

desc "Sync Android and iOS version of the app with the package.json file"
lane :set_version do
  set_android_version
  set_ios_version
end

desc "Bump build number and version code"
lane :bump_build_number do
  package_json = load_json(json_path: "./package.json")
  xcodeproj_path = "./ios/" + package_json["name"] + ".xcodeproj"

  if File.directory?("./android")
    increment_version_code(
      gradle_file_path: "./android/app/build.gradle",
    )
  else
    # handle expo managed projects
    app_json = load_json(json_path: "./app.json")
    current_version_code = app_json["expo"]["android"]["versionCode"] || 0
    new_version_code = current_version_code + 1

    app_json["expo"]["android"]["versionCode"] = new_version_code

    File.open("../app.json", "w") do |f|
      f.write(JSON.pretty_generate(app_json))
    end
  end

  if File.directory?("./ios")
    increment_build_number(
      xcodeproj: xcodeproj_path,
    )
  else
    # handle expo managed projects
    app_json = load_json(json_path: "./app.json")
    current_build_number = app_json["expo"]["ios"]["buildNumber"].to_i || 0
    new_build_number = current_build_number + 1

    app_json["expo"]["ios"]["buildNumber"] = new_build_number.to_s

    File.open("../app.json", "w") do |f|
      f.write(JSON.pretty_generate(app_json))
    end
  end
end

# note: this lane will not work if you do not configure the google_drive_key.json file.
# tip: to not specify the folder_id each time you call this lane, you can add the folder_id to this fastfile
desc "Upload the Android APK to Google Drive"
lane :upload_apk do |options|
  apk_path = options[:apk_path] || prompt(text: "Enter the path to the APK file:")
  folder_id = options[:folder_id] || prompt(text: "Enter the folder ID (it is the last part of the url when viewing the folder in the browser):")
  drive_keyfile = options[:drive_keyfile] || "google_service_key.json"

  upload_to_google_drive(
    drive_keyfile: drive_keyfile,
    service_account: true,
    folder_id: folder_id,
    upload_files: [apk_path] || ["./android/app/build/outputs/apk/release/app-release.apk"],
  )
end
