from django.shortcuts import render
from allauth.account.views import SignupView
from django.http import JsonResponse
from django.urls import reverse_lazy
from Sistemas.models import Sistema, BitacoraErrores
from .models import Sistemas_por_cliente_A24, ClientesA24
from django.views.generic.edit import CreateView
from django.views.generic.list import ListView
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin

from .forms import ClienteForm_IMMEX
from .serializers import ClientesA24Serailizer,SerialiazerA24

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from Sistemas.permissions import ItsAdminToken, HasAuthorizationHeader

import urllib.parse
import traceback

class Sistemas_xCliente_IMMEX_ListView(UserPassesTestMixin,LoginRequiredMixin, ListView):
    model = Sistemas_por_cliente_A24
    paginate_by = 100
    template_name = 'IMMEX/xclientes/lista.html'
    def test_func(self):
        res = self.request.user.groups.filter(name= 'admin_soft')
        if not res:
            messages.error(self.request, 'Lo sentimos. La página que buscas no está disponible o no cuentas con los permisos.')
        return res

class Sistemas_xCliente_IMMEX_CreateView(UserPassesTestMixin,LoginRequiredMixin,CreateView):
    model = Sistemas_por_cliente_A24
    fields = ['id_sistema', 'cliente', 'num_licencias']
    template_name = 'IMMEX/xclientes/sistema_create_IMMEX.html'
    success_url = reverse_lazy('sistemasXcli_IMMEX')
    def test_func(self):
        res = self.request.user.groups.filter(name= 'admin_soft')
        if not res:
            messages.error(self.request, 'Lo sentimos. La página que buscas no está disponible o no cuentas con los permisos.')
        return res

class ClientesIMMEX_CreateView(CreateView):
    model = ClientesA24
    form_class = ClienteForm_IMMEX
    success_url = '/IMMEX/'
    template_name = 'IMMEX/xclientes/edit_cliente.html'
    def form_valid(self, form):
        
        response = super().form_valid(form)
        if self.request.headers.get('X-Requested-With') == 'XMLHttpRequest':
            data={
                'id':self.object.id,
                'RFC':self.object.RFC,
                'Nombre':self.object.Nombre,
                'Activo':self.object.Activo,                
            }
            return JsonResponse(data)
        else:
            return response

    def form_invalid(self,form):
        response = super().form_invalid(form)
        if self.request.headers.get('X-Requested-With') == 'XMLHttpRequest':
            errors = form.errors.as_text()
            return JsonResponse({'errors':f'{errors}'},status=200,content_type='application/json')
        else:
            return response

"""---------API VIEWS---------"""
class RegistroUsuarios(APIView):
    permission_classes = [ItsAdminToken]
    def post(self,request, *args, **kwargs):
        try:
            signup_view = SignupView()
            signup_view.request = request
            response = signup_view.dispatch(request)
            if response.status_code == 302:
                return Response({'message': 'Registro exitoso'})
            else:
                return Response({'message': 'Error de registro', 'errors': response.context_data['form'].errors})
        except Exception as E:
            return Response({'Error':f'Error al registro con datos del usuario', 'isError':True})

class Check_IMMEX_RFC(APIView):
    """Verifica que el cliente pueda Timbrar"""
    permission_classes = [IsAuthenticated,ItsAdminToken]

    def post(self,request,*args, **kwargs):
        rfc= request.data.get('RFC')
        try:
            clienteA24 , created = ClientesA24.objects.get_or_create(RFC=rfc)
            serializer = ClientesA24Serailizer(clienteA24)

            if created:
                clienteA24.Activo =True
                clienteA24.Nombre = rfc
                clienteA24.save()
            if not serializer.is_valid:
                return Response({'Error':f'{serializer.errors}','isError':True},status=200)
            return Response(serializer.data)
        except Exception as E:
            return Response({'Error':f'check_RFC:{E} RFC:{rfc}','isError':True})

class RegisterIMMEX_Device_APIView(APIView):
    """Register IMMEX Devices"""
    permissions_classes=[IsAuthenticated, ItsAdminToken]
    def post(self,request):
        try:
            serializer = SerialiazerA24(data=request.data, context={'request':request})
        except Exception as ex:
            BitacoraErrores.objects.create(level=2, message=str(ex), traceback=traceback.format_exc(), 
            view='Sistemas.RegisterDeviceView')
            return Response({'Error':f'{ex}','isError':True}, status=status.HTTP_200_OK)            
