package fr.openwide.core.basicapp.web.application.history.component;

import static com.google.common.base.Predicates.equalTo;
import static fr.openwide.core.wicket.more.condition.Condition.predicate;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.panel.GenericPanel;
import org.apache.wicket.model.IModel;

import com.google.common.base.Predicates;

import fr.openwide.core.basicapp.core.business.history.model.HistoryDifference;
import fr.openwide.core.basicapp.core.util.binding.Bindings;
import fr.openwide.core.basicapp.web.application.history.component.factory.IHistoryComponentFactory;
import fr.openwide.core.jpa.more.business.history.model.atomic.HistoryDifferenceEventType;
import fr.openwide.core.wicket.behavior.ClassAttributeAppender;
import fr.openwide.core.wicket.more.markup.html.basic.EnclosureContainer;
import fr.openwide.core.wicket.more.model.BindingModel;

public class CompositeHistoryDifferencePanel extends GenericPanel<HistoryDifference> {
	private static final long serialVersionUID = 1L;
	
	public static IHistoryComponentFactory block(final IHistoryComponentFactory childFactory) {
		return new IHistoryComponentFactory() {
			private static final long serialVersionUID = 1L;
			
			@Override
			public Component create(String wicketId, IModel<HistoryDifference> parameter1) {
				return new CompositeHistoryDifferencePanel(wicketId, parameter1, "history-difference-block", childFactory);
			}
			
			@Override
			public void detach() {
				childFactory.detach();
			}
		};
	}
	
	public static IHistoryComponentFactory inline(final  IHistoryComponentFactory childFactory) {
		return new IHistoryComponentFactory() {
			private static final long serialVersionUID = 1L;
			
			@Override
			public Component create(String wicketId, IModel<HistoryDifference> parameter1) {
				return new CompositeHistoryDifferencePanel(wicketId, parameter1, "history-difference-inline", childFactory);
			}
			
			@Override
			public void detach() {
				childFactory.detach();
			}
		};
	}
	
	public CompositeHistoryDifferencePanel(String id, IModel<HistoryDifference> model, String cssClass,
			IHistoryComponentFactory historyComponentFactory) {
		super(id, model);
		
		IModel<HistoryDifferenceEventType> eventTypeModel = BindingModel.of(model, Bindings.historyDifference().eventType());
		
		add(
				new EnclosureContainer("updated").condition(predicate(eventTypeModel, equalTo(HistoryDifferenceEventType.UPDATED)))
						.add(newHistoryDifferenceListPanel("differences", cssClass, historyComponentFactory)),
				new EnclosureContainer("untouched").condition(predicate(eventTypeModel, equalTo(HistoryDifferenceEventType.UNTOUCHED))),
				new EnclosureContainer("added").condition(predicate(eventTypeModel, equalTo(HistoryDifferenceEventType.ADDED)))
						.add(newHistoryDifferenceListPanel("differences", cssClass, historyComponentFactory)),
				new EnclosureContainer("removed").condition(predicate(eventTypeModel, Predicates.equalTo(HistoryDifferenceEventType.REMOVED)))
						.add(newHistoryDifferenceListPanel("differences", cssClass, historyComponentFactory))
		);
	}

	private Component newHistoryDifferenceListPanel(String wicketId, String cssClass, IHistoryComponentFactory historyComponentFactory) {
		return
				new HistoryDifferenceListPanel(
						wicketId, BindingModel.of(getModel(), Bindings.historyDifference().differences()), historyComponentFactory
				)
				.add(new ClassAttributeAppender(cssClass));
	}
}
@@ -1628,33 +1628,32 @@
  %7B%0A%09%09return new 
-I
 HistoryComponent
@@ -1664,361 +1664,21 @@
 ory(
-) %7B%0A%09%09%09private static final long serialVersionUID = 1L;%0A%09%09%09%0A%09%09%09@Override%0A%09%09%09public Component create(String wicketId, IModel%3CHistoryDifference%3E parameter1) %7B%0A%09%09%09%09return new CompositeHistoryDifferencePanel(wicketId, parameter1, %22history-difference-inline%22, childFactory);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09@Override%0A%09%09%09public void detach() %7B%0A%09%09%09%09childFactory.detach();%0A%09%09%09%7D%0A%09%09%7D
+childFactory)
 ;%0A%09%7D
@@ -3118,9 +3118,604 @@
 s));%0A%09%7D%0A
+%09%0A%09private static class HistoryComponentFactory implements IHistoryComponentFactory %7B%0A%09%09private static final long serialVersionUID = 1L;%0A%09%09%0A%09%09private IHistoryComponentFactory childFactory;%0A%09%09%0A%09%09private HistoryComponentFactory(IHistoryComponentFactory childFactory) %7B%0A%09%09%09this.childFactory = childFactory;%0A%09%09%7D%0A%09%09%0A%09%09@Override%0A%09%09public Component create(String wicketId, IModel%3CHistoryDifference%3E parameter1) %7B%0A%09%09%09return new CompositeHistoryDifferencePanel(wicketId, parameter1, %22history-difference-inline%22, childFactory);%0A%09%09%7D%0A%09%09%0A%09%09@Override%0A%09%09public void detach() %7B%0A%09%09%09childFactory.detach();%0A%09%09%7D%0A%09%7D%0A
 %7D

Move to a private class instead of using inlining