from django import forms
from .models import *
from configurazione.views import getConfigurazione

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

  # lista degli ftp server disponibili
  def ftpserver(self):
    _ftpserver = []
    f = FTPGroup.objects.all().filter(enabled=True)
    for i in f:
      _ftpserver.append((i.id,i.server))
    return _ftpserver

  # lista dei mail server disponibili
  def listmailserver(self):
    _mailserver = []
    m = MailServer.objects.all().filter(enabled=True)
    for i in m:
      _mailserver.append((i.id,i.nome))
    return _mailserver

  def listregistrar(self):
    _listregistrar = []
    r = Registrar.objects.all()
    for i in r:
      _listregistrar.append((i.id,i.nome))
    return _listregistrar

  def listtarget(self):
    _listtarget = []
    t = Target.objects.all().order_by('id')
    for i in t:
      _listtarget.append((i.id,i.nome))
    return _listtarget

  def listcausali(self):
    _listcausali = []
    c = Causale.objects.all().filter(enabled=True)
    for i in c:
      _listcausali.append((i.id,i.nome))
    return _listcausali

  def update(self):
    self.fields['dominio'] = forms.CharField(required=True)
    self.fields['public_email'] = forms.CharField(required=False,initial=getConfigurazione('default-public-email'))

    #necessario per il server di posta per capire dove andre a infilare la posta
    self.fields['home'] = forms.CharField(required=False) 

    self.fields['date_start_contract']   = forms.DateField(required=True)
    #,widget=forms.DateInput(format=['%m%d%Y','%m/%d%y']))
    self.fields['date_expire_contract']  = forms.DateField(required=True)
    #,widget=forms.DateInput(format=['%m%d%Y','%m/%d%y']))
    self.fields['date_expire_registrar'] = forms.DateField(required=True)
    #,widget=forms.DateInput(format=['%m%d%Y','%m/%d%y']))
    
    self.fields['contatti'] = forms.CharField(required=False)
    self.fields['proprietario'] = forms.CharField(required=False)
    
    self.fields['note'] = forms.CharField(required=False)
    
    #    self.fields['ftp_server'] = forms.ChoiceField(required=False,choices=self.ftpserver())

    self.fields['enabled'] = forms.BooleanField(required=False)
    self.fields['tobedeleted'] = forms.BooleanField(required=False,initial=0)
    
    self.fields['mail'] = forms.BooleanField(required=False)
    self.fields['pop3'] = forms.BooleanField(required=False)
    self.fields['imap'] = forms.BooleanField(required=False)
    self.fields['smtp'] = forms.BooleanField(required=False)
    self.fields['smtp_relay'] = forms.BooleanField(required=False)
    
    self.fields['sms'] =  forms.BooleanField(required=False)
    self.fields['webmail'] = forms.BooleanField(required=False)
    self.fields['nextcloud'] = forms.BooleanField(required=False)
    self.fields['nextcloud_pwd'] = forms.BooleanField(required=False)
    self.fields['sendafile'] = forms.BooleanField(required=False)

    self.fields['lmtp'] = forms.BooleanField(required=False)
    self.fields['sieve'] = forms.BooleanField(required=False)
    self.fields['antivirus'] = forms.BooleanField(required=False)
    self.fields['spf'] = forms.BooleanField(required=False)
    self.fields['blacklist'] = forms.BooleanField(required=False)
    
    self.fields['ftp'] = forms.BooleanField(required=False)
    self.fields['sql'] = forms.BooleanField(required=False)
    self.fields['http'] = forms.BooleanField(required=False)
    self.fields['proxy'] = forms.BooleanField(required=False)
    
    self.fields['nota'] = forms.CharField(required=False)

    self.fields['mail_server'] = forms.ChoiceField(required=True,initial=0,choices=self.listmailserver())
    self.fields['lmtp_server'] = forms.ChoiceField(required=True,initial=0,choices=self.listmailserver())
    self.fields['mail_proxy'] = forms.ChoiceField(required=True,initial=0,choices=self.listmailserver())
    self.fields['sql_remote'] = forms.ChoiceField(required=True,initial=0,choices=self.listmailserver())

    self.fields['account_used'] = forms.IntegerField(required=False)
    self.fields['alias_used'] = forms.IntegerField(required=False)
    self.fields['list_used'] =  forms.IntegerField(required=False)
    self.fields['mail_server'] = forms.ChoiceField(required=True,choices=self.listmailserver())
    self.fields['target'] = forms.ChoiceField(required=True,choices=self.listtarget())
    self.fields['registrar'] = forms.ChoiceField(required=True,choices=self.listregistrar())
    self.fields['last_causale'] = forms.ChoiceField(required=False,choices=self.listcausali())
    self.fields['max_account'] = forms.IntegerField(required=True)
    self.fields['max_alias'] = forms.IntegerField(required=True)
    self.fields['max_list'] = forms.IntegerField(required=True)
    self.fields['edit'] = forms.BooleanField(required=False)
    self.fields['rinnovo_automatico'] = forms.BooleanField(required=False)

class FormDomainCausale(forms.Form):
  def __init__(self,*args,**kwargs):
    forms.Form.__init__(self,*args,**kwargs)
    self.fields['causale'] = forms.ChoiceField(required=False,choices=self.listcausali())

  def listcausali(self):
    _listcausali = []
    c = Causale.objects.all().filter(enabled=True)
    for i in c:
      _listcausali.append((i.id,i.nome))
    return _listcausali

class FormServiziNota(forms.Form):
  def __init__(self,*args,**kwargs):
    forms.Form.__init__(self,*args,**kwargs)
    self.fields['nota'] = forms.CharField(required=False)
'''
  renew_authorized = models.BooleanField(db_column='renew_authorized')
  payment_status = models.BooleanField(db_column='payment_status')
  payment_value = models.DecimalField(db_column='payment_value',max_digits=8,decimal_places=2)
  payment_exclusion = models.BooleanField(db_column='payment_exclusion')

'''


'''
campo 'classe':
per evitare che ogni volta io vada a togliere,modificare e aggiungere un campo per integrare 
una nuova funzione, potrei usare il campo classe per definire cosa voglio dal sistema: per esempio,
la classe definita come 'MAIL' ufficializza il discorso mail e abilita il sotto sistema di competenza.
la classe definita come 'ANTIV' ufficializza l'abilitazione del servizio antivirus per il dominio
'''

class DomainServiceForm(forms.Form):
  def __init__(self,*args,**kwargs):
    forms.Form.__init__(self,*args,**kwargs)
    #  id   = models.IntegerField(db_column='id',primary_key=True)
    self.fields['nome'] = forms.CharField(required=True)
    self.fields['classe'] = forms.CharField(required=True)
    self.fields['enabled'] = forms.BooleanField(required=False)
    self.fields['home'] = forms.CharField(required=False)

    '''
    self.fields['domain'] = forms.BooleanField(required=False)
    self.fields['mail'] = forms.BooleanField(required=False)
    self.fields['pop3'] = forms.BooleanField(required=False)
    self.fields['imap'] = forms.BooleanField(required=False)

    imap = models.BooleanField(db_column='imap') # imap associato al servizio mail
    smtp = models.BooleanField(db_column='smtp')
    smtp_relay = models.BooleanField(db_column='smtp_relay') # consente di far uscire una mail dal sistema
    antivirus = models.BooleanField(db_column='antivirus')
    spf = models.BooleanField(db_column='spf')
    webmail = models.BooleanField(db_column='webmail')
    nextcloud = models.BooleanField(db_column='nextcloud')
    ftp = models.BooleanField(db_column='ftp')
    http = models.BooleanField(db_column='http')
    proxy = models.BooleanField(db_column='proxy')
    pec_std = models.BooleanField(db_column='pec_std')
    pec_med = models.BooleanField(db_column='pec_med')
    pec_max = models.BooleanField(db_column='pec_max')
    '''

