import logging

from cinp import client

API_VERSION = '0.9'


class Contractor( object ):
  def __init__( self, host, proxy, username, password, stop_event ):
    self.cinp = client.CInP( host, '/api/v1/', proxy, retry_event=stop_event )

    root, _ = self.cinp.describe( '/api/v1/', retry_count=10 )

    if root[ 'api-version' ] != API_VERSION:
      raise Exception( 'Expected API version "{0}" found "{1}"'.format( API_VERSION, root[ 'api-version' ] ) )

    self.token = self.cinp.call( '/api/v1/Auth/User(login)', { 'username': username, 'password': password }, retry_count=10 )
    self.cinp.setAuth( username, self.token )

  def logout( self ):
    try:
      self.cinp.call( '/api/v1/Auth/User(logout)', { 'token': self.token }, retry_count=10 )
    except client.InvalidSession:
      pass
    self.cinp.setAuth()
    self.token = None

  def getConfig( self, structure_id_list ):
    if not structure_id_list:
      return {}

    logging.info( 'Contractor: getConfig for "{0}"'.format( structure_id_list ) )
    return self.cinp.call( '/api/v1/Building/Structure:{0}:(getConfig)'.format( ':'.join( [ str( i ) for i in structure_id_list ] ) ), {}, force_multi_mode=True, retry_count=10 )

  def updateConfig( self, structure_id, config_value_map ):
    logging.info( 'Contractor: setConfigValues for "{0}"'.format( structure_id ) )
    return self.cinp.call( '/api/v1/Building/Structure:{0}:(updateConfig)'.format( structure_id ), { 'config_value_map': config_value_map }, retry_count=10 )
