from django.conf import settings
from django.db import reset_queries
from django.http.response import Http404
from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from services.models import Service
from .models import Product, Tool, Documentation
from users.models import UserProfile, Testimony
from django.core.mail import EmailMessage
from django.core import mail

import operator


def home(request):
    p = Product.objects.all()
    s = Service.objects.all()
    q = UserProfile.objects.filter(userprofile_isforpartner=True)
    t = Testimony.objects.filter(testimony_isvalidated=True)
    taille = len(t)
    testimony_list = []
    i = 0
    for i in range(0, taille, 3):
        testimony_list.append([])
        testimony_list[-1].append(t[i])
        if i+1 < taille:
            testimony_list[-1].append(t[i+1])
        if i+2 < taille:
            testimony_list[-1].append(t[i+2])

    context = {
        'profiles': q,
        'services': s,
        'products': p,
        'testimonies': testimony_list,
    }

    return render(request, 'home.html', context)


def product_detail_view(request, pk):
    product = get_object_or_404(Product, pk=pk)
    c = {'product': product}
    return render(request, 'product/product_detail.html', c)


def product_list_view(request):
    p = Product.objects.all()
    return render(request, 'product/products.html', {'products': p})


def tool_detail_view(request, pk):
    tool = get_object_or_404(Tool, pk=pk)
    c = {'tool': tool}
    return render(request, 'product/tool_detail.html', c)


def downloads(request):
    doc_and_product = []
    doc_and_tool = []
    i = 0
    t = Tool.objects.all()
    p = Product.objects.all()
    for produit in p:
        doc_and_product.append({'product': produit, 'doc': False})
        docs = Documentation.objects.filter(product=produit)
        docs = sorted(docs, key=operator.attrgetter(
            'documentation_publicationdate'), reverse=True)
        if docs != []:
            docs = docs[0]
            doc_and_product[i]['doc'] = docs
        i += 1

    for tool in t:
        doc_and_tool.append({'tool': tool, 'doc': False})
        docs = Documentation.objects.filter(tool=tool)
        docs = sorted(docs, key=operator.attrgetter(
            'documentation_publicationdate'), reverse=True)
        if docs != []:
            docs = docs[0]
            doc_and_tool[i]['doc'] = docs
        i += 1

    context = {
        'products': doc_and_product,
        'tools': doc_and_tool
    }
    return render(request, 'product/download.html', context)


def comming_soon(request):
    return render(request, 'encours.html')


def about(request):
    return render(request, 'about.html')


def contact(request):
    if request.method == "POST":
        c = mail.get_connection()
        for e in request.POST:
            print(e)
        message = request.POST.get("message")
        email = request.POST.get("email")
        mail.send_mail(
            'test Message',
            f"L'utilisateur {email} vous a envoyé le message suivant: << {message} >>",
            settings.EMAIL_HOST_USER,
            ['kamganguifo@gmail.com'],
            connection=c,
        )
        c.close()
        return render(request, 'home.html')
    else:
        return render(request, 'contact.html')
