# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import print_function
from __future__ import absolute_import
import contextlib
import logging
import os

import py_utils
from py_utils import binary_manager
from py_utils import cloud_storage
from py_utils import dependency_util
import dependency_manager
from dependency_manager import base_config

from devil import devil_env


from telemetry.core import exceptions
from telemetry.core import util


TELEMETRY_PROJECT_CONFIG = os.path.join(
    util.GetTelemetryDir(), 'telemetry', 'binary_dependencies.json')


CHROME_BINARY_CONFIG = os.path.join(util.GetCatapultDir(), 'common', 'py_utils',
                                    'py_utils', 'chrome_binaries.json')


SUPPORTED_DEP_PLATFORMS = (
    'linux_aarch64', 'linux_x86_64', 'linux_armv7l', 'linux_mips',
    'mac_x86_64', 'mac_arm64',
    'win_x86', 'win_AMD64',
    'android_arm64-v8a', 'android_armeabi-v7a', 'android_arm', 'android_x64',
    'android_x86'
)

PLATFORMS_TO_DOWNLOAD_FOLDER_MAP = {
    'linux_aarch64': 'bin/linux/aarch64',
    'linux_x86_64': 'bin/linux/x86_64',
    'linux_armv7l': 'bin/linux/armv7l',
    'linux_mips': 'bin/linux/mips',
    'mac_x86_64': 'bin/mac/x86_64',
    'mac_arm64': 'bin/mac/arm64',
    'win_x86': 'bin/win/x86',
    'win_AMD64': 'bin/win/AMD64',
    'android_arm64-v8a': 'bin/android/arm64-v8a',
    'android_armeabi-v7a': 'bin/android/armeabi-v7a',
    'android_arm': 'bin/android/arm',
    'android_x64': 'bin/android/x64',
    'android_x86': 'bin/android/x86',
}

NoPathFoundError = dependency_manager.NoPathFoundError
CloudStorageError = dependency_manager.CloudStorageError


_binary_manager = None
_installed_helpers = set()


TELEMETRY_BINARY_BASE_CS_FOLDER = 'binary_dependencies'
TELEMETRY_BINARY_CS_BUCKET = cloud_storage.PUBLIC_BUCKET

def NeedsInit():
  return not _binary_manager


def InitDependencyManager(client_configs):
  if GetBinaryManager():
    raise exceptions.InitializationError(
        'Trying to re-initialize the binary manager with config %s'
        % client_configs)
  configs = []
  if client_configs:
    configs += client_configs
  configs += [TELEMETRY_PROJECT_CONFIG, CHROME_BINARY_CONFIG]
  SetBinaryManager(binary_manager.BinaryManager(configs))

  devil_env.config.Initialize()


@contextlib.contextmanager
def TemporarilyReplaceBinaryManager(manager):
  old_manager = GetBinaryManager()
  try:
    SetBinaryManager(manager)
    yield
  finally:
    SetBinaryManager(old_manager)


def GetBinaryManager():
  return _binary_manager


def SetBinaryManager(manager):
  global _binary_manager # pylint: disable=global-statement
  _binary_manager = manager


def _IsChromeOSLocalMode(os_name):
  """Determines if we're running telemetry on a Chrome OS device.

  Used to differentiate local mode (telemetry running on the CrOS DUT) from
  remote mode (running telemetry on another platform that communicates with
  the CrOS DUT over SSH).