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

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:android)
ENV_PATH = "../asset/app/.env"
ANDROID_VERSION_NAME = 'androidVersionName'
APP_BUNDLE_PATH="../build/app/outputs/bundle/release/app-release.aab"
INTERNAL = 'internal'
platform :android do
  // lane :internal do |options| xác định một làn Fastlane được gọi là "internal" nhận các tùy chọn như tham số.
  desc "Deploy to internal test app"
  lane :internal do |options|
    versionNumberArrayLength = google_play_track_version_codes(track:INTERNAL)
    versionNumber =(versionNumberArrayLength.length > 0 ? versionNumberArrayLength[0] : 0).to_i + 1
    incerementVersion version: options[:version]
    setVersionNumber versionNumber: options[:versionNumber]
    versionName = getVersionName()
    flutter_build(versionName,versionNumber)  
    buildStore(INTERNAL)
  end
    
  // private_lane :setVersionNumber do |options| là một làn Fastlane riêng tư để đặt số phiên bản trong tệp thuộc tính môi trường nếu được cung cấp
  private_lane :setVersionNumber do |options|
    if options[:versionNumber] != nil 
      set_properties_value(
        key: ANDROID_VERSION_NAME,
        path: ENV_PATH,
        value: options[:versionNumber]
      )
    end
  end

  // buildStore(track) tải lên gói ứng dụng lên Google Play Store với track và trạng thái phát hành được chỉ định
  def buildStore(track)
    upload_to_play_store(
      track: track,
      release_status: 'draft',
      aab: APP_BUNDLE_PATH
    )
  end
  // private_lane :incrementVersion do |options| là một làn Fastlane riêng tư khác để tăng tên phiên bản trong tệp thuộc tính môi trường nếu được cung cấp.
  private_lane :incerementVersion do |options|
    if options[:versionNumber] != nil 
      increment_version_name_in_properties_file(
        key: ANDROID_VERSION_NAME,
        path: ENV_PATH,
        update_type: options[:version]
      )
    end
  end
 // getVersionName lấy tên phiên bản hiện tại từ tệp thuộc tính môi trường
  def getVersionName 
    return get_properties_value(
      key: ANDROID_VERSION_NAME,
      path: ENV_PATH
    )
  end
  // flutter_build(versionName,number) xây dựng gói ứng dụng Flutter. Nó thay đổi thư mục hiện tại thành thư mục cha ('../../') và thực thi các lệnh Flutter để lấy các gói, làm sạch bản dựng và xây dựng gói ứng dụng với tên và số phiên bản được cung cấp
  def flutter_build(versionName,number)
    # local_version_number = 
    # get_properties_value(key: KEY_VERSION_NAME, path: ENV_PATH) 
    Dir.chdir '../../' do
      sh('flutter' , 'packages', 'get')
      sh('flutter' , 'clean')
      sh("flutter build appbundle --build-name=#{
        versionName
      } --build-number=#{number.to_s}")
    end
  end

 
end
