import face_recognition
from flask import Flask, request, redirect, Response
import camera
import firestore as db

# You can change     this to any folder on your system
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}

app = Flask(__name__)


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


def detect_faces_in_image(name, access, file_stream):
    # Load the uploaded image filed
    img = face_recognition.load_image_file(file_stream)
    # Get face encodings for any faces in the uploaded image
    unknown_face_encodings = face_recognition.face_encodings(img)[0].tolist()

    db.add_member(name, access, unknown_face_encodings)
    return ('', 204)


@app.route('/')
def root():
    return ('', 204)


@app.route('/upload', methods=['GET', 'POST'])
def upload_image():
    db.encoding.update()
    name = request.form.get("name")
    access = request.form.get("access")
    access = int(access)
    if request.method == 'POST':
        if 'file' not in request.files:
            return redirect(request.url)

        file = request.files['file']
        if file.filename == '':
            return redirect(request.url)

        if file and allowed_file(file.filename):
            return detect_faces_in_image(name, access, file)
    return redirect('/video_feed')


@app.route('/update', methods=['GET', 'POST'])
def update():
    db.encoding.update()
    member = request.form.get("updateMember")
    changeName = request.form.get("changeName")
    changeAccess = request.form.get("changeAccess")
    if changeAccess == None:
        changeAccess = ""
    db.update_member(member, changeName, changeAccess)

    return ('', 204)


@app.route('/configuration',  methods=['GET', 'POST'])
def config():
    db.config_camera_interval(int(request.form.get('cameraDuration')))
    return('', 204)


@app.route('/members')
def members():
    print(type(db.encoding.get_names()))
    return str(db.encoding.get_names())


@app.route('/video_feed')
def video_feed():
    print('CAMERA RUN')
    return Response(camera.gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')


@app.route('/recent_person')
def recent_person():
    return db.history_log.get_most_recent_member()


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5001, debug=True)

[(9, 'flask.Flask', 'Flask', (['__name__'], {}), False, 'from flask import Flask, request, redirect, Response\n'), (19, 'face_recognition.load_image_file', 'face_recognition.load_image_file', (['file_stream'], {}), False, 'import face_recognition\n'), (23, 'firestore.add_member', 'db.add_member', (['name', 'access', 'unknown_face_encodings'], {}), True, 'import firestore as db\n'), (34, 'firestore.encoding.update', 'db.encoding.update', ([], {}), True, 'import firestore as db\n'), (35, 'flask.request.form.get', 'request.form.get', (['"""name"""'], {}), False, 'from flask import Flask, request, redirect, Response\n'), (36, 'flask.request.form.get', 'request.form.get', (['"""access"""'], {}), False, 'from flask import Flask, request, redirect, Response\n'), (48, 'flask.redirect', 'redirect', (['"""/video_feed"""'], {}), False, 'from flask import Flask, request, redirect, Response\n'), (53, 'firestore.encoding.update', 'db.encoding.update', ([], {}), True, 'import firestore as db\n'), (54, 'flask.request.form.get', 'request.form.get', (['"""updateMember"""'], {}), False, 'from flask import Flask, request, redirect, Response\n'), (55, 'flask.request.form.get', 'request.form.get', (['"""changeName"""'], {}), False, 'from flask import Flask, request, redirect, Response\n'), (56, 'flask.request.form.get', 'request.form.get', (['"""changeAccess"""'], {}), False, 'from flask import Flask, request, redirect, Response\n'), (59, 'firestore.update_member', 'db.update_member', (['member', 'changeName', 'changeAccess'], {}), True, 'import firestore as db\n'), (84, 'firestore.history_log.get_most_recent_member', 'db.history_log.get_most_recent_member', ([], {}), True, 'import firestore as db\n'), (73, 'firestore.encoding.get_names', 'db.encoding.get_names', ([], {}), True, 'import firestore as db\n'), (79, 'camera.gen_frames', 'camera.gen_frames', ([], {}), False, 'import camera\n'), (40, 'flask.redirect', 'redirect', (['request.url'], {}), False, 'from flask import Flask, request, redirect, Response\n'), (44, 'flask.redirect', 'redirect', (['request.url'], {}), False, 'from flask import Flask, request, redirect, Response\n'), (66, 'flask.request.form.get', 'request.form.get', (['"""cameraDuration"""'], {}), False, 'from flask import Flask, request, redirect, Response\n'), (72, 'firestore.encoding.get_names', 'db.encoding.get_names', ([], {}), True, 'import firestore as db\n'), (21, 'face_recognition.face_encodings', 'face_recognition.face_encodings', (['img'], {}), False, 'import face_recognition\n')]