require 'uri'

# constants

SHARED_VALUE_CONFIG = 'SHARED_VALUE_CONFIG'
SHARED_VALUE_PACKAGE = 'SHARED_VALUE_PACKAGE'
PACKAGE_JSON_FILE_PATH = './package.json'

STORE_STORE = "store"
STORE_S3 = "s3"

BRAND_ORCHARD = "orchard"
BRAND_AWAL = "awal"
BRAND_SME = "sme"

ENV_PROD = "prod"
ENV_QA = "qa"
ENV_DEV = "dev"

ANDROID_BINARY_TYPE_APK="apk"
ANDROID_BINARY_TYPE_AAB="aab"

# init

def init_configuration()
  init_config_json()
  init_package_json()
end

def init_config_json()
  init_json(config_json_file(), SHARED_VALUE_CONFIG)
end

def init_package_json()
  init_json(PACKAGE_JSON_FILE_PATH, SHARED_VALUE_PACKAGE)
end

def init_json(file_relative_path, shared_value)
  config_hash = load_json(json_path: "#{root_project_directory_path()}/#{file_relative_path}")
  lane_context[shared_value] = JSON.parse(config_hash.to_json, object_class: OpenStruct)
end

def config()
  if lane_context[SHARED_VALUE_CONFIG].nil?
    raise "Call 'init_configuration()'"
  end
  return lane_context[SHARED_VALUE_CONFIG]
end

def package()
  if lane_context[SHARED_VALUE_PACKAGE].nil?
    raise "Call 'init_configuration()'"
  end
  return lane_context[SHARED_VALUE_PACKAGE]
end

# ensure primitives

def ensure_color(value)
  error_message = "'#{value}' is not a color"
  if value.class != String or
    value.length != 7 or
    value[0] != "#"
    raise error_message
  end
  value[1..6].each_char { |character|
    unless "0123456789abcdefABCDEF".include? character
      raise error_message
    end
  }
end

def ensure_project_file(value)
  if value.class != String or
    value.length == 0
    raise "File path is not string or empty"
  end
  file_path = "#{root_project_directory_path()}/#{value}"
  if !File.exist?(file_path) or !File.file?(file_path)
    raise "File '#{file_path}' does not exist"
  end
end

def ensure_system_file(value)
  if value.class != String or
    value.length == 0
    raise "File path is not string or empty"
  end
  if !File.exist?(value) or !File.file?(value)
    raise "File '#{value}' does not exist"
  end
end

def ensure_not_empty_string(value)
  if value.class != String or
    value.length == 0
    raise "'#{value}' is not an empty string"
  end
end

def ensure_integer(value)
  if value.class != Integer
    raise "'#{value}' is not an integer"
  end
end

def ensure_brand(value)
  ensure_not_empty_string(value)
  if value != BRAND_ORCHARD && value != BRAND_AWAL && value != BRAND_SME
    raise "'#{value}' is not a correct brand"
  end
end

def ensure_env(value)
  ensure_not_empty_string(value)
  if value != ENV_PROD && value != ENV_QA && value != ENV_DEV
    raise "'#{value}' is not a correct env"
  end
end

def ensure_store(value)
  ensure_not_empty_string(value)
  if value != STORE_STORE && value != STORE_S3
    raise "'#{value}' is not a correct store"
  end
end

def ensure_android_binary_type(value)
  ensure_not_empty_string(value)
  if value != ANDROID_BINARY_TYPE_APK && value != ANDROID_BINARY_TYPE_AAB
    raise "'#{value}' is not a correct store"
  end
end

# ensure configuration

def ensure_configuration()
  init_configuration()
  #ensure_envs()
  #ensure_package_json()
  #ensure_brand_config()
end

def ensure_envs()
  ensure_brand(brand())
  ensure_env(environment())
  ensure_store(store())
  ensure_android_binary_type(android_binary_type())

  if store() == STORE_S3
    ensure_not_empty_string(aws_access_key())
    ensure_not_empty_string(aws_secret_access_key())
    ensure_not_empty_string(aws_region())
    ensure_not_empty_string(aws_bucket())
    ensure_not_empty_string(aws_binaries_name_prefix())
  end

  ensure_not_empty_string(code_push_standalone_access_key())

  ensure_not_empty_string(sentry_auth_token())
  ensure_not_empty_string(sentry_organization_name())
  ensure_not_empty_string(qa_sentry_project_name())
  ensure_not_empty_string(prod_sentry_project_name())

  ensure_not_empty_string(xcode_path())

  ensure_system_file(google_play_json_file_path())
  ensure_system_file(google_play_keystore_file_path())
  ensure_not_empty_string(google_play_keystore_file_password())
  ensure_not_empty_string(google_play_keystore_key_alias())
  ensure_not_empty_string(google_play_keystore_key_password())

  ensure_not_empty_string(app_store_team_id())
  ensure_not_empty_string(app_store_api_key_id())
  ensure_not_empty_string(app_store_api_key_issuer_id())
  ensure_system_file(app_store_api_key_file_path())
  ensure_system_file(app_store_certificate_file_path())
  ensure_not_empty_string(app_store_certificate_password())
  ensure_not_empty_string(app_store_certificate_signing_identity_name())
  # TODO: fix. conflicts with not supported configuration
  #ensure_system_file(app_store_profile_file_path())
end

def ensure_package_json()
  ensure_not_empty_string(application_version())
  ensure_not_empty_string(git_repository_url())
end

def ensure_brand_config()
  ensure_project_file(config_json_file())

  ensure_not_empty_string(company_name())
  ensure_not_empty_string(support_email())
  ensure_not_empty_string(user_brand_fallback())

  ensure_color(splash_background_color())
  ensure_color(launcher_adaptive_icon_background_gradient_start_color())
  ensure_color(launcher_adaptive_icon_background_gradient_end_color())
  ensure_integer(launcher_adaptive_icon_background_gradient_angle())
  ensure_color(notification_icon_color())
  ensure_color(status_bar_color())

  ensure_not_empty_string(deep_link_firebase_host())
  ensure_not_empty_string(deep_link_insights_host())

  ensure_not_empty_string(code_push_server_url())

  ensure_not_empty_string(application_name())
  ensure_not_empty_string(redirection_scheme())
  ensure_not_empty_string(redirection_host())
  ensure_integer(redirection_port())
  ensure_not_empty_string(android_application_id())
  ensure_not_empty_string(ios_bundle_id())

  ensure_not_empty_string(android_auth0_sdk_callback_scheme())
  ensure_not_empty_string(android_auth0_sdk_callback_host())
  ensure_not_empty_string(android_auth0_sdk_callback_path())

  ensure_project_file(launcher_icon())
  ensure_project_file(launcher_adaptive_icon())
  ensure_project_file(launcher_monochrome_icon())
  ensure_project_file(notification_icon())
  ensure_project_file(splash_image())
  ensure_project_file(android_google_services_json_file())
  ensure_project_file(ios_google_services_info_plist_file())
end

# env

def brand()
  return ENV[ENV_BRAND]
end

def environment()
  return ENV[ENV_ENVIRONMENT]
end

def store()
  return ENV[ENV_STORE]
end

def android_binary_type()
  return ENV[ENV_ANDROID_BINARY_TYPE]
end


def aws_access_key()
  return ENV["AWS_ACCESS_KEY"]
end

def aws_secret_access_key()
  return ENV["AWS_SECRET_ACCESS_KEY"]
end

def aws_region()
  return ENV["AWS_REGION"]
end

def aws_bucket()
  return ENV["AWS_BUCKET"]
end

def aws_binaries_name_prefix()
  return ENV["AWS_BINARIES_NAME_PREFIX"]
end


def code_push_standalone_access_key()
  return ENV["CODE_PUSH_STANDALONE_ACCESS_KEY"]
end


def sentry_auth_token()
  return ENV["SENTRY_AUTH_TOKEN"]
end

def sentry_organization_name()
  return ENV["SENTRY_ORGANIZATION_NAME"]
end

def qa_sentry_project_name()
  return ENV["QA_SENTRY_PROJECT_NAME"]
end

def prod_sentry_project_name()
  return ENV["PROD_SENTRY_PROJECT_NAME"]
end


def xcode_path()
  return ENV["XCODE_PATH"]
end


def google_play_json_file_path()
  return ENV["#{brand().upcase}_GOOGLE_PLAY_JSON_FILE_PATH"]
end

def google_play_keystore_file_path()
  return ENV["#{brand().upcase}_#{environment().upcase}_GOOGLE_PLAY_KEYSTORE_FILE_PATH"]
end

def google_play_keystore_file_password()
  return ENV["#{brand().upcase}_#{environment().upcase}_GOOGLE_PLAY_KEYSTORE_FILE_PASSWORD"]
end

def google_play_keystore_key_alias()
  return ENV["#{brand().upcase}_#{environment().upcase}_GOOGLE_PLAY_KEYSTORE_KEY_ALIAS"]
end

def google_play_keystore_key_password()
  return ENV["#{brand().upcase}_#{environment().upcase}_GOOGLE_PLAY_KEYSTORE_KEY_PASSWORD"]
end


def app_store_team_id()
  return ENV["#{brand().upcase}_APP_STORE_TEAM_ID"]
end

def app_store_api_key_id()
  return ENV["#{brand().upcase}_APP_STORE_API_KEY_ID"]
end

def app_store_api_key_issuer_id()
  return ENV["#{brand().upcase}_APP_STORE_API_KEY_ISSUER_ID"]
end

def app_store_api_key_file_path()
  return ENV["#{brand().upcase}_APP_STORE_API_KEY_FILE_PATH"]
end

def app_store_certificate_file_path()
  return ENV["#{brand().upcase}_APP_STORE_CERTIFICATE_FILE_PATH"]
end

def app_store_certificate_password()
  return ENV["#{brand().upcase}_APP_STORE_CERTIFICATE_PASSWORD"]
end

def app_store_certificate_signing_identity_name()
  return ENV["#{brand().upcase}_APP_STORE_CERTIFICATE_SIGNING_IDENTITY_NAME"]
end

def app_store_profile_file_path()
  return ENV["#{brand().upcase}_#{environment().upcase}_#{store().upcase}_APP_STORE_PROFILE_FILE_PATH"]
end

# package.json

def application_version()
  return package().version
end

def git_repository_url()
  return package().repository.url
end

# brand config

def environment_config()
  return config().environment[environment()]
end


def config_json_file()
  return "./brands/#{brand()}/config.json"
end

def company_name()
  return config().company.name
end

def support_email()
  return config().company.supportEmail
end

def user_brand_fallback()
  return config().company.userBrandFallback
end


def splash_background_color()
  return config().color.splashBackground
end

def launcher_adaptive_icon_background_gradient_start_color()
  return config().color.adaptiveIconBackgroundGradientStart
end

def launcher_adaptive_icon_background_gradient_end_color()
  return config().color.adaptiveIconBackgroundGradientEnd
end

def launcher_adaptive_icon_background_gradient_angle()
  return config().color.adaptiveIconBackgroundGradientAngle
end

def notification_icon_color()
  return config().color.notificationIcon
end

def status_bar_color()
  return config().color.statusBar
end


def deep_link_firebase_host()
  return config().deepLink.firebaseHost
end

def deep_link_insights_host()
  return config().deepLink.insightsHost
end


def code_push_server_url()
  return config().codePush.serverUrl
end


def application_name()
  return environment_config().applicationName
end

def redirection_scheme()
  return environment_config().redirection.scheme
end

def redirection_host()
  return environment_config().redirection.host
end

def redirection_port()
  return environment_config().redirection.port
end

def android_application_id()
  return environment_config().android.applicationId
end

def ios_bundle_id()
  return environment_config().ios.bundleIdentifier
end

def android_auth0_sdk_callback_scheme()
  url = URI.parse(environment_config().auth0Sdk.androidCallbackUrl)
  return url.scheme
end

def android_auth0_sdk_callback_host()
  url = URI.parse(environment_config().auth0Sdk.androidCallbackUrl)
  return url.host
end

def android_auth0_sdk_callback_path()
  url = URI.parse(environment_config().auth0Sdk.androidCallbackUrl)
  return url.path
end


def launcher_icon()
  return "./brands/#{brand()}/icons/launcher/#{environment()}.png"
end

def launcher_adaptive_icon()
  return "./brands/#{brand()}/icons/launcher/adaptive/#{environment()}.png"
end

def launcher_monochrome_icon()
  return "./brands/#{brand()}/icons/launcher/monochrome/#{environment()}.png"
end

def notification_icon()
  return "./brands/#{brand()}/icons/notification.png"
end

def splash_image()
  return "./brands/#{brand()}/icons/splash.png"
end

def android_google_services_json_file()
  return "./brands/#{brand()}/android/google-services/#{environment()}.json"
end

def ios_google_services_info_plist_file()
  return "./brands/#{brand()}/ios/GoogleService-Info/#{environment()}.plist"
end
