# -*- mode: python; coding: utf-8 -*-
# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later

from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.shortcuts import render
from django.template.context_processors import csrf

from med.settings import SEARCH_DISPLAY_PAGE
from media.models import Media, Jeu, Emprunt
from search.forms import SearchForm, SearchFormPlus
from users.models import User


def form(ctx, template, request):
    c = ctx
    c.update(csrf(request))
    return render(request, template, c)


def search_result(search, type, request):
    date_deb = None
    date_fin = None
    states = []
    aff = []
    if (type):
        aff = search.cleaned_data['affichage']
        states = search.cleaned_data['filtre']
        date_deb = search.cleaned_data['date_deb']
        date_fin = search.cleaned_data['date_fin']
    date_query = Q()
    if aff == []:
        aff = ['0', '1', '2', '3']
    if date_deb != None:
        date_query = date_query & Q(date_emprunt__gte=date_deb)
    if date_fin != None:
        date_query = date_query & Q(date_emprunt__lte=date_fin)
    search = search.cleaned_data['search_field']
    query1 = Q()
    for s in states:
        query1 = query1 | Q(state=s)

    connexion = []

    recherche = {'users_list': None, 'emprunts_list': None, 'medias_list': None, 'jeux_list': None}

    if request.user.has_perms(('perm',)):
        query = Q(user__pseudo__icontains=search) | Q(user__name__icontains=search) | Q(user__surname__icontains=search)
    else:
        query = (Q(user__pseudo__icontains=search) | Q(user__name__icontains=search) | Q(
            user__surname__icontains=search)) & Q(user=request.user)

    for i in aff:
        if i == '0':
            query_user_list = Q(pseudo__icontains=search) | Q(name__icontains=search) | Q(
                surname__icontains=search) & query1
            if request.user.has_perms(('perm',)):
                recherche['users_list'] = User.objects.filter(query_user_list).order_by('state', 'surname')
            else:
                recherche['users_list'] = User.objects.filter(query_user_list & Q(id=request.user.id)).order_by('state',
                                                                                                                'surname')
        if i == '1':
            recherche['emprunts_list'] = Emprunt.objects.filter(query & date_query).order_by('date_emprunt').reverse()
        if i == '2':
            recherche['medias_list'] = Media.objects.filter(
                Q(auteur__nom__icontains=search) | Q(titre__icontains=search))
        if i == '3':
            recherche['jeux_list'] = Jeu.objects.filter(
                Q(nom__icontains=search) | Q(proprietaire__pseudo__icontains=search) | Q(
                    proprietaire__name__icontains=search) | Q(proprietaire__surname__icontains=search))

    for r in recherche:
        if recherche[r] != None:
            recherche[r] = recherche[r][:SEARCH_DISPLAY_PAGE]

    recherche.update({'max_result': SEARCH_DISPLAY_PAGE})

    return recherche


@login_required
def search(request):
    search = SearchForm(request.POST or None)
    if search.is_valid():
        return form(search_result(search, False, request), 'search/index.html', request)
    return form({'searchform': search}, 'search/search.html', request)


@login_required
def searchp(request):
    search = SearchFormPlus(request.POST or None)
    if search.is_valid():
        return form(search_result(search, True, request), 'search/index.html', request)
    return form({'searchform': search}, 'search/search.html', request)
