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

platform :android do

    @variants = ['arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64']
    @apkPath = ENV["FASTLANE_APK_PATH"]

    def checkAPKFileExists(version, checkSha)
        allExist = true
        @variants.each { |variant|
            file_name = @apkPath+"VLC-Android-"+version+"-"+variant+".apk"
            # checking apks
            if (!File.file?("../"+file_name))
               allExist = false
               UI.error "File "+file_name+" doesn't exist"
            end
            if checkSha
                # checking sha256
                if (!File.file?("../"+file_name+".sha256"))
                   allExist = false
                   UI.error "File "+file_name+".sha256 doesn't exist"
                end
            end
        }
        if !allExist
            UI.user_error!("Missing files to upload. See above")
        end
    end

    def getFileList(version)
        files = Array.new
        @variants.each { |variant|
            files.push(@apkPath+"VLC-Android-"+version+"-"+variant+".apk")
        }
        return files
    end
    def getAabList()
        files = Array.new
        files.push(@apkPath+"app-release-signed.aab")
        return files
    end

  desc "Runs all the tests"
  lane :test do
    gradle(task: "test")
  end

  desc "Deploy a new version to the Google Play"
  lane :deploy_release do |options|
    checkAPKFileExists options[:version], false
    upload_to_play_store(skip_upload_metadata:true, skip_upload_changelogs:true, skip_upload_images:true, skip_upload_screenshots:true, release_status:"draft", apk_paths:getFileList(options[:version]))
    slack(message: 'Successfully created a new draft for '+options[:version])
  end

  lane :test_files do |options|
    sh("pwd")
    checkAPKFileExists options[:version], false
    getFileList(options[:version]).each { |version|
        UI.message version
    }

  end

  lane :deploy_screenshots do |options|
    upload_to_play_store(skip_upload_metadata:true, skip_upload_changelogs:true, skip_upload_images:true, skip_upload_apk:true, release_status:"draft", version_code:options[:version])
    slack(message: 'Successfully uploaded screenshots to Play Store')
  end

  lane :deploy_descriptions do |options|
    upload_to_play_store(skip_upload_screenshots:true, skip_upload_changelogs:true, skip_upload_images:true, skip_upload_apk:true, release_status:"draft", version_code:options[:version])
    slack(message: 'Successfully uploaded translations to Play Store')
  end

  lane :deploy_beta do |options|
    checkAPKFileExists options[:version], false
    upload_to_play_store(skip_upload_metadata:true, skip_upload_changelogs:true, track: 'beta', skip_upload_images:true, skip_upload_screenshots:true, skip_upload_apk:false, release_status:"draft", apk_paths:getFileList(options[:version]))
    slack(message: 'Successfully created a new beta draft for '+options[:version])
  end

  lane :deploy_internal do |options|
    checkAPKFileExists options[:version], false
    upload_to_play_store(skip_upload_metadata:true, skip_upload_changelogs:true, track: 'internal', skip_upload_images:true, skip_upload_screenshots:true, skip_upload_apk:false, release_status:"draft", aab_paths:getAabList())
    upload_to_play_store(skip_upload_metadata:true, skip_upload_changelogs:true, track: 'internal', skip_upload_images:true, skip_upload_screenshots:true, skip_upload_apk:false, release_status:"draft", apk_paths:getFileList(options[:version]))
    slack(message: 'Successfully created a new internal draft for '+options[:version])
  end

  lane :screenshots do |options|
    gradle(task: 'assemble', build_type: 'Debug', project_dir:'../..')
    gradle(task: ':application:app:assemble', build_type: 'DebugAndroidTest', project_dir:'../..')
    capture_android_screenshots(app_apk_path:"../../application/app/build/outputs/apk/debug/VLC-Android-"+options[:version]+"-debug-all.apk", tests_apk_path:"../../application/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk")
  end

  lane :screenshots_seven do |options|
    gradle(task: 'assemble', build_type: 'Debug', project_dir:'../..')
    gradle(task: ':application:app:assemble', build_type: 'DebugAndroidTest', project_dir:'../..')
    capture_android_screenshots(device_type:"sevenInch" ,app_apk_path:"../../application/app/build/outputs/apk/debug/VLC-Android-"+options[:version]+"-debug-all.apk", tests_apk_path:"../../application/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk")
  end

  lane :screenshots_ten do |options|
    gradle(task: 'assemble', build_type: 'Debug', project_dir:'../..')
    gradle(task: ':application:app:assemble', build_type: 'DebugAndroidTest', project_dir:'../..')
    capture_android_screenshots(device_type:"tenInch" ,app_apk_path:"../../application/app/build/outputs/apk/debug/VLC-Android-"+options[:version]+"-debug-all.apk", tests_apk_path:"../../application/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk")
  end

  lane :screenshots_tv do |options|
    gradle(task: 'assemble', build_type: 'Debug', project_dir:'../..')
    gradle(task: ':application:app:assemble', build_type: 'DebugAndroidTest', project_dir:'../..')
    capture_android_screenshots(device_type:"tv",use_tests_in_classes:'org.videolan.vlc.TvScreenhotsInstrumentedTest', app_apk_path:"../../application/app/build/outputs/apk/debug/VLC-Android-"+options[:version]+"-debug-all.apk", tests_apk_path:"../../application/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk")
  end

  lane :huawei_info do
      huawei_appgallery_connect_get_app_info(
          client_id: ENV["HUAWEI_CLIENT_ID"],
          client_secret: ENV["HUAWEI_CLIENT_SECRET"],
          app_id: ENV["HUAWEI_APP_ID"]
      )
    end

  lane :deploy_huawei do |options|
    huawei_appgallery_connect(
        client_id: ENV["HUAWEI_CLIENT_ID"],
        client_secret: ENV["HUAWEI_CLIENT_SECRET"],
        app_id: ENV["HUAWEI_APP_ID"],
        apk_path: @apkPath+"VLC-Android-"+options[:version]+"-arm64-v8a.apk",
        submit_for_review: false,
    )
    slack(message: 'Successfully created a new draft for '+options[:version]+' on Huawei AppGallery')
  end

  lane :deploy_ftp do |options|

    checkAPKFileExists options[:version], true

    puts "All files are here. Uploading to FTP"
    @variants.each { |variant|
        ftp(
            host: ENV["VIDEOLAN_FTP_HOST"],
            username: 'anonymous',
            password: '',
            upload:{
                src: @apkPath+"VLC-Android-"+options[:version]+"-"+variant+".apk",
                dest: "/incoming/"+options[:version]
            }
        )
        ftp(
            host: ENV["VIDEOLAN_FTP_HOST"],
            username: 'anonymous',
            password: '',
            upload:{
                src: @apkPath+"VLC-Android-"+options[:version]+"-"+variant+".apk.sha256",
                dest: "/incoming/"+options[:version]
            }
        )
    }

    slack(message: 'Successfully uploaded '+options[:version]+' to FTP')
  end

end
