# Path: rophako/settings.py
# class ConfigHandler(object):
#     def load_settings(self):
#     def print_settings(self):
#     def load_plugins(self):
#     def __getattr__(self, section):
# 
# Path: rophako/log.py
# class LogHandler(logging.Handler):
#     def emit(self, record):


import bcrypt
import time
import rophako.jsondb as JsonDB
import rophako.model.photo as Photo
from rophako.settings import Config
from rophako.log import logger


def get_user(uid=None, username=None):
    """Get a user's DB file, or None if not found."""
    if username:
        uid = get_uid(username)
        logger.debug("get_user: resolved username {} to UID {}".format(username, uid))
    return JsonDB.get("users/by-id/{}".format(uid))


def get_picture(uid):
    """Get the chosen profile photo for the user."""
    data = get_user(uid=uid)
    pic = data["picture"]
    if len(pic):
        photo = Photo.get_photo(pic)
        if photo:
            return photo["avatar"]
    return None


def exists(uid=None, username=None):
    """Query whether a user ID or name exists."""
    if uid:
        return JsonDB.exists("users/by-id/{}".format(uid))
    elif username:
        return JsonDB.exists("users/by-name/{}".format(username.lower()))


def hash_password(password):

# Path: rophako/settings.py
# class ConfigHandler(object):
#     def load_settings(self):
#     def print_settings(self):
#     def load_plugins(self):
#     def __getattr__(self, section):
# 
# Path: rophako/log.py
# class LogHandler(logging.Handler):
#     def emit(self, record):


# Path: rophako/model/user.py
import bcrypt
import time
import rophako.jsondb as JsonDB
import rophako.model.photo as Photo
from rophako.settings import Config
from rophako.log import logger



def get_user(uid=None, username=None):
    """Get a user's DB file, or None if not found."""
    if username:
        uid = get_uid(username)
        logger.debug("get_user: resolved username {} to UID {}".format(username, uid))
    return JsonDB.get("users/by-id/{}".format(uid))


def get_picture(uid):
    """Get the chosen profile photo for the user."""
    data = get_user(uid=uid)
    pic = data["picture"]
    if len(pic):
        photo = Photo.get_photo(pic)
        if photo:
            return photo["avatar"]
    return None


def exists(uid=None, username=None):
    """Query whether a user ID or name exists."""
    if uid:
        return JsonDB.exists("users/by-id/{}".format(uid))
    elif username:
        return JsonDB.exists("users/by-name/{}".format(username.lower()))


def hash_password(password):

    return bcrypt.hashpw(str(password).encode("utf-8"), bcrypt.gensalt(int(Config.security.bcrypt_iterations))).decode("utf-8")