/*
 *  Copyright 2013-2050 Emmanuel BRUN (contact@amapj.fr)
 * 
 *  This file is part of AmapJ.
 *  
 *  AmapJ is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.

 *  AmapJ is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with AmapJ.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 */
 package fr.amapj.view.views.advanced.maintenance;

import com.vaadin.server.Sizeable.Unit;

import fr.amapj.service.services.mailer.MailerMessage;
import fr.amapj.service.services.mailer.MailerService;
import fr.amapj.service.services.parametres.ParametresDTO;
import fr.amapj.service.services.parametres.ParametresService;
import fr.amapj.view.engine.popup.formpopup.WizardFormPopup;
import fr.amapj.view.engine.popup.formpopup.validator.StringLengthValidator;

/**
 * Popup pour le test de l'envoi des mails
 * 
 *
 */
public class SendMailPopup extends WizardFormPopup
{


	private MailInfo model;

	/**
	 * 
	 */
	public SendMailPopup()
	{
		setWidth(80);
		popupTitle = "Test de l'envoi des mails";
		saveButtonTitle = "Quitter";
		
		ParametresDTO param = new ParametresService().getParametres();
		model = new MailInfo();
		model.from = param.sendingMailUsername;
		setModel(model);

	}
	
	@Override
	protected void configure()
	{
		add(()->addFieldInfoGenerales());
		add(()->addFieldTexteMail(),()->checkMail());
		add(()->addFieldResultat());
	}

	

	private void addFieldInfoGenerales()
	{
		// Titre
		setStepTitle("les informations générales.");
		
		addHtml("Cet outil permet d'envoyer un mail à des fins de tests vers un destinataire unique.");
		addHtml("Cet outil permet de tester le bon fonctionnement de l'envoi des mails par l'administrateur informatique.");
		addHtml("Cet outil ne doit pas être utilisé dans un but autre que le test.");
			
	}
	
	

	
	
	private void addFieldTexteMail()
	{
		
		
		addTextField("Expéditeur du mail","from").setEnabled(false);
		
		addTextField("Destinataire du mail","dest");
		
		addTextField("Titre du mail", "title",new StringLengthValidator(1,500));
		
		addCKEditorFieldForLabel("Contenu du mail", "content").setHeight(8,Unit.CM); 
		
	}
	
	
	private String checkMail() 
	{
		if (model.dest.toLowerCase().contains("contact@amapj.fr"))
		{
			return  "Il n'est pas possible d'envoyer un mail à contact@amapj.fr avec cet outil";
		}
		
		if (model.dest.toLowerCase().contains(",") || model.dest.toLowerCase().contains(";"))
		{
			return  "Il n'est pas possible de mettre des , ou des ;";
		}
		return null;
	}
	
	
	
	
	
	private void addFieldResultat()
	{
		new MailerService().sendHtmlMail( new MailerMessage(model.dest, model.title, model.content));
		addHtml("Message envoyé à "+model.dest);
		setAllButtonsAsOK();
	}


	

	@Override
	protected void performSauvegarder()
	{
		
	}
	
	static public class MailInfo
	{
		public String from;
		public String dest;
		public String title;
		public String content;
		
		
		
		public String getFrom() 
		{
			return from;
		}
		public void setFrom(String from) 
		{
			this.from = from;
		}
		public String getDest() 
		{
			return dest;
		}
		public void setDest(String dest) 
		{
			this.dest = dest;
		}
		public String getTitle() 
		{
			return title;
		}
		public void setTitle(String title) 
		{
			this.title = title;
		}
		public String getContent() 
		{
			return content;
		}
		public void setContent(String content) 
		{
			this.content = content;
		}
	}
}
