from django.shortcuts import render,get_object_or_404,redirect
from django.views.generic import TemplateView
from django.core.mail import send_mail
from django.contrib import messages
from django.http import *
from pkg_resources import require
from doucoder import settings
from portfolio.models import *
from django.core.paginator import Paginator
import random
import httpx
import json
# Create your views here.




class Home(TemplateView):
    template_name = 'index.html'
    def get(self, request, *args, **kwargs):

        realisations = Realisation.objects.all().order_by('-click')  
        fastMovie = FastMovieYoutube.objects.all()
        categorieRea = CategorieReal.objects.all()
        return render(request,self.template_name,{'realisations':realisations,'fastMovie':fastMovie,"categorieRea":categorieRea})


def portfolio(request): 
    
    return render(request,'pages/portfolio.html')


def meet(request):

    def sendSms(link: str):
        data = {
            "tel": "+330623773717",
            "msg": "Vous avez un nouveau meet lien : "+link,
            "company": "Doucoder",
            "password": "6007"
        }
        h = {"Content-Type": "application/json"}
        res = httpx.post('https://upsms.herokuapp.com/send',
                         data=json.dumps(dict(data)), headers=h)
        return res.json()

    def generateId(nb):

        cle = ""
        val = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789"
        for i in range(0, nb):
            c = random.randint(0, len(val) - 1)
            cle += val[c]
        return cle

    name = "doucoder"+generateId(10)
    lien = "https://meet.jit.si/"+name
    send_mail('lien meet doucoder.com', 'vous avez une invitation meet , lien:'+lien, settings.EMAIL_HOST_USER,
              ["abdoumbodji1401@gmail.com"])
    messages.success(request, "message envoye !")
    # print(res)
    # messages.success(request,"le lien est envoyé ! merci de patienter")
    return render(request,'pages/visioconf.html',{"name":name})


async def devis(request):
    



    try:
        tel = request.POST['telephone']
        type = request.POST['type']
        description = request.POST['description']
        name = request.POST['name']
        budget = request.POST['budget']
        data = {
            "tel": "+330623773717",
            "msg": "Vous avez un nouveau Demande de devis de "+name+', tel: '+tel + ", type de projet: "+type + ', budget: '+budget + ', description: ' + description,
            "company": "Doucoder",
            "password": "6007"
        }

        async def sendSms():
            h = {"Content-Type": "application/json"}
            res = httpx.post('https://upsms.herokuapp.com/send',
                             data=json.dumps(dict(data)), headers=h)
            return res.json()

        send_mail('Demande de dévis doucoder.com', data['msg'], settings.EMAIL_HOST_USER, [
                  "abdoumbodji1401@gmail.com"])
        messages.success(request, "message envoye !")
    except:
        
        return redirect('/')

    return redirect('/')


def bc(request): 

    return render(request,'pages/bc.html')

def meContacter (request):

    
    if request.method == 'POST':
        
        subject = 'Contact de '+ request.POST['nom'] +' '+ request.POST['email']
        message = request.POST['message']
        email_from = settings.EMAIL_HOST_USER
        recipient_list = ['abdoumbodji1401@gmail.com',]

        send_mail( subject, message, email_from, recipient_list )
        messages.success(request,"message envoye !")

    return redirect('portfolio')


def sitemap(request): 

    return render(request,'sitemap.xml')