import django_filters
from dal import autocomplete

from .models import Category, Question, UserAnswer, Exam
from django.db.models import Q
from atlas.models import Condition, Structure, Finding, Presentation, Subspecialty

#from generic.filters import UserAnswerFilter
from django.contrib.auth.models import User


def get_exams(request):
    if request is None:
        return Exam.objects.none()

    if not request.user.groups.filter(name="sba_checker").exists():
        queryset = Exam.objects.filter(author__id=request.user.id)
    else:
        queryset = Exam.objects.all()

    return queryset

def get_authors(request):
    if request is None:
        return User.objects.none()

    if not request.user.groups.filter(name="sba_checker").exists():
        queryset = User.objects.filter(id=request.user.id)
    else:
        queryset = User.objects.all()

    return queryset


class QuestionFilter(django_filters.FilterSet):
    # simple text searches
    title = django_filters.CharFilter(field_name='title', lookup_expr='icontains', label='Title')
    stem = django_filters.CharFilter(field_name='stem', lookup_expr='icontains', label='Stem')
    # combined quick search across title and stem
    q = django_filters.CharFilter(method='filter_q', label='Search')
    exams = django_filters.ModelMultipleChoiceFilter(queryset=get_exams, null_label="No exam")
    author = django_filters.ModelMultipleChoiceFilter(queryset=get_authors, null_label="No author")
    category = django_filters.ModelMultipleChoiceFilter(queryset=Category.objects.all(),null_label="Uncategorised")
    subspecialty = django_filters.ModelMultipleChoiceFilter(
        queryset=Subspecialty.objects.all(),
        widget=autocomplete.ModelSelect2Multiple(url='atlas:subspecialty-autocomplete'),
        label='Subspecialty',
    )
    condition = django_filters.ModelMultipleChoiceFilter(
        queryset=Condition.objects.all(),
        widget=autocomplete.ModelSelect2Multiple(url='atlas:condition-autocomplete'),
        label='Condition',
    )
    structure = django_filters.ModelMultipleChoiceFilter(
        queryset=Structure.objects.all(),
        widget=autocomplete.ModelSelect2Multiple(url='atlas:structure-autocomplete'),
        label='Structure',
    )
    finding = django_filters.ModelMultipleChoiceFilter(
        queryset=Finding.objects.all(),
        widget=autocomplete.ModelSelect2Multiple(url='atlas:finding-autocomplete'),
        label='Finding',
    )
    presentation = django_filters.ModelMultipleChoiceFilter(
        queryset=Presentation.objects.all(),
        widget=autocomplete.ModelSelect2Multiple(url='atlas:presentation-autocomplete'),
        label='Presentation',
    )
    class Meta:
        model = Question
        fields = ("exams", "created_date", "condition", "structure", "finding", "presentation", "category", "subspecialty", "title", "stem", "q")

    def __init__(self, data=None, queryset=None, prefix=None, strict=None, user=None, request=None):
        if not request.user.groups.filter(name="sba_checker").exists():
            queryset = queryset.filter(open_access=True) | queryset.filter(author__id=request.user.id)
        super(QuestionFilter, self).__init__(data=data, queryset=queryset, prefix=prefix, request=request)
        pass

    def filter_q(self, queryset, name, value):
        # search both title and stem
        if not value:
            return queryset
        return queryset.filter(
            Q(title__icontains=value) |
            Q(stem__icontains=value) |
            Q(a_answer__icontains=value) |
            Q(b_answer__icontains=value) |
            Q(c_answer__icontains=value) |
            Q(d_answer__icontains=value) |
            Q(e_answer__icontains=value) |
            Q(best_answer__icontains=value)
        )

class UserAnswerFilter(django_filters.FilterSet):
    class Meta:
        model= UserAnswer
        fields = (
            "cid",
            "user",
            #"question",
            "answer",
            #"answer_compare",
            "exam",
            "created",
            "updated",
        )

    def __init__(self, data=None, queryset=None, prefix=None, strict=None, user=None, request=None):
        if not request.user.is_superuser:
            if not request.user.groups.filter(name="sba_checker").exists():
                queryset = queryset.filter(exam__open_access=True) | queryset.filter(exam__author__id=request.user.id)
        super(UserAnswerFilter, self).__init__(data=data, queryset=queryset, prefix=prefix, request=request)
        pass