# Path: securedpi_facerec/forms.py
# class PhotoForm(ModelForm):
#     class Meta:
#         model = Photo
#         fields = ['image']
# 
# Path: securedpi_facerec/models.py
# class Photo(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     date_uploaded = models.DateTimeField(
#             auto_now_add=True,
#             null=False)
#     image = models.ImageField(upload_to='training')
# 
#     def __str__(self):
#         return "Photo: {}".format(self.user.username)
# 
# Path: securedpi_facerec/facial_recognition/facial_recognition.py
# HERE = os.path.dirname(os.path.abspath(__file__))
# CASCADE_MODEL = os.path.join(HERE, 'haarcascade_frontalface_default.xml')
# FACE_CASCADE = cv2.CascadeClassifier(CASCADE_MODEL)
# TRAINING_SET_PATH = os.path.join(MEDIA_ROOT, 'training')
# RECOG_MODEL = os.path.join(HERE, 'recog_brain.yml')
# def train_recognizer(recognizer=cv2.face.createLBPHFaceRecognizer,
#                      image_path=TRAINING_SET_PATH,
#                      save_file=os.path.join(HERE, 'recog_brain.yml'),
#                      recog_model=None,
#                      demo=False):
# def test_individual(image_to_test, recog_model=RECOG_MODEL, verbose=False):


from django.views.generic import TemplateView
from django.shortcuts import render
from django.views import View
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.conf import settings
from django.urls import reverse
from django.http import JsonResponse
from .forms import PhotoForm
from .models import Photo
from django.views.decorators.csrf import csrf_exempt
from securedpi_facerec.facial_recognition import facial_recognition
import json


@login_required
@csrf_exempt
def upload_file(request):
    if request.method == 'POST':
        if request.FILES.get('webcam', None):
            f = request.FILES['webcam']
            f.name = "{}-{}-adasda".format('member', request.user.pk)
            new_file = Photo(user=request.user, image=f)
            new_file.save()
            return JsonResponse({})
        else:
            json_data = json.loads(request.body.decode('utf-8'))
            if json_data.get('action', None) == 'train':
                facial_recognition.train_recognizer()
                return JsonResponse({'result': 'success'})

# Path: securedpi_facerec/forms.py
# class PhotoForm(ModelForm):
#     class Meta:
#         model = Photo
#         fields = ['image']
# 
# Path: securedpi_facerec/models.py
# class Photo(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL)
#     date_uploaded = models.DateTimeField(
#             auto_now_add=True,
#             null=False)
#     image = models.ImageField(upload_to='training')
# 
#     def __str__(self):
#         return "Photo: {}".format(self.user.username)
# 
# Path: securedpi_facerec/facial_recognition/facial_recognition.py
# HERE = os.path.dirname(os.path.abspath(__file__))
# CASCADE_MODEL = os.path.join(HERE, 'haarcascade_frontalface_default.xml')
# FACE_CASCADE = cv2.CascadeClassifier(CASCADE_MODEL)
# TRAINING_SET_PATH = os.path.join(MEDIA_ROOT, 'training')
# RECOG_MODEL = os.path.join(HERE, 'recog_brain.yml')
# def train_recognizer(recognizer=cv2.face.createLBPHFaceRecognizer,
#                      image_path=TRAINING_SET_PATH,
#                      save_file=os.path.join(HERE, 'recog_brain.yml'),
#                      recog_model=None,
#                      demo=False):
# def test_individual(image_to_test, recog_model=RECOG_MODEL, verbose=False):


# Path: securedpi_facerec/views.py
from django.views.generic import TemplateView
from django.shortcuts import render
from django.views import View
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.conf import settings
from django.urls import reverse
from django.http import JsonResponse
from .forms import PhotoForm
from .models import Photo
from django.views.decorators.csrf import csrf_exempt
from securedpi_facerec.facial_recognition import facial_recognition
import json



@login_required
@csrf_exempt
def upload_file(request):
    if request.method == 'POST':
        if request.FILES.get('webcam', None):
            f = request.FILES['webcam']
            f.name = "{}-{}-adasda".format('member', request.user.pk)
            new_file = Photo(user=request.user, image=f)
            new_file.save()
            return JsonResponse({})
        else:
            json_data = json.loads(request.body.decode('utf-8'))
            if json_data.get('action', None) == 'train':
                facial_recognition.train_recognizer()
                return JsonResponse({'result': 'success'})

    return render(request, 'securedpi_facerec/training.html')