package ch.difty.scipamato.core.web.newsletter.list;

import java.util.ArrayList;
import java.util.List;

import com.googlecode.wicket.jquery.ui.JQueryIcon;
import com.googlecode.wicket.jquery.ui.interaction.sortable.Sortable;
import de.agilecoders.wicket.core.markup.html.bootstrap.button.BootstrapAjaxButton;
import de.agilecoders.wicket.core.markup.html.bootstrap.button.Buttons;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.PageReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ch.difty.scipamato.core.entity.newsletter.Newsletter;
import ch.difty.scipamato.core.entity.newsletter.NewsletterNewsletterTopic;
import ch.difty.scipamato.core.persistence.NewsletterTopicService;
import ch.difty.scipamato.core.web.common.BasePage;
import ch.difty.scipamato.core.web.paper.list.PaperListPage;

public class NewsletterTopicSortPage extends BasePage<Newsletter> {
    private static final long serialVersionUID = 1L;

    private       List<NewsletterNewsletterTopic> topics = new ArrayList<>();
    private final PageReference                   previousPageRef;

    @SpringBean
    private NewsletterTopicService service;

    public NewsletterTopicSortPage(final IModel<Newsletter> model) {
        this(model, null);
    }

    public NewsletterTopicSortPage(final IModel<Newsletter> model, final PageReference previous) {
        super(model);
        this.previousPageRef = previous;
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();

        if (getModelObject() != null)
            topics = service.getSortedNewsletterTopicsForNewsletter(getModelObject().getId());

        queue(newHeader("header"));
        queue(new Form<Void>("form"));
        queue(newSortable("sortable", topics));
        queue(newSubmitButton("submit"));
        queue(newCancelButton("cancel"));
    }

    private Label newHeader(final String id) {
        final StringResourceModel model = new StringResourceModel(id + ".label", this, getModel());
        return new Label(id, model);
    }

    private void alignSortToIndex(final List<NewsletterNewsletterTopic> topics) {
        int index = 0;
        for (final NewsletterNewsletterTopic t : topics)
            t.setSort(index++);
    }

    private Sortable<NewsletterNewsletterTopic> newSortable(final String id,
        final List<NewsletterNewsletterTopic> list) {
        return new Sortable<>(id, list) {

            private static final long serialVersionUID = 1L;

            @Override
            protected HashListView<NewsletterNewsletterTopic> newListView(
                IModel<List<NewsletterNewsletterTopic>> model) {
                return NewsletterTopicSortPage.newListView("items", model);
            }
        };
    }

    protected static Sortable.HashListView<NewsletterNewsletterTopic> newListView(String id,
        IModel<List<NewsletterNewsletterTopic>> model) {
        return new Sortable.HashListView<NewsletterNewsletterTopic>(id, model) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<NewsletterNewsletterTopic> item) {
                item.add(
                    new EmptyPanel("icon").add(AttributeModifier.append("class", "ui-icon " + JQueryIcon.ARROW_2_N_S)));
                item.add(new Label("item", item
                    .getModelObject()
                    .getDisplayValue()));
                item.add(AttributeModifier.append("class", "ui-state-default"));
            }

        };
    }

    private BootstrapAjaxButton newSubmitButton(final String id) {
        return new BootstrapAjaxButton(id, new StringResourceModel(id + ".label", this, null), Buttons.Type.Primary) {

            @Override
            protected void onSubmit(final AjaxRequestTarget target) {
                super.onSubmit(target);
                alignSortToIndex(topics);

                try {
                    service.saveSortedNewsletterTopics(NewsletterTopicSortPage.this
                        .getModelObject()
                        .getId(), topics);
                } catch (Exception ex) {
                    error("Unexpected error: " + ex.getMessage());
                }

                if (previousPageRef != null)
                    setResponsePage(previousPageRef.getPage());
                else
                    setResponsePage(PaperListPage.class);
            }
        };
    }

    private BootstrapAjaxButton newCancelButton(final String id) {
        return new BootstrapAjaxButton(id, new StringResourceModel(id + ".label", this, null), Buttons.Type.Default) {

            @Override
            protected void onSubmit(final AjaxRequestTarget target) {
                super.onSubmit(target);
                if (previousPageRef != null)
                    setResponsePage(previousPageRef.getPage());
                else
                    setResponsePage(PaperListPage.class);
            }
        };
    }
}

@@ -1252,16 +1252,17 @@
 tter%3E %7B%0A
+%0A
     priv
@@ -1303,24 +1303,75 @@
 nUID = 1L;%0A%0A
+    private static final String LABEL = %22.label%22;%0A%0A
     private 
@@ -2380,32 +2380,29 @@
 eModel(id + 
-%22.label%22
+LABEL
 , this, getM
@@ -4085,32 +4085,29 @@
 eModel(id + 
-%22.label%22
+LABEL
 , this, null
@@ -4986,24 +4986,21 @@
 el(id + 
-%22.label%22
+LABEL
 , this, 

Extract constant