from django.shortcuts import render
from django.http import HttpResponse,FileResponse

from .forms import *
from django.views.decorators.csrf import csrf_exempt
import os
from bpconverter.settings import MEDIA_ROOT
import PyPDF2
import re
import glob
import mimetypes

def handle_uploaded_file(f):
    print(dir(f.chunks))
    counter = 0
    with open(os.path.join(MEDIA_ROOT,f.name), 'wb+') as destination:
        counter = counter +1
        print(counter)
        for chunk in f.chunks():
            #print(chunk) ok. qualcosa carica
            destination.write(chunk)

    print("{}/out/*.pdf".format(MEDIA_ROOT))

    listtoremove = glob.glob("{}/out/*.pdf".format(MEDIA_ROOT))
    for ltr in listtoremove:
      print('removing:',ltr)
      os.remove(ltr)

            
    return os.path.join(MEDIA_ROOT,f.name)

def esegue_conversione(inputfile):
  rec = re.compile('[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]',flags=re.I)

  print('apertura file')

  pdfr=None
  numpdfr=0
  try: 
    pdfr = PyPDF2.PdfReader(inputfile)
  except PyPDF2.errors.PdfReadError as pre:
    print('errore',pre)

  risultato = dict()
  if pdfr:  
    print('numero di pagine',len(pdfr.pages))
    numpdfr=len(pdfr.pages)
    codicefiscale=None
    listapagine={} #contiene il codice fiscale e le pagine in cui e' stato trovato

    for i in range(0,len(pdfr.pages)):
      pagina = pdfr.pages[i]
      testo = pagina.extract_text(0)
      # il testo estratto e' tutto carattere.
      trovato=re.search(rec,testo)
      if trovato:
        print('trovato',trovato[0])
        if trovato[0] in listapagine:
          listapagine[trovato[0]].append(i)
        else:
          listapagine[trovato[0]] = list()
          listapagine[trovato[0]].append(i)
          risultato[trovato[0]] = ''

    for k in listapagine:
      # per ogni codice fiscale, genera le pagine che corrispondono (listapagine[k])
      print(k,listapagine[k])

      writer = PyPDF2.PdfWriter()
      for kk in listapagine[k]:
        print('pagine da salvare',kk)
        writer.add_page(pdfr.pages[kk])

      r = open("{}/out/{}.pdf".format(MEDIA_ROOT,k),'wb')
      writer.write(r)
      r.close()
      risultato[k] = {'file':"{}/out/{}.pdf".format(MEDIA_ROOT,k),'pagine':listapagine[k]}

      '''
      fino a qui e' tutto chiaro.
      il file elaborato viene salvato in out denetro la cartella MEDIA_ROOT
      '''

       
  else: risultato['errore']=[]

  #rimozione del file
  os.remove(inputfile)

  print('risultato',risultato)
  return (risultato,numpdfr)

@csrf_exempt
def frontpage(request):
  print(__name__)
  listaccepted = list()
  listaccepted.append('127.0.0.1')
  listaccepted.append('195.110.154.210')
  listaccepted.append('195.110.154.213')

  if 'REMOTE' in os.environ:
    remote = os.environ['REMOTE'].split(',')
    for r in remote:
      listaccepted.append(r)

  print(request.META)
  if 'x_real_ip'.upper() in request.META:
    print('x_real_ip',request.META['HTTP_X_REAL_IP'])
  if 'http_host'.upper() in request.META:
    print('http_host',request.META['HTTP_HOST'])
  if 'remote_address'.upper() in request.META:
    print('remote_addr',request.META['REMOTE_ADDR'])
  if 'http_x_forwarded_for'.upper() in request.META:
    print('http_x_forwarded_for',request.META['HTTP_X_FORWARDED_FOR'])

  auth = False

  if 'HTTP_X_REAL_IP' in request.META:
    print('presente X_REAL_IP',request.META['HTTP_X_REAL_IP'])
    if request.META['HTTP_X_REAL_IP'] in listaccepted:
      print('found presente X_REAL_IP',request.META['HTTP_X_REAL_IP'])
      auth = True
  if 'HTTP_HOST' in request.META:
    if request.META['HTTP_HOST'] in listaccepted:
      print('found presente HTTP_HOST',request.META['HTTP_HOST'])
      auth = True
  if 'REMOTE_ADDR' in request.META:
    if request.META['REMOTE_ADDR'] in listaccepted:
      print('found presente REMOTE_ADDR',request.META['REMOTE_ADDR'])
      auth = True
  if 'HTTP_X_FORWARDED_FOR' in request.META:
    if request.META['HTTP_X_FORWARDED_FOR'] in listaccepted:
      print('found presente HTTP_X_FORWARDED_FOR',request.META['HTTP_X_FORWARDED_FOR'])
      auth = True


  if not auth:
    return HttpResponse("Accesso Non Autorizzato", status=503, headers={})

  data = {}
  print('request.method',request.method)
  if request.method == 'POST':
    form = UploadFileForm(request.POST,request.FILES)
    print('form is valid',form.is_valid())
    if form.is_valid():
      nomefile = request.FILES['filecaricato']
      print('nome file',nomefile)

      filesalvato = handle_uploaded_file(nomefile)
      risultato =  esegue_conversione(filesalvato)

      data['listafiles'] = risultato[0]
      data['numeropagine'] = risultato[1]

      print('dati passati al template',data)

  return render(request,'frontpage.html',data)

def finalize(request,cf=None):

  if not cf:
      return render("download.error.html")

  fl_completa = os.path.join(MEDIA_ROOT,'out',"{}.pdf".format(cf))

  print('path completa',fl_completa)

  if os.path.isfile(fl_completa):
    try:
      fl = open(fl_completa, 'rb')
    except Exception as er:
      print('errore',er)
      data=dict()
      data['errore']="File non esistente o non ancora disponibile"
      return render(request,'documento.error.html',data)
  else:
    print('il file non esiste in document,download')
    data=dict()
    data['errore']='File non esistente o non ancora disponibile'
    return render(request,'documento.error.html',data)

  mime_type, _ = mimetypes.guess_type(fl_completa)
  fl = fl_completa
  print('fl',fl,mime_type) # deve essere un file pdf
  #response = HttpResponse(open(fl,'rb'))
  response = FileResponse(open(fl,'rb'), content_type='application/pdf')
  response['Content-Disposition'] = "inline; filename=%s" % "{}.pdf".format(cf)

  return response

