from django.http import HttpResponse, JsonResponse
import random
from .models import *
from .serializers import *
from rest_framework import viewsets
from rest_framework.decorators import action, api_view
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import serializers


def get_gamerun_by_id(id):
    return GameRuns.objects.get(gamerun_id=id)


# TODO: set number of emails and % of phishing emails
def set_random_emails(gamerun_id):
    number = 4
    perc_of_phishing = 50

    number_of_legitimate = int(round(number * (perc_of_phishing / 100), 0))
    number_of_phishing = number - number_of_legitimate

    phishing_emails = list(Emails.objects.filter(type__type_id__gt=1))
    legitimate_emails = list(Emails.objects.filter(type__type_id=1))

    r_phishing_emails = random.sample(phishing_emails, number_of_phishing)
    r_legitimate_emails = random.sample(legitimate_emails, number_of_legitimate)

    GameRuns.objects.get(gamerun_id=gamerun_id).emails.set(r_phishing_emails + r_legitimate_emails)


@api_view(('GET',))
def next_email(request):
    gamerun = get_gamerun_by_id(request.session['gamerun'])

    if request.session['remaining'] <= 0:  # TODO zmenit na file nastavenia
        return Response({"end": True})

    email = gamerun.emails.filter(emailgameruns__player_answer__isnull=True).first()

    request.session['current_email'] = email.email_id
    print(request.session['current_email'])
    request.session['remaining'] -= 1

    return JsonResponse({"type": email.type.type, "subject": email.subject, "sender": email.sender, "body": email.body})


# TODO: make session expire on page reload
@api_view(('POST',))
def init_gamerun(request):
    username = request.data['username']
    gamerun = GameRuns.objects.create()

    request.session['username'] = username
    request.session['gamerun'] = gamerun.gamerun_id
    request.session['current_email'] = 0
    request.session['remaining'] = 4  # TODO zmenit na nastaveny pocet vo file

    # set_random_emails(gamerun.gamerun_id)
    set_random_emails(gamerun.gamerun_id)
    return Response("successful")


# TODO : repair update
@api_view(('POST',))
def set_player_answer(request):
    gamerun_id = request.session['gamerun']
    email_id = request.session['current_email']

    egrun = EmailGameRuns.objects.get(email__exact=email_id, game_run=gamerun_id)
    egrun.player_answer = request.data['answer']
    egrun.save()

    return Response("successful")


class EmailViewSet(viewsets.ModelViewSet):
    queryset = Emails.objects.all()
    serializer_class = EmailSerializer


class SignViewSet(viewsets.ModelViewSet):
    queryset = Signs.objects.all()
    serializer_class = SignSerializer


class TypeViewSet(viewsets.ModelViewSet):
    queryset = Types.objects.all()
    serializer_class = TypeSerializer


class GameRunViewSet(viewsets.ModelViewSet):
    queryset = GameRuns.objects.all()
    serializer_class = GameRunSerializer


class EmailGameRunViewSet(viewsets.ModelViewSet):
    queryset = EmailGameRuns.objects.all()
    serializer_class = EmailGameRunSerializer
