# -*- coding: utf-8 -*- #
# Copyright 2016 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Supporting libraries for the config-helper command."""


from googlecloudsdk.core import config


class ConfigHelperResult(object):
  """The result of the gcloud config config-helper command that gets serialzied.

  Attributes:
    credential: Credential, The OAuth2 credential information.
    configuration: Configuration, Local Cloud SDK configuration information.
    sentinels: Sentinels, Paths to various sentinel files.
  """

  def __init__(self, credential, active_configuration, properties):
    self.credential = Credential(credential)
    self.configuration = Configuration(active_configuration, properties)
    self.sentinels = Sentinels(config.Paths().config_sentinel_file)


class Credential(object):
  """Holder for credential data.

  Attributes:
    access_token: str, The current OAuth2 access token.
    token_expiry: str, The expiry time in UTC as an RFC3339 formatted string.
    id_token: str, The current OAuth2 identity token, if present.
    regional_access_boundary: str, The current regional access boundary, if
      present. This is a hex-encoded bitmap string (e.g. '0xA30') representing a
      list of regions the credential is allowed to access.
    regional_access_boundary_expiry: str, The expiry time of the regional access
      boundary as an RFC3339 formatted string, if present.
  """
  _EXPIRY_FORMAT = '%Y-%m-%dT%H:%M:%SZ'

  def __init__(self, cred):
    self.access_token = cred.token
    expiry = getattr(cred, 'expiry', None)

    self.token_expiry = (
        expiry.strftime(Credential._EXPIRY_FORMAT) if expiry else None)

    # The cache blanks the token_response field, so if it's present that
    # indicates there's either no cache entry, or we just refreshed tokens.
    # Either way, the response is fresher.
    token_response = getattr(cred, 'token_response', None)

    if token_response:
      id_token = token_response.get('id_token', None)
    else:
      id_token = getattr(cred, 'id_tokenb64', None)

    self.id_token = id_token
    self.regional_access_boundary = getattr(
        cred, 'regional_access_boundary', None
    )
    rab_expiry = getattr(cred, 'regional_access_boundary_expiry', None)
    self.regional_access_boundary_expiry = (
        rab_expiry.strftime(Credential._EXPIRY_FORMAT) if rab_expiry else None
    )


class Configuration(object):
  """Holder for configuration data.

  Attributes:
    active_configuration: str, The name of the active configuration.
    properties: {str: {str: str}}, A dict of section names to properties and
      values.
  """

  def __init__(self, active_configuration, properties):
    self.active_configuration = active_configuration
    self.properties = properties


class Sentinels(object):
  """Holder for sentinel file locations.

  Attributes:
    config_sentinel: str, The path to the sentinel that indicates changes were
      made to properties or the active configuration.
  """

  def __init__(self, config_sentinel):
    self.config_sentinel = config_sentinel
