# coding=utf-8

from wikilife_utils.patterns import USERNAME_PATTERN_C, PIN_PATTERN_C


class UserServiceException(Exception):
    pass


class UserService(object):

    _user_dao = None

    def __init__(self, logger, user_dao):
        self._logger = logger
        self._user_dao = user_dao
    
    def get_user_by_id(self, user_id):
        return self._user_dao.get_user_by_id(user_id)
    
    def add_user(self, user_name, pin):
        self._validate_user_name(user_name)
        self._validate_pin(pin)

        if not self.is_username_available(user_name):
            raise UserServiceException("User name is not available")

        created_user_id = self._user_dao.insert_user(user_name, pin)
        return created_user_id

    def add_autogenerated_user(self, user_name_prefix):
        autogenerated_pin = "123456"
        created_user_id = self._user_dao.insert_autogenerated_user(autogenerated_pin, user_name_prefix)
        return created_user_id

    def edit_user_name(self, dto):
        """
        Edit a user user_name
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_user_name":"",
        }
        Raises: UserServiceException
        """
        user_id = dto["user_id"]
        user_pin = dto["pin"]
        new_user_name = dto["new_user_name"]
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        self.validate_user_name(new_user_name)
        user["user_name"] = new_user_name
        self._user_dao.save_user(user)

    def edit_pin(self, dto):
        """
        Edit a user pin
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_pin":"",
        }
        Raises: UserServiceException
        """
        user_id = dto["user_id"]
        user_pin = dto["pin"]
        new_user_pin = dto["new_pin"]
        user = self._find_user_by_id_and_pin(user_id, user_pin)

        if not new_user_pin or len(new_user_pin) == 0:
            raise UserServiceException("Pin is mandatory")

        user["pin"] = new_user_pin
        self._user_dao.save_user(user)

    def remove_user(self, user_id):
        if self._user_dao.get_user_by_id(user_id) == None:
            raise UserServiceException("User id '%s' not found" %user_id)

        self._user_dao.delete_user(user_id)

    def is_username_available(self, user_name):
        self._validate_user_name(user_name)
        return self._user_dao.get_user_by_user_name(user_name) == None

    def is_valid_user(self, user_id):
        """
        user_id: String
        """
        user = self._user_dao.get_user_by_id(user_id)
        return user != None and user["status"] == 1

    def _validate_user_name(self, user_name):
        """
        user_name: String
        """
        if not user_name or len(user_name) < 4:
            raise UserServiceException("User name too short")

        if not USERNAME_PATTERN_C.match(user_name):
            raise UserServiceException("User name must be alfanumeric and -._ chars")

    def _validate_pin(self, pin):
        """
        pin: String
        """
        if not pin or len(pin) < 3:
            raise UserServiceException("Pin too short")

        if not PIN_PATTERN_C.match(pin):
            raise UserServiceException("Pin must be alfanumeric only chars")

    def validate_user(self, user_id, user_name, user_pin):
        """
        user_id: String
        user_name: String
        user_pin: String
        """
        user = self._find_user_by_id_and_pin(user_id, user_pin)

        if user["user_name"] != user_name:
            raise UserServiceException("Invalid user name")

    '''
    def _find_user_by_id_and_pin(self, user_id, user_pin):
        user = self._user_dao.get_user_by_id(user_id)

        if user == None or user["pin"] != user_pin:
            raise UserServiceException("Invalid wikilife User ID or PIN")

        return user
    '''

    '''
    def find_user(self, user_credentials):
        """
        Finds a user by user_name and pin
        user_credentials: Dictionary
        {
            "user_name":"joaquin",
            "pin":"4449"
        }
        Returns: user
        Raises: UserServiceException if user not found
        """
        user_name = user_credentials["user_name"]
        user_pin = user_credentials["pin"]
        user = self._user_dao.get_user_by_user_name(user_name)

        if user != None and user["pin"] == user_pin:
            return user
        else:
            raise UserServiceException("Invalid wikilife Username or PIN")
    '''

2076419756abf03414fb85371b9a5b130e3664ea
<mask token>


class UserService(object):
    <mask token>
    <mask token>

    def get_user_by_id(self, user_id):
        return self._user_dao.get_user_by_id(user_id)
    <mask token>

    def add_autogenerated_user(self, user_name_prefix):
        autogenerated_pin = '123456'
        created_user_id = self._user_dao.insert_autogenerated_user(
            autogenerated_pin, user_name_prefix)
        return created_user_id

    def edit_user_name(self, dto):
        """
        Edit a user user_name
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_user_name":"",
        }
        Raises: UserServiceException
        """
        user_id = dto['user_id']
        user_pin = dto['pin']
        new_user_name = dto['new_user_name']
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        self.validate_user_name(new_user_name)
        user['user_name'] = new_user_name
        self._user_dao.save_user(user)

    def edit_pin(self, dto):
        """
        Edit a user pin
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_pin":"",
        }
        Raises: UserServiceException
        """
        user_id = dto['user_id']
        user_pin = dto['pin']
        new_user_pin = dto['new_pin']
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        if not new_user_pin or len(new_user_pin) == 0:
            raise UserServiceException('Pin is mandatory')
        user['pin'] = new_user_pin
        self._user_dao.save_user(user)

    def remove_user(self, user_id):
        if self._user_dao.get_user_by_id(user_id) == None:
            raise UserServiceException("User id '%s' not found" % user_id)
        self._user_dao.delete_user(user_id)

    def is_username_available(self, user_name):
        self._validate_user_name(user_name)
        return self._user_dao.get_user_by_user_name(user_name) == None

    def is_valid_user(self, user_id):
        """
        user_id: String
        """
        user = self._user_dao.get_user_by_id(user_id)
        return user != None and user['status'] == 1

    def _validate_user_name(self, user_name):
        """
        user_name: String
        """
        if not user_name or len(user_name) < 4:
            raise UserServiceException('User name too short')
        if not USERNAME_PATTERN_C.match(user_name):
            raise UserServiceException(
                'User name must be alfanumeric and -._ chars')

    def _validate_pin(self, pin):
        """
        pin: String
        """
        if not pin or len(pin) < 3:
            raise UserServiceException('Pin too short')
        if not PIN_PATTERN_C.match(pin):
            raise UserServiceException('Pin must be alfanumeric only chars')

    def validate_user(self, user_id, user_name, user_pin):
        """
        user_id: String
        user_name: String
        user_pin: String
        """
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        if user['user_name'] != user_name:
            raise UserServiceException('Invalid user name')
    <mask token>
    <mask token>

<mask token>


class UserService(object):
    _user_dao = None

    def __init__(self, logger, user_dao):
        self._logger = logger
        self._user_dao = user_dao

    def get_user_by_id(self, user_id):
        return self._user_dao.get_user_by_id(user_id)

    def add_user(self, user_name, pin):
        self._validate_user_name(user_name)
        self._validate_pin(pin)
        if not self.is_username_available(user_name):
            raise UserServiceException('User name is not available')
        created_user_id = self._user_dao.insert_user(user_name, pin)
        return created_user_id

    def add_autogenerated_user(self, user_name_prefix):
        autogenerated_pin = '123456'
        created_user_id = self._user_dao.insert_autogenerated_user(
            autogenerated_pin, user_name_prefix)
        return created_user_id

    def edit_user_name(self, dto):
        """
        Edit a user user_name
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_user_name":"",
        }
        Raises: UserServiceException
        """
        user_id = dto['user_id']
        user_pin = dto['pin']
        new_user_name = dto['new_user_name']
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        self.validate_user_name(new_user_name)
        user['user_name'] = new_user_name
        self._user_dao.save_user(user)

    def edit_pin(self, dto):
        """
        Edit a user pin
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_pin":"",
        }
        Raises: UserServiceException
        """
        user_id = dto['user_id']
        user_pin = dto['pin']
        new_user_pin = dto['new_pin']
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        if not new_user_pin or len(new_user_pin) == 0:
            raise UserServiceException('Pin is mandatory')
        user['pin'] = new_user_pin
        self._user_dao.save_user(user)

    def remove_user(self, user_id):
        if self._user_dao.get_user_by_id(user_id) == None:
            raise UserServiceException("User id '%s' not found" % user_id)
        self._user_dao.delete_user(user_id)

    def is_username_available(self, user_name):
        self._validate_user_name(user_name)
        return self._user_dao.get_user_by_user_name(user_name) == None

    def is_valid_user(self, user_id):
        """
        user_id: String
        """
        user = self._user_dao.get_user_by_id(user_id)
        return user != None and user['status'] == 1

    def _validate_user_name(self, user_name):
        """
        user_name: String
        """
        if not user_name or len(user_name) < 4:
            raise UserServiceException('User name too short')
        if not USERNAME_PATTERN_C.match(user_name):
            raise UserServiceException(
                'User name must be alfanumeric and -._ chars')

    def _validate_pin(self, pin):
        """
        pin: String
        """
        if not pin or len(pin) < 3:
            raise UserServiceException('Pin too short')
        if not PIN_PATTERN_C.match(pin):
            raise UserServiceException('Pin must be alfanumeric only chars')

    def validate_user(self, user_id, user_name, user_pin):
        """
        user_id: String
        user_name: String
        user_pin: String
        """
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        if user['user_name'] != user_name:
            raise UserServiceException('Invalid user name')
    <mask token>
    <mask token>

<mask token>


class UserService(object):
    _user_dao = None

    def __init__(self, logger, user_dao):
        self._logger = logger
        self._user_dao = user_dao

    def get_user_by_id(self, user_id):
        return self._user_dao.get_user_by_id(user_id)

    def add_user(self, user_name, pin):
        self._validate_user_name(user_name)
        self._validate_pin(pin)
        if not self.is_username_available(user_name):
            raise UserServiceException('User name is not available')
        created_user_id = self._user_dao.insert_user(user_name, pin)
        return created_user_id

    def add_autogenerated_user(self, user_name_prefix):
        autogenerated_pin = '123456'
        created_user_id = self._user_dao.insert_autogenerated_user(
            autogenerated_pin, user_name_prefix)
        return created_user_id

    def edit_user_name(self, dto):
        """
        Edit a user user_name
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_user_name":"",
        }
        Raises: UserServiceException
        """
        user_id = dto['user_id']
        user_pin = dto['pin']
        new_user_name = dto['new_user_name']
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        self.validate_user_name(new_user_name)
        user['user_name'] = new_user_name
        self._user_dao.save_user(user)

    def edit_pin(self, dto):
        """
        Edit a user pin
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_pin":"",
        }
        Raises: UserServiceException
        """
        user_id = dto['user_id']
        user_pin = dto['pin']
        new_user_pin = dto['new_pin']
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        if not new_user_pin or len(new_user_pin) == 0:
            raise UserServiceException('Pin is mandatory')
        user['pin'] = new_user_pin
        self._user_dao.save_user(user)

    def remove_user(self, user_id):
        if self._user_dao.get_user_by_id(user_id) == None:
            raise UserServiceException("User id '%s' not found" % user_id)
        self._user_dao.delete_user(user_id)

    def is_username_available(self, user_name):
        self._validate_user_name(user_name)
        return self._user_dao.get_user_by_user_name(user_name) == None

    def is_valid_user(self, user_id):
        """
        user_id: String
        """
        user = self._user_dao.get_user_by_id(user_id)
        return user != None and user['status'] == 1

    def _validate_user_name(self, user_name):
        """
        user_name: String
        """
        if not user_name or len(user_name) < 4:
            raise UserServiceException('User name too short')
        if not USERNAME_PATTERN_C.match(user_name):
            raise UserServiceException(
                'User name must be alfanumeric and -._ chars')

    def _validate_pin(self, pin):
        """
        pin: String
        """
        if not pin or len(pin) < 3:
            raise UserServiceException('Pin too short')
        if not PIN_PATTERN_C.match(pin):
            raise UserServiceException('Pin must be alfanumeric only chars')

    def validate_user(self, user_id, user_name, user_pin):
        """
        user_id: String
        user_name: String
        user_pin: String
        """
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        if user['user_name'] != user_name:
            raise UserServiceException('Invalid user name')
    """
    def _find_user_by_id_and_pin(self, user_id, user_pin):
        user = self._user_dao.get_user_by_id(user_id)

        if user == None or user["pin"] != user_pin:
            raise UserServiceException("Invalid wikilife User ID or PIN")

        return user
    """
    """
    def find_user(self, user_credentials):
        ""\"
        Finds a user by user_name and pin
        user_credentials: Dictionary
        {
            "user_name":"joaquin",
            "pin":"4449"
        }
        Returns: user
        Raises: UserServiceException if user not found
        ""\"
        user_name = user_credentials["user_name"]
        user_pin = user_credentials["pin"]
        user = self._user_dao.get_user_by_user_name(user_name)

        if user != None and user["pin"] == user_pin:
            return user
        else:
            raise UserServiceException("Invalid wikilife Username or PIN")
    """

<mask token>


class UserServiceException(Exception):
    pass


class UserService(object):
    _user_dao = None

    def __init__(self, logger, user_dao):
        self._logger = logger
        self._user_dao = user_dao

    def get_user_by_id(self, user_id):
        return self._user_dao.get_user_by_id(user_id)

    def add_user(self, user_name, pin):
        self._validate_user_name(user_name)
        self._validate_pin(pin)
        if not self.is_username_available(user_name):
            raise UserServiceException('User name is not available')
        created_user_id = self._user_dao.insert_user(user_name, pin)
        return created_user_id

    def add_autogenerated_user(self, user_name_prefix):
        autogenerated_pin = '123456'
        created_user_id = self._user_dao.insert_autogenerated_user(
            autogenerated_pin, user_name_prefix)
        return created_user_id

    def edit_user_name(self, dto):
        """
        Edit a user user_name
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_user_name":"",
        }
        Raises: UserServiceException
        """
        user_id = dto['user_id']
        user_pin = dto['pin']
        new_user_name = dto['new_user_name']
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        self.validate_user_name(new_user_name)
        user['user_name'] = new_user_name
        self._user_dao.save_user(user)

    def edit_pin(self, dto):
        """
        Edit a user pin
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_pin":"",
        }
        Raises: UserServiceException
        """
        user_id = dto['user_id']
        user_pin = dto['pin']
        new_user_pin = dto['new_pin']
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        if not new_user_pin or len(new_user_pin) == 0:
            raise UserServiceException('Pin is mandatory')
        user['pin'] = new_user_pin
        self._user_dao.save_user(user)

    def remove_user(self, user_id):
        if self._user_dao.get_user_by_id(user_id) == None:
            raise UserServiceException("User id '%s' not found" % user_id)
        self._user_dao.delete_user(user_id)

    def is_username_available(self, user_name):
        self._validate_user_name(user_name)
        return self._user_dao.get_user_by_user_name(user_name) == None

    def is_valid_user(self, user_id):
        """
        user_id: String
        """
        user = self._user_dao.get_user_by_id(user_id)
        return user != None and user['status'] == 1

    def _validate_user_name(self, user_name):
        """
        user_name: String
        """
        if not user_name or len(user_name) < 4:
            raise UserServiceException('User name too short')
        if not USERNAME_PATTERN_C.match(user_name):
            raise UserServiceException(
                'User name must be alfanumeric and -._ chars')

    def _validate_pin(self, pin):
        """
        pin: String
        """
        if not pin or len(pin) < 3:
            raise UserServiceException('Pin too short')
        if not PIN_PATTERN_C.match(pin):
            raise UserServiceException('Pin must be alfanumeric only chars')

    def validate_user(self, user_id, user_name, user_pin):
        """
        user_id: String
        user_name: String
        user_pin: String
        """
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        if user['user_name'] != user_name:
            raise UserServiceException('Invalid user name')
    """
    def _find_user_by_id_and_pin(self, user_id, user_pin):
        user = self._user_dao.get_user_by_id(user_id)

        if user == None or user["pin"] != user_pin:
            raise UserServiceException("Invalid wikilife User ID or PIN")

        return user
    """
    """
    def find_user(self, user_credentials):
        ""\"
        Finds a user by user_name and pin
        user_credentials: Dictionary
        {
            "user_name":"joaquin",
            "pin":"4449"
        }
        Returns: user
        Raises: UserServiceException if user not found
        ""\"
        user_name = user_credentials["user_name"]
        user_pin = user_credentials["pin"]
        user = self._user_dao.get_user_by_user_name(user_name)

        if user != None and user["pin"] == user_pin:
            return user
        else:
            raise UserServiceException("Invalid wikilife Username or PIN")
    """

# coding=utf-8

from wikilife_utils.patterns import USERNAME_PATTERN_C, PIN_PATTERN_C


class UserServiceException(Exception):
    pass


class UserService(object):

    _user_dao = None

    def __init__(self, logger, user_dao):
        self._logger = logger
        self._user_dao = user_dao
    
    def get_user_by_id(self, user_id):
        return self._user_dao.get_user_by_id(user_id)
    
    def add_user(self, user_name, pin):
        self._validate_user_name(user_name)
        self._validate_pin(pin)

        if not self.is_username_available(user_name):
            raise UserServiceException("User name is not available")

        created_user_id = self._user_dao.insert_user(user_name, pin)
        return created_user_id

    def add_autogenerated_user(self, user_name_prefix):
        autogenerated_pin = "123456"
        created_user_id = self._user_dao.insert_autogenerated_user(autogenerated_pin, user_name_prefix)
        return created_user_id

    def edit_user_name(self, dto):
        """
        Edit a user user_name
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_user_name":"",
        }
        Raises: UserServiceException
        """
        user_id = dto["user_id"]
        user_pin = dto["pin"]
        new_user_name = dto["new_user_name"]
        user = self._find_user_by_id_and_pin(user_id, user_pin)
        self.validate_user_name(new_user_name)
        user["user_name"] = new_user_name
        self._user_dao.save_user(user)

    def edit_pin(self, dto):
        """
        Edit a user pin
        dto: Dictionary
        {
            "user_id":""
            "pin":""
            "new_pin":"",
        }
        Raises: UserServiceException
        """
        user_id = dto["user_id"]
        user_pin = dto["pin"]
        new_user_pin = dto["new_pin"]
        user = self._find_user_by_id_and_pin(user_id, user_pin)

        if not new_user_pin or len(new_user_pin) == 0:
            raise UserServiceException("Pin is mandatory")

        user["pin"] = new_user_pin
        self._user_dao.save_user(user)

    def remove_user(self, user_id):
        if self._user_dao.get_user_by_id(user_id) == None:
            raise UserServiceException("User id '%s' not found" %user_id)

        self._user_dao.delete_user(user_id)

    def is_username_available(self, user_name):
        self._validate_user_name(user_name)
        return self._user_dao.get_user_by_user_name(user_name) == None

    def is_valid_user(self, user_id):
        """
        user_id: String
        """
        user = self._user_dao.get_user_by_id(user_id)
        return user != None and user["status"] == 1

    def _validate_user_name(self, user_name):
        """
        user_name: String
        """
        if not user_name or len(user_name) < 4:
            raise UserServiceException("User name too short")

        if not USERNAME_PATTERN_C.match(user_name):
            raise UserServiceException("User name must be alfanumeric and -._ chars")

    def _validate_pin(self, pin):
        """
        pin: String
        """
        if not pin or len(pin) < 3:
            raise UserServiceException("Pin too short")

        if not PIN_PATTERN_C.match(pin):
            raise UserServiceException("Pin must be alfanumeric only chars")

    def validate_user(self, user_id, user_name, user_pin):
        """
        user_id: String
        user_name: String
        user_pin: String
        """
        user = self._find_user_by_id_and_pin(user_id, user_pin)

        if user["user_name"] != user_name:
            raise UserServiceException("Invalid user name")

    '''
    def _find_user_by_id_and_pin(self, user_id, user_pin):
        user = self._user_dao.get_user_by_id(user_id)

        if user == None or user["pin"] != user_pin:
            raise UserServiceException("Invalid wikilife User ID or PIN")

        return user
    '''

    '''
    def find_user(self, user_credentials):
        """
        Finds a user by user_name and pin
        user_credentials: Dictionary
        {
            "user_name":"joaquin",
            "pin":"4449"
        }
        Returns: user
        Raises: UserServiceException if user not found
        """
        user_name = user_credentials["user_name"]
        user_pin = user_credentials["pin"]
        user = self._user_dao.get_user_by_user_name(user_name)

        if user != None and user["pin"] == user_pin:
            return user
        else:
            raise UserServiceException("Invalid wikilife Username or PIN")
    '''
