CODE_PUSH_DEPLOYMENT_PRODUCTION = 'Production'
CODE_PUSH_DEPLOYMENT_QA = 'QA'
CODE_PUSH_APP_ANDROID = 'android'
CODE_PUSH_APP_IOS = 'ios'

CODE_PUSH_STANDALONE_APP_NAME_ANDROID = 'OrchardGoAndroid'
CODE_PUSH_STANDALONE_APP_NAME_IOS = 'OrchardGoIOS'

def code_push_upload_js_bundle_for_production_for_android()
  code_push_upload_js_bundle_for_android(CODE_PUSH_DEPLOYMENT_PRODUCTION)
end

def code_push_upload_js_bundle_for_qa_for_android()
  code_push_upload_js_bundle_for_android(CODE_PUSH_DEPLOYMENT_QA)
end

def code_push_upload_js_bundle_for_android(deployment_name)
  code_push_upload_js_bundle(
    deployment_name,
    CODE_PUSH_STANDALONE_APP_NAME_ANDROID,
    ANDROID_BUNDLE_FILE,
    ANDROID_ASSETS_DIRECTORY,
    ANDROID_CODE_PUSH_BUILD_DIRECTORY,
    ANDROID_CODE_PUSH_BUNDLE_NAME)
end

def code_push_upload_js_bundle_for_production_for_ios()
  code_push_upload_js_bundle_for_ios(CODE_PUSH_DEPLOYMENT_PRODUCTION)
end

def code_push_upload_js_bundle_for_qa_for_ios()
  code_push_upload_js_bundle_for_ios(CODE_PUSH_DEPLOYMENT_QA)
end

def code_push_upload_js_bundle_for_ios(deployment_name)
  code_push_upload_js_bundle(
    deployment_name,
    CODE_PUSH_STANDALONE_APP_NAME_IOS,
    IOS_BUNDLE_FILE,
    IOS_ASSETS_DIRECTORY,
    IOS_CODE_PUSH_BUILD_DIRECTORY,
    IOS_CODE_PUSH_BUNDLE_NAME)
end

def code_push_upload_js_bundle(
  deployment_name,
  code_push_standalone_app_name,
  original_bundle_file_path,
  original_assets_directory_path,
  code_push_build_directory,
  code_push_bundle_name)
  Dir.chdir(root_project_directory_path()) do
    FileUtils.mkdir_p(code_push_build_directory)
    FileUtils.cp_r("#{original_assets_directory_path}/.", code_push_build_directory)
    FileUtils.cp(original_bundle_file_path, "#{code_push_build_directory}/#{code_push_bundle_name}")

    code_push_standalone_login
    code_push_standalone_release(code_push_standalone_app_name, code_push_build_directory, deployment_name)
  end
end

def code_push_release_message()
  commit_sha = sh 'git rev-parse HEAD'
  commit_sha = commit_sha.strip
  url_without_git_suffix = git_repository_url()[0...-4]
  url = "#{url_without_git_suffix}/commits/#{commit_sha}"
  commit_message = sh "git log -n 1 --pretty=format:\'%s\'"
  commit_message = commit_message.strip.delete("'\"")
  message = [
    "Commit url:\n\n",
    "#{url}\n\n",
    "Commit sha:\n\n",
    "\t#{commit_sha}\n\n",
    "Commit message:\n\n",
    "\t#{commit_message}\n\n",
  ].join("")
  return message
end

def code_push_promote_js_bundle(app)
  Dir.chdir(root_project_directory_path()) do
    code_push_standalone_login
    code_push_standalone_promote(code_push_standalone_app_name(app))
  end
end

def code_push_standalone_app_name(app)
  if app == CODE_PUSH_APP_ANDROID
    return CODE_PUSH_STANDALONE_APP_NAME_ANDROID
  end
  if app == CODE_PUSH_APP_IOS
    return CODE_PUSH_STANDALONE_APP_NAME_IOS
  end
  raise "Unreachable situation. Unknown app #{app}"
end

def code_push_standalone_login()
  ENV["LOCALAPPDATA"]="./"

  code_push_config_shadow_file = "./.code-push.config.prod.shadow"
  code_push_config_file = "./.code-push.config"

  content = File.read(code_push_config_shadow_file)
  replaced = content.gsub("INSERT_ACCESS_KEY", code_push_standalone_access_key())

  File.write(code_push_config_file, replaced)

  sh "DEBUG=* yarn code-push-standalone whoami"
  sh "DEBUG=* yarn code-push-standalone app ls"
end

def code_push_standalone_release(app_name, code_push_build_directory, deployment_name)
  sh [
    'yarn code-push-standalone release',
    app_name,
    code_push_build_directory,
    application_version(),
    '--deploymentName', deployment_name,
    '--description', "'#{code_push_release_message()}'",
    '--mandatory',
    '--noDuplicateReleaseError'
  ]
end

def code_push_standalone_promote(app_name)
  sh [
    'yarn code-push-standalone promote', app_name, CODE_PUSH_DEPLOYMENT_QA, CODE_PUSH_DEPLOYMENT_PRODUCTION,
    '--noDuplicateReleaseError'
  ]
end

def code_push_history(project, env, format)
  Dir.chdir(root_project_directory_path()) do
    code_push_standalone_login

    puts "History: #{project} #{env}"
    sh [
      'DEBUG=* yarn code-push-standalone deployment history', project, env, '--format', format
    ]
  end
end

def code_push_enable_bundle_release(project, env, target, enable)
  transformedEnable = enable.to_s.strip.downcase
  disabled = ""
  if transformedEnable == "true"
    disabled = "false"
  elsif transformedEnable == "false"
    disabled = "true"
  else
    raise "Invalid value for 'enable': must be 'true' or 'false', got '#{enable}'"
  end

  Dir.chdir(root_project_directory_path()) do
    code_push_standalone_login

    sh [
      'DEBUG=* yarn code-push-standalone patch', project, env,
      '--targetRelease', target,
      '--disabled', disabled
    ]
  end
end
