from django import forms

import re
from sicurezza.views import getSecurityLevelAll
from configurazione.views import *

from .models import *

class formFtp(forms.Form):

  def __init__(self,*args,**kwargs):
    forms.Form.__init__(self,*args,**kwargs)

  def update(self):
    self.fields['utente'] = forms.CharField(required=True)
    self.fields['dominio'] = forms.IntegerField(required=True)
    self.fields['dominio_domain'] = forms.CharField(required=True)
    self.fields['homedir'] = forms.CharField(required=False)

    self.fields['utente'] = forms.CharField(required=True)

    self.fields['utente_assegnato'] = forms.CharField(required=False)

    self.fields['password1'] = forms.CharField(required=True,widget=forms.PasswordInput)
    self.fields['password2'] = forms.CharField(required=True,widget=forms.PasswordInput)
    self.fields['clear'] = forms.CharField(required=True)
    self.fields['rigenerapassword'] = forms.BooleanField(required=False,initial=False)
    self.fields['inviaparametri'] = forms.BooleanField(required=False,initial=False)
    self.fields['password_change_enabled'] = forms.BooleanField(required=False,initial=True)

    self.fields['enabled'] = forms.BooleanField(required=False,initial=False)
    self.fields['tobedeleted'] = forms.BooleanField(required=False,initial=False)

    self.fields['ftpgroup'] = forms.ChoiceField(required=True,initial=0,choices=self.listFTPGroup())
    self.fields['ftpserver'] = forms.ChoiceField(required=True,initial=0,choices=self.listServer())

    self.fields['mail'] = forms.CharField(required=False)
    self.fields['mail_send'] = forms.BooleanField(required=False,initial=False)

    self.fields['master'] = forms.BooleanField(required=False,initial=False)

    self.fields['edit'] = forms.BooleanField(required=False,initial=False)

  def clean_password1(self):
    #bypass in caso di password non modificata
    #if self.has_changed():
    #  return self.data['password1']

    # password uguali nei due campi
    data = self.data['password1']
    if data != self.data['password2']:
      raise forms.ValidationError("I due campi password devono essere uguali")

    # lunghezza password
    if len(data.strip()) < int(getConfigurazione('ftp_password_length')):
      raise forms.ValidationError("Password troppo corta!")

    # stessa password usata attualmente
    if data.strip() == self.fields['utente']:
      raise forms.ValidationError("Password gia' utilizzata")

    # presenza di spazi non validi
    if re.search('\s',data):
      raise forms.ValidationError("Sono presenti caratteri non validi. I caratteri accettati sono: a-z,A-Z,0-9 e \.\,\[\!\@\#\$\%\^\&\*\?\_\~\-\:\;\(\)\[\]\{\}")

    if re.search('\d',data):
      if re.search('[a-z]',data):
        if re.search('[A-Z]',data):
          if re.search('[\.\,\[\!\@\#\$\%\^\&\*\?\_\~\-\:\;\(\)\[\]\{\}]',data):
            return data
    raise forms.ValidationError("Non e' presente almeno un numero, una lettera maiuscola, una minuscola e un simbolo")

  def clean_password2(self):
    data2 = self.data['password2']
    data1 = self.data['password1']
    if data2 != data1: raise forms.ValidationError('Confronto Email non riuscito')
    return data2

  def clean__mail(self):
    data = self.cleaned_data['mail']
    if ('@',' ','!') in data: raise forms.ValidationError('Caratteri non validi')
    return data

  def listFTPGroup(self):
    listaftpgroup = FTPGroup.objects.filter(enabled=True)
    lista = []
    for i in listaftpgroup:
      lista.append((i.id,i.nomegruppo))
    return lista

  def listServer(self):
    listaserver = FTPServer.objects.filter(enabled=True)
    lista = []
    for i in listaserver:
      lista.append((i.id,i.nome))
    return lista
