before_all do
  ENV["BUILD_NUMBER"] = sh("printf $(git show -s --format=%ct)")
  ENV["VERSION"] = sh("printf $(cat ../package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')")
end

error do |lane, exception|
  reset_git_repo(force: true, skip_clean: true)
 end

desc 'Deploy Staging Builds'
lane :deploy_staging do
  sh "bash ./pre-build-staging.sh"
  install
  android_staging
  ios_staging
  clean_build_artifacts
  reset_git_repo(force: true, skip_clean: true)
end

desc 'Deploy Staging Builds'
lane :deploy_release do
  sh "bash ./pre-build-release.sh"
  install
  android_release
  ios_release
  clean_build_artifacts
  reset_git_repo(force: true, skip_clean: true)
end

desc 'Install'
private_lane :install do
  yarn(
    command: "install",
    package_path: "./package.json"
  )
end

# desc 'Tag'
# private_lane :tag do
#   add_git_tag(
#     tag: ENV["VERSION"] + '.' + ENV["BUILD_NUMBER"]
#   )
#   push_git_tags
# end


desc 'Pods'
private_lane :pods do
  clean_cocoapods_cache
  clear_derived_data
  cocoapods(
    repo_update: true,
    podfile: "./ios/Podfile"
  )
end

desc 'Build the Android application. and deploy to beta'
private_lane :android_staging do
  gradle(
    task: 'clean',
    project_dir: './android/'
  )
  gradle(
    task: "assemble",
    build_type: 'release',
    flavor: 'staging',
    project_dir: './android/'
  )
  commit = last_git_commit
  appcenter_upload(
    api_token: ENV["APPCENTER_TOKEN"],
    owner_name: "Lightbase",
    app_name: "lightbaseTemplate-android",
    release_notes: commit[:message],
    destinations: "Collaborators, Internal testers Android",
    notify_testers: true,
    file: "android/app/build/outputs/apk/stagingRelease/app-stagingRelease.apk",
  )
end

desc 'Build the Android application. and deploy to play store alpha'
private_lane :android_release do
  sh "bash ./pre-build-release.sh"
  gradle(
    task: 'clean',
    project_dir: './android/'
  )
  gradle(
    task: "bundle",
    build_type: 'release',
    project_dir: './android/',
  )
  commit = last_git_commit
  appcenter_upload(
    api_token: ENV["APPCENTER_TOKEN"],
    owner_name: "Lightbase",
    app_name: "lightbaseTemplate-android",
    release_notes: commit[:message],
    file: "android/app/build/outputs/bundle/release/app-release.aab",
    destination_type:'store',
    destinations: "Alpha"
  )
end


desc 'Fetch certificates, build and upload to App Center.'
private_lane :ios_staging do
  pods
  match(app_identifier: 'nl.lightbaseTemplate.app', type: "adhoc", readonly: true)
  commit = last_git_commit
  gym(
    scheme: "lightbaseTemplate",
    workspace: './ios/lightbaseTemplate.xcworkspace',
    clean: 'true',
    silent: 'true',
    configuration: 'Staging',
    build_path: './build',
    output_name: "lightbaseTemplate-staging.ipa",       # specify the name of the .ipa file to generate (including file extension)
    export_method: 'ad-hoc'
  )
  appcenter_upload(
    api_token: ENV["APPCENTER_TOKEN"],
    owner_name: "Lightbase",
    app_name: "lightbaseTemplate-ios",
    release_notes: commit[:message],
    destinations: "Collaborators, Internal testers iOS",
    notify_testers: true,
    ipa: "./lightbaseTemplate-staging.ipa"
  )
end

desc 'Fetch certificates, build and upload to App Center.'
private_lane :ios_release do
  install
  pods
  match(app_identifier: 'nl.lightbaseTemplate.app', type: "appstore", readonly: true)
  sh "bash ./pre-build-release.sh"
  commit = last_git_commit
  gym(
    scheme: "lightbaseTemplate",
    workspace: './ios/lightbaseTemplate.xcworkspace',
    clean: 'true',
    silent: 'true',
    configuration: 'Release',
    build_path: './build',
    output_name: "lightbaseTemplate-prod.ipa",       # specify the name of the .ipa file to generate (including file extension)
    export_method: 'app-store',
  )
  appcenter_upload(
    api_token: ENV["APPCENTER_TOKEN"],
    owner_name: "Lightbase",
    app_name: "lightbaseTemplate-ios",
    release_notes: commit[:message],
    ipa: "./lightbaseTemplate-prod.ipa",
    destination_type:'store',
    destinations: "App Store Connect Users"
  )
end
 