from flask import Blueprint, render_template, redirect, url_for, request,  current_app as app
from flask_oidc import OpenIDConnect
from oauth2client.client import OAuth2Credentials
from .custom_oidc_socket import logoutSession
from ...db.settings import db, oidc


from ...models.Statistic import Statistic

import json
import datetime
import time

from ...models.User import User
from ...repository.AuthenticationRepository import AuthenticationRepository

from ...repository.UserRepositoryNew import updateOrInsertUser

from flask_jwt_extended import (create_access_token, create_refresh_token, jwt_required, jwt_refresh_token_required, get_jwt_identity, get_raw_jwt, set_access_cookies, get_jti,
                                set_refresh_cookies, unset_jwt_cookies, decode_token)


repo = AuthenticationRepository(testing=False)

auth = Blueprint('auth', __name__, static_folder="static",
                 template_folder="templates")





@auth.route('/login')
@oidc.require_login
def login():
    user_creds = oidc.user_getinfo(
        ['sub', 'preferred_username', 'given_name', 'family_name', 'email', 'roles'])
        
    user_id = user_creds.get('sub')
    

    if "roles" in user_creds:
        access_token = create_access_token(identity=user_id, expires_delta=datetime.timedelta(seconds=10), user_claims={"is_administrator": ("slidea_admin" in user_creds["roles"])})
        refresh_token = create_refresh_token(identity=user_id, user_claims={"is_administrator": ("slidea_admin" in user_creds["roles"])} )
        updateOrInsertUser(
            u_id=user_creds.get('sub'),
            username=user_creds.get('preferred_username'),
            surname=user_creds.get('given_name'),
            family_name=user_creds.get('family_name'),
            email=user_creds.get('email'),
            created=time.time(),
            last_login=time.time(),
            isAdmin=("slidea_admin" in user_creds["roles"])
        )   

    else:
        access_token = create_access_token(identity=user_id, expires_delta=datetime.timedelta(seconds=10), user_claims={"is_administrator": False})
        refresh_token = create_refresh_token(identity=user_id, user_claims={"is_administrator": False})
        #repo.createUser(user_id=user_id, name=user_name, email=user_mail, img=None, last_login=time.time(), created=time.time(), is_admin=False)
        updateOrInsertUser(
            u_id=user_creds.get('sub'),
            username=user_creds.get('preferred_username'),
            surname=user_creds.get('given_name'),
            family_name=user_creds.get('family_name'),
            email=user_creds.get('email'),
            created=time.time(),
            last_login=time.time(),
            isAdmin=False
        )   

    redir = render_template('/profile-redesign/index.html',
                            access=access_token, refresh=refresh_token, loginSucceeded=True)
    return redir


@auth.route('/logout', methods=['GET'])
def logout():
    if oidc.user_loggedin:
        refresh_token = oidc.get_refresh_token()
        access_token = oidc.get_access_token()

        logoutSession(refresh_token, access_token)
        oidc.logout()
    #logoutSession(refresh_token, access_token)
    return json.dumps({"success": 1})


@auth.route('/test')
@jwt_required
def test():
    current_user = get_jwt_identity()
    print(current_user)
    return 'moin!'


@auth.route('/isAdmin')
@jwt_required
def isAdmin():
    u_id = get_jwt_identity()
    claims = get_raw_jwt()

    return json.dumps({"res": claims["user_claims"]["is_administrator"]})

@auth.route('/refresh_token', methods=['POST'])
@jwt_refresh_token_required
def refresh():
    current_user = get_jwt_identity()
    claims = get_raw_jwt()

    if claims["user_claims"]["is_administrator"]:
        print("REFRESHING TOKEN WITH ADMIN RIGHTS")
        access_token = create_access_token(identity=current_user, user_claims={"is_administrator": True})
    else:
        access_token = create_access_token(identity=current_user, user_claims={"is_administrator": False})

    return access_token


@auth.route('/getUserID')
@jwt_required
def getUserID():
    return json.dumps({"u_id": get_jwt_identity()})
from flask import Blueprint, render_template, redirect, url_for, request,  current_app as app
from flask_oidc import OpenIDConnect
from oauth2client.client import OAuth2Credentials
from .custom_oidc_socket import logoutSession
from ...db.settings import db, oidc


from ...models.Statistic import Statistic

import json
import datetime
import time

from ...models.User import User
from ...repository.AuthenticationRepository import AuthenticationRepository

from ...repository.UserRepositoryNew import updateOrInsertUser

from flask_jwt_extended import (create_access_token, create_refresh_token, jwt_required, jwt_refresh_token_required, get_jwt_identity, get_raw_jwt, set_access_cookies, get_jti,
                                set_refresh_cookies, unset_jwt_cookies, decode_token)


repo = AuthenticationRepository(testing=False)

auth = Blueprint('auth', __name__, static_folder="static",
                 template_folder="templates")





@auth.route('/login')
@oidc.require_login
def login():
    user_creds = oidc.user_getinfo(
        ['sub', 'preferred_username', 'given_name', 'family_name', 'email', 'roles'])
        
    user_id = user_creds.get('sub')
    

    if "roles" in user_creds:
        access_token = create_access_token(identity=user_id, expires_delta=datetime.timedelta(seconds=10), user_claims={"is_administrator": ("slidea_admin" in user_creds["roles"])})
        refresh_token = create_refresh_token(identity=user_id, user_claims={"is_administrator": ("slidea_admin" in user_creds["roles"])} )
        updateOrInsertUser(
            u_id=user_creds.get('sub'),
            username=user_creds.get('preferred_username'),
            surname=user_creds.get('given_name'),
            family_name=user_creds.get('family_name'),
            email=user_creds.get('email'),
            created=time.time(),
            last_login=time.time(),
            isAdmin=("slidea_admin" in user_creds["roles"])
        )   

    else:
        access_token = create_access_token(identity=user_id, expires_delta=datetime.timedelta(seconds=10), user_claims={"is_administrator": False})
        refresh_token = create_refresh_token(identity=user_id, user_claims={"is_administrator": False})
        #repo.createUser(user_id=user_id, name=user_name, email=user_mail, img=None, last_login=time.time(), created=time.time(), is_admin=False)
        updateOrInsertUser(
            u_id=user_creds.get('sub'),
            username=user_creds.get('preferred_username'),
            surname=user_creds.get('given_name'),
            family_name=user_creds.get('family_name'),
            email=user_creds.get('email'),
            created=time.time(),
            last_login=time.time(),
            isAdmin=False
        )   

    redir = render_template('/profile-redesign/index.html',
                            access=access_token, refresh=refresh_token, loginSucceeded=True)
    return redir


@auth.route('/logout', methods=['GET'])
def logout():
    if oidc.user_loggedin:
        refresh_token = oidc.get_refresh_token()
        access_token = oidc.get_access_token()

        logoutSession(refresh_token, access_token)
        oidc.logout()
    #logoutSession(refresh_token, access_token)
    return json.dumps({"success": 1})


@auth.route('/test')
@jwt_required
def test():
    current_user = get_jwt_identity()
    print(current_user)
    return 'moin!'


@auth.route('/isAdmin')
@jwt_required
def isAdmin():
    u_id = get_jwt_identity()
    claims = get_raw_jwt()

    return json.dumps({"res": claims["user_claims"]["is_administrator"]})

@auth.route('/refresh_token', methods=['POST'])
@jwt_refresh_token_required
def refresh():
    current_user = get_jwt_identity()
    claims = get_raw_jwt()

    if claims["user_claims"]["is_administrator"]:
        print("REFRESHING TOKEN WITH ADMIN RIGHTS")
        access_token = create_access_token(identity=current_user, user_claims={"is_administrator": True})
    else:
        access_token = create_access_token(identity=current_user, user_claims={"is_administrator": False})

    return access_token


@auth.route('/getUserID')
@jwt_required
def getUserID():
    return json.dumps({"u_id": get_jwt_identity()})
#repo.createUser(user_id=user_id, name=user_name, email=user_mail, img=None, last_login=time.time(), created=time.time(), is_admin=False) #logoutSession(refresh_token, access_token)