/*
 *  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.engine.popup.passwordpopup;

import com.vaadin.server.Page;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

import fr.amapj.service.services.authentification.PasswordComplianceService;
import fr.amapj.service.services.authentification.ProcessSetPasswordService;
import fr.amapj.service.services.parametres.ParametresService;
import fr.amapj.view.engine.popup.formpopup.WizardFormPopup;
import fr.amapj.view.engine.tools.BaseUiTools;

/**
 * Popup de saisie d'un mot de passe, dans le but de créer un nouveau compte ou 
 * de modifier un mot de passe existant 
 * 
 * Ce popup permet la sauvegarde du mot de passe par le gestionnaire du browser
 * 
 * Ce popup ne doit pas être utilisé pour l'authentification 
 *  
 */
public class PasswordPopup extends WizardFormPopup
{

	private CustomLoginForm loginForm;
	private Long idUtilisateur;
	private String email;
	
	private String textPage1;
	private String passwordFieldCaption;
	
	private String textPage2;
	
	
	
	public PasswordPopup(Long idUtilisateur,String email)
	{
		this(idUtilisateur, email, "Changement de votre mot de passe", "","Votre nouveau mot de passe","Votre mot de passe a été changé avec succés");
	}

	/**
	 * 
	 */
	public PasswordPopup(Long idUtilisateur,String email,String popupTitle,String textPage1, String passwordFieldCaption,String textPage2)
	{
		this.popupTitle = popupTitle;
		setWidth(40,450);
		
		this.idUtilisateur = idUtilisateur;
		this.email = email;
		this.textPage1 = textPage1;
		this.passwordFieldCaption = passwordFieldCaption;
		this.textPage2 = textPage2;
	}
	
	@Override
	protected void configure()
	{
		add(()->addSaisiePassword(),()->checkPassword());
		add(()->addResultat());
	}
	
	protected void addSaisiePassword()
	{
				
		addHtml(textPage1);
		
		loginForm =  new CustomLoginForm();
		form.addComponent(loginForm);
		
		addHtml(new PasswordComplianceService().getHtmlExplanationPasswordCompliant());
		
		setAllButtonsAsCancelOK();
			
	}
	
	private String checkPassword() 
	{
		boolean isCompliant = new PasswordComplianceService().isCompliantPassword(loginForm.getPassword());
		if (isCompliant==true)
		{
			return null;
		}
		return "Votre mot de passe ne respecte pas les contraintes.<br/>"+new PasswordComplianceService().getHtmlExplanationPasswordCompliant();
	}
	
	private void addResultat()
	{
		boolean res = new ProcessSetPasswordService().setUserPassword(idUtilisateur,loginForm.getPassword(),true);
		
		if (res==false)
		{
			addHtml("Impossible de modifier votre mot de passe");
		}
		else
		{
			addHtml(textPage2);
		}
		setAllButtonsAsOK();
	}

	@Override
	protected void performSauvegarder()
	{
		// Il est obligatoire de recharger la page pour que l'auto completion du mot de passe fonctionne correctement sous Firefox
		if (BaseUiTools.isFirefox())
		{
			String url = new ParametresService().getParametres().url;
			Page.getCurrent().setLocation(url);
		}
	}
	
	
	/**
	 * Zone de saisie du password 
	 *
	 */
    public class CustomLoginForm extends LoginForm 
    {
		
		private PasswordField passwordField;


		@Override
        protected Component createContent(TextField userNameField, PasswordField passwordField, Button loginButton) 
        {
			passwordField.setCaption(passwordFieldCaption);
			passwordField.setWidth("280px");
			
			userNameField.setCaption("Votre email");
			userNameField.setWidth("280px");
			
			this.passwordField = passwordField;
			VerticalLayout layout = new VerticalLayout();
			
		    layout.setSpacing(true);
		    layout.setMargin(false);
			
			userNameField.setValue(email);
			userNameField.setEnabled(false);
			
			
			layout.addComponent(userNameField);
		    layout.addComponent(passwordField);
		    
		    return layout;
        }
		
		public String getPassword()
		{
			return passwordField.getValue();
		}
    }	
}
