# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division

import base64
import binascii
import json
import os
from datetime import datetime, timedelta

import falcon
import pytest
from falcon import testing

from falcon_auth import BasicAuthBackend, TokenAuthBackend, \
    JWTAuthBackend, NoneAuthBackend, MultiAuthBackend, HawkAuthBackend
from falcon_auth import FalconAuthMiddleware
from falcon_auth.backends import AuthBackend
from falcon_auth.serializer import ExtendedJSONEncoder

try:
    import jwt
    jwt_available = pytest.mark.skipif(False, reason="jwt not installed")
except ImportError:
    jwt_available = pytest.mark.skipif(True, reason="jwt not installed")

try:
    import mohawk
    hawk_available = pytest.mark.skipif(False, reason="hawk not installed")
except ImportError:
    hawk_available = pytest.mark.skipif(True, reason="hawk not installed")

EXPIRATION_DELTA = 30 * 60

SECRET_KEY = 'SecretKey123'

LEEWAY = 0


class User(object):

    def __init__(self, _id, username, password):
        self.id = _id
        self.username = username
        self.password = password
        self.token = binascii.hexlify(os.urandom(20)).decode()

    def clone(self):
        return User(self.id, self.username, self.password)

    def __str__(self):
        return "User(id='%s')" % self.id

    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username
        }

    def serialize(self):
        return json.dumps(self.to_dict())


@pytest.fixture(scope='function')
def user():
    return User(_id=1, username='joe', password='pass')


@pytest.fixture(scope='function')
def none_user():
    return User(_id=0, username='anonymous', password=None)


def create_app(auth_middleware, resource):

    api = falcon.API(middleware=[auth_middleware])

    api.add_route('/auth', resource)
    api.add_route('/posts/{post_id}', resource)
    return api


class AuthResource: