package fr.openwide.maven.artifact.notifier.web.application.administration.component;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.odlabs.wiquery.core.events.MouseEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.openwide.core.wicket.markup.html.link.EmailLink;
import fr.openwide.core.wicket.markup.html.panel.GenericPanel;
import fr.openwide.core.wicket.more.markup.html.basic.DateLabel;
import fr.openwide.core.wicket.more.markup.html.feedback.FeedbackUtils;
import fr.openwide.core.wicket.more.markup.html.image.BooleanGlyphicon;
import fr.openwide.core.wicket.more.markup.html.template.js.jquery.plugins.bootstrap.confirm.component.AjaxConfirmLink;
import fr.openwide.core.wicket.more.markup.html.template.js.jquery.plugins.bootstrap.modal.behavior.AjaxModalOpenBehavior;
import fr.openwide.core.wicket.more.model.BindingModel;
import fr.openwide.core.wicket.more.util.DatePattern;
import fr.openwide.maven.artifact.notifier.core.business.user.model.User;
import fr.openwide.maven.artifact.notifier.core.business.user.service.IUserService;
import fr.openwide.maven.artifact.notifier.core.util.binding.Binding;
import fr.openwide.maven.artifact.notifier.web.application.MavenArtifactNotifierSession;
import fr.openwide.maven.artifact.notifier.web.application.administration.form.ChangePasswordPopupPanel;
import fr.openwide.maven.artifact.notifier.web.application.administration.form.UserFormPopupPanel;

public class UserProfilPanel extends GenericPanel<User> {

	private static final long serialVersionUID = 8651898170121443991L;

	private static final Logger LOGGER = LoggerFactory.getLogger(UserProfilPanel.class);

	@SpringBean
	private IUserService userService;

	public UserProfilPanel(String id, final IModel<User> userModel) {
		super(id, userModel);
		
		// Principal email address
		add(new EmailLink("email", BindingModel.of(userModel, Binding.user().email())));
		
		add(new Label("fullName", BindingModel.of(userModel, Binding.user().fullName())));
		
		add(new BooleanGlyphicon("active", BindingModel.of(userModel, Binding.user().active())));
		
		// Dates
		add(new DateLabel("creationDate", BindingModel.of(userModel, Binding.user().creationDate()),
				DatePattern.SHORT_DATETIME));
		add(new DateLabel("lastLoginDate", BindingModel.of(userModel, Binding.user().lastLoginDate()),
				DatePattern.SHORT_DATETIME));
		add(new DateLabel("lastUpdateDate", BindingModel.of(userModel, Binding.user().lastUpdateDate()),
				DatePattern.SHORT_DATETIME));
		add(new Label("locale", BindingModel.of(userModel, Binding.user().locale())));
		
		// User update popup
		UserFormPopupPanel userUpdatePanel = new UserFormPopupPanel("userUpdatePopupPanel", getModel());
		add(userUpdatePanel);
		
		Button updateUser = new Button("updateUser");
		updateUser.add(new AjaxModalOpenBehavior(userUpdatePanel, MouseEvent.CLICK) {
			private static final long serialVersionUID = 5414159291353181776L;
			
			@Override
			protected void onShow(AjaxRequestTarget target) {
			}
		});
		add(updateUser);
		
		// Change password popup
		ChangePasswordPopupPanel changePasswordPanel = new ChangePasswordPopupPanel("changePasswordPopupPanel", getModel());
		add(changePasswordPanel);
		
		Button changeUserPassword = new Button("changeUserPassword");
		changeUserPassword.add(new AjaxModalOpenBehavior(changePasswordPanel, MouseEvent.CLICK) {
			private static final long serialVersionUID = -7179264122322968921L;
			
			@Override
			protected void onShow(AjaxRequestTarget target) {
			}
		});
		add(changeUserPassword);
		
		// Enable user link
		add(new Link<User>("enableUser", userModel) {
			private static final long serialVersionUID = 6157423807032594861L;
			
			@Override
			public void onClick() {
				try {
					userService.setActive(getModelObject(), true);
					getSession().success(getString("administration.user.enable.success"));
				} catch (Exception e) {
					LOGGER.error("Error occured while enabling user", e);
					getSession().error(getString("common.error"));
				}
			}
			
			@Override
			protected void onConfigure() {
				super.onConfigure();
				setVisible(!getModelObject().isActive());
			}
		});
		
		// Disable user link
		IModel<String> confirmationTextModel = new StringResourceModel(
				"administration.user.disable.confirmation.text", null, 
				new Object[] { userModel.getObject().getDisplayName() }
		);
		
		add(new AjaxConfirmLink<User>("disableUser", userModel,
				new ResourceModel("administration.user.disable.confirmation.title"),
				confirmationTextModel,
				new ResourceModel("common.confirm"),
				new ResourceModel("common.cancel"),
				null, false) {
			private static final long serialVersionUID = 6157423807032594861L;
			
			@Override
			public void onClick(AjaxRequestTarget target) {
				try {
					userService.setActive(getModelObject(), false);
					getSession().success(getString("administration.user.disable.success"));
				} catch (Exception e) {
					LOGGER.error("Error occured while disabling user", e);
					getSession().error(getString("common.error"));
				}
				target.add(getPage());
				FeedbackUtils.refreshFeedback(target, getPage());
			}
			
			@Override
			protected void onConfigure() {
				super.onConfigure();
				User displayedUser = getModelObject();
				User currentUser = MavenArtifactNotifierSession.get().getUser();
				setVisible(!displayedUser.equals(currentUser) && displayedUser.isActive());
			}
		});
	}
}

@@ -1266,16 +1266,104 @@
 attern;%0A
+import fr.openwide.maven.artifact.notifier.core.business.user.model.AuthenticationType;%0A
 import f
@@ -3709,16 +3709,301 @@
 ssword%22)
+ %7B%0A%09%09%09private static final long serialVersionUID = 1L;%0A%0A%09%09%09@Override%0A%09%09%09protected void onConfigure() %7B%0A%09%09%09%09super.onConfigure();%0A%09%09%09%09User user = userModel.getObject();%0A%09%09%09%09if (user != null) %7B%0A%09%09%09%09%09setVisible(AuthenticationType.LOCAL.equals(user.getAuthenticationType()));%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D
 ;%0A%09%09chan

Allow password change only for local accounts