# Path: resources/lib/nvhttp/pairingmanager/abstractpairingmanager.py
# class AbstractPairingManager(object):
#     __metaclass__ = ABCMeta
# 
#     STATE_NOT_PAIRED = 0
#     STATE_PAIRED = 1
#     STATE_PIN_WRONG = 2
#     STATE_FAILED = 3
# 
#     @abstractmethod
#     def pair(self, request_service, server_info, dialog):
#         pass
# 
#     def unpair(self, request_service, server_info):
#         try:
#             request_service.open_http_connection(
#                 request_service.base_url_http + '/unpair?' + request_service.build_uid_uuid_string(),
#                 True
#             )
#         except ValueError:
#             # Unpairing a host needs to be fire and forget
#             pass
# 
#     @staticmethod
#     def get_pair_state(request_service, server_info):
#         if request_service.get_xml_string(server_info, 'PairStatus') != '1':
#             return AbstractPairingManager.STATE_NOT_PAIRED
#         else:
#             return AbstractPairingManager.STATE_PAIRED
# 
#     @staticmethod
#     def generate_pin_string():
#         return '%s%s%s%s' % (random.randint(0, 9), random.randint(0, 9), random.randint(0, 9), random.randint(0, 9))
# 
#     @staticmethod
#     def update_dialog(pin, dialog):
#         pin_message = 'Please enter the PIN: %s' % pin
#         dialog.update(0, pin_message)


import importlib
from resources.lib.nvhttp.pairingmanager.abstractpairingmanager import AbstractPairingManager



class PairingManagerWrapper(AbstractPairingManager):
    def __init__(self, crypto_provider, config_helper, logger):
        # implementation will be lazy loaded when needed
        self._crypto_provider = crypto_provider
        self._config_helper = config_helper
        self._logger = logger
        self._pairing_manager = None

    def pair(self, request_service, server_info, dialog):
        if self._pairing_manager is None:
            self._load_pairing_manager()
        return self._pairing_manager.pair(request_service, server_info, dialog)

    def unpair(self, request_service, server_info):
        if self._pairing_manager is None:
            self._load_pairing_manager()
        return self._pairing_manager.unpair(request_service, server_info)

    def _load_pairing_manager(self):
        try:
            module = importlib.import_module('resources.lib.nvhttp.pairingmanager.advancedpairingmanager')
            class_name = 'AdvancedPairingManager'
        except ImportError, e:
            print 'Could not load advanced pairing manager. Reason: %r' % e

# Path: resources/lib/nvhttp/pairingmanager/abstractpairingmanager.py
# class AbstractPairingManager(object):
#     __metaclass__ = ABCMeta
# 
#     STATE_NOT_PAIRED = 0
#     STATE_PAIRED = 1
#     STATE_PIN_WRONG = 2
#     STATE_FAILED = 3
# 
#     @abstractmethod
#     def pair(self, request_service, server_info, dialog):
#         pass
# 
#     def unpair(self, request_service, server_info):
#         try:
#             request_service.open_http_connection(
#                 request_service.base_url_http + '/unpair?' + request_service.build_uid_uuid_string(),
#                 True
#             )
#         except ValueError:
#             # Unpairing a host needs to be fire and forget
#             pass
# 
#     @staticmethod
#     def get_pair_state(request_service, server_info):
#         if request_service.get_xml_string(server_info, 'PairStatus') != '1':
#             return AbstractPairingManager.STATE_NOT_PAIRED
#         else:
#             return AbstractPairingManager.STATE_PAIRED
# 
#     @staticmethod
#     def generate_pin_string():
#         return '%s%s%s%s' % (random.randint(0, 9), random.randint(0, 9), random.randint(0, 9), random.randint(0, 9))
# 
#     @staticmethod
#     def update_dialog(pin, dialog):
#         pin_message = 'Please enter the PIN: %s' % pin
#         dialog.update(0, pin_message)


# Path: resources/lib/nvhttp/pairingmanager/pairingmanagerwrapper.py
import importlib
from resources.lib.nvhttp.pairingmanager.abstractpairingmanager import AbstractPairingManager




class PairingManagerWrapper(AbstractPairingManager):
    def __init__(self, crypto_provider, config_helper, logger):
        # implementation will be lazy loaded when needed
        self._crypto_provider = crypto_provider
        self._config_helper = config_helper
        self._logger = logger
        self._pairing_manager = None

    def pair(self, request_service, server_info, dialog):
        if self._pairing_manager is None:
            self._load_pairing_manager()
        return self._pairing_manager.pair(request_service, server_info, dialog)

    def unpair(self, request_service, server_info):
        if self._pairing_manager is None:
            self._load_pairing_manager()
        return self._pairing_manager.unpair(request_service, server_info)

    def _load_pairing_manager(self):
        try:
            module = importlib.import_module('resources.lib.nvhttp.pairingmanager.advancedpairingmanager')
            class_name = 'AdvancedPairingManager'
        except ImportError, e:
            print 'Could not load advanced pairing manager. Reason: %r' % e

            module = importlib.import_module('resources.lib.nvhttp.pairingmanager.simplepairingmanager')