from flask import request, jsonify, Blueprint
from project.services.auth import Auth, current_user, SocialSignin
from models import user, invite
from flask_api.status import HTTP_400_BAD_REQUEST
from project import database_wrapper

blueprint = Blueprint(
    'account', __name__
)


@blueprint.route('/login', methods=['POST'])
def login():
    req = request.json
    try:
        email = req['email'].lower()
        password_hash = req['password']
    except:
        return '', HTTP_400_BAD_REQUEST

    user_object = user.findSingleUser({'email': email})
    error = Auth.login(user_object, password_hash)
    if error:
        return jsonify(LoggedIn=False, error=error), HTTP_400_BAD_REQUEST
    return user.get_basic_info_with_security(user_object)


@blueprint.route('/login-social', methods=['POST'])
def login_social():
    req = request.json
    try:
        social_type = req['socialType']
        token = req['token']
    except:
        return '', HTTP_400_BAD_REQUEST

    error = Auth.login_social(social_type, token)
    if error:
        return jsonify(LoggedIn=False, error=error), HTTP_400_BAD_REQUEST
    return user.get_basic_info_with_security(current_user)


@blueprint.route('/signup', methods=['POST'])
def signup():
    error = None
    req = request.json
    password = req['password']
    request_email = req['email'].lower()

    entry = user.findSingleUser({'email': request_email})

    if entry is None:
        try:
            invite_code = req['invite']
            if not invite.is_valid(invite_code):
                raise Exception("Invalid invite code")
            new_user = user.create_user(req)
            database_wrapper.save_entity(new_user)
            invite.consume(invite_code, new_user['_id'])
            #TODO: The issue is that get_basic_info_with_security which was
            #here before relies on the current_user field which is now not set
            #since there is no user logged in. Using the following method is a
            #temporary workaround.. Really a lot of this stuff needs refactoring
            #so I'm not going to write another get_basic_info method.
            basic_info = user.get_basic_info_from_users([new_user])[0]
        except Exception as e:
            return jsonify(error=str(e)), HTTP_400_BAD_REQUEST
        #TODO:
        return jsonify(basic_info)
    else:
        error = 'Email is already in use'
        return jsonify(LoggedIn=False, error=error), HTTP_400_BAD_REQUEST


@blueprint.route('/logout', methods=['GET'])
@Auth.require(Auth.GHOST)
def logout():
    Auth.logout()
    return jsonify(LoggedIn=False, error=None)


@blueprint.route('/attach-social-login', methods=['POST'])
@Auth.require(Auth.GHOST)
def attach_social_login():
    req = request.json
    try:
        social_type = req['socialType']
        token = req['token']
    except:
        return '', HTTP_400_BAD_REQUEST

    SocialSignin.attach(current_user['_id'], social_type, token)
    return jsonify(error=None)
@@ -1281,39 +1281,8 @@
 son%0A
-    password = req%5B'password'%5D%0A
     
@@ -1318,16 +1318,46 @@
 lower()%0A
+    password = req%5B'password'%5D
 %0A    ent
@@ -1424,16 +1424,20 @@
 ntry is 
+not 
 None:%0A  
@@ -1438,33 +1438,141 @@
 ne:%0A        
+error = 'Email is already in use'%0A        return jsonify(LoggedIn=False, error=error), HTTP_400_BAD_REQUEST%0A    
 try:%0A
-    
         invi
@@ -1603,20 +1603,16 @@
         
-    
 if not i
@@ -1640,20 +1640,16 @@
 _code):%0A
-    
         
@@ -1691,20 +1691,16 @@
  code%22)%0A
-    
         
@@ -1732,24 +1732,82 @@
 er(req)%0A    
+    if (new_user is None):%0A            raise Exception()%0A%0A
         data
@@ -1841,20 +1841,16 @@
 w_user)%0A
-    
         
@@ -1894,20 +1894,17 @@
 '_id'%5D)%0A
-    
+%0A
         
@@ -1908,405 +1908,124 @@
    #
-TODO: The issue is that get_basic_info_with_security which was%0A            #here before relies on the current_user field which is now not set%0A            #since there is no user logged in. Using the following method is a%0A            #temporary workaround.. Really a lot of this stuff needs refactoring%0A            #so I'm not going to write another get_basic_info method.%0A            basic_info = 
+ We need to log in the just-registered user.%0A        status = Auth.login(new_user, password)%0A        return jsonify(
 user
@@ -2065,21 +2065,18 @@
 ser%5D)%5B0%5D
+)
 %0A
-    
     exce
@@ -2098,28 +2098,24 @@
  e:%0A        
-    
 return jsoni
@@ -2157,184 +2157,8 @@
 EST%0A
-        #TODO:%0A        return jsonify(basic_info)%0A    else:%0A        error = 'Email is already in use'%0A        return jsonify(LoggedIn=False, error=error), HTTP_400_BAD_REQUEST%0A
 %0A%0A@b
@@ -2660,16 +2660,17 @@
 nify(error=None)
+%0A

Fix registration to log you in.