/*
 * Copyright 2012 s_wolff.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aegee.runanddine.registration;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.aegee.runanddine.BasePage;
import org.aegee.runanddine.runanddine.RunAndDine;
import org.aegee.runanddine.util.YesNoLabel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.request.mapper.parameter.PageParameters;

/**
 * Page to display all Single- and GroupRegistrations for a RunAndDine
 *
 * @author s_wolff
 */
public class ParticipantManager extends BasePage {

    public ParticipantManager(RunAndDine event) {
        this(new PageParameters("id=" + event.getId()));
    }

    public ParticipantManager(PageParameters parameters) {
        super(parameters);
        final int id = parameters.get("id").toInt();

        this.add(new BookmarkablePageLink("newSingle", SingleRegistrationEditor.class, new PageParameters().add("new", "true").add("id", id)));
        this.add(new BookmarkablePageLink("newGroup", GroupRegistrationEditor.class, new PageParameters().add("new", "true").add("id", id)));

        ListView<SingleRegistration> singleRegistrationListView = new ListView<SingleRegistration>("rowSingle", getSingleRegistrationList(id)) {

            @Override
            protected void populateItem(ListItem<SingleRegistration> li) {
                final SingleRegistration reg = li.getModelObject();
                li.add(new Label("firstName", reg.getFirstName()));
                li.add(new Label("lastName", reg.getLastName()));
                li.add(new Label("email", reg.getEmail()));
                li.add(new YesNoLabel("validated", reg.isIsValidated()));
                li.add(new YesNoLabel("accomodation", reg.isAccommodation()));
                li.add(new YesNoLabel("comment", reg.getComment() != null && !reg.getComment().trim().equals("")));
                li.add(new YesNoLabel("tv", reg.isTvAllowed()));
                li.add(new BookmarkablePageLink("linkEdit", SingleRegistrationEditor.class,
                        new PageParameters().add("id", reg.getId())));
                li.add(new Link("linkDelete") {

                    @Override
                    public void onClick() {
                        SingleRegistration.OBJECTS.delete(reg);
                        super.setResponsePage(ParticipantManager.class, new PageParameters().add("id", id));
                    }
                });
            }
        };
        this.add(singleRegistrationListView);

        ListView<GroupRegistration> groupRegistrationListView = new ListView<GroupRegistration>("rowGroup", getGroupRegistrationList(id)) {

            @Override
            protected void populateItem(ListItem<GroupRegistration> li) {
                final GroupRegistration reg = li.getModelObject();
                li.add(new Label("firstName1", reg.getFirstName1()));
                li.add(new Label("firstName2", reg.getFirstName2()));
                li.add(new Label("lastName1", reg.getLastName1()));
                li.add(new Label("lastName2", reg.getLastName2()));
                li.add(new Label("email1", reg.getEmail1()));
                li.add(new Label("email2", reg.getEmail2()));
                li.add(new YesNoLabel("validated1", reg.isIsValidated1()));
                li.add(new YesNoLabel("validated2", reg.isIsValidated2()));
                li.add(new YesNoLabel("accomodation1", reg.isAccommodation1()));
                li.add(new YesNoLabel("accomodation2", reg.isAccommodation2()));
                li.add(new YesNoLabel("tv", reg.isTvAllowed()));
                li.add(new YesNoLabel("comment", reg.getComment() != null && !reg.getComment().trim().equals("")));
                li.add(new BookmarkablePageLink("linkEdit", GroupRegistrationEditor.class,
                        new PageParameters("id=" + reg.getId())));
                li.add(new Link("linkDelete") {

                    @Override
                    public void onClick() {
                        GroupRegistration.OBJECTS.delete(reg);
                        super.setResponsePage(ParticipantManager.class, new PageParameters().add("id", id));
                    }
                });
            }
        };
        this.add(groupRegistrationListView);

    }

    private List<SingleRegistration> getSingleRegistrationList(int id) {
        // TODO: sort

        List<SingleRegistration> list = SingleRegistration.OBJECTS.getAllByRunAndDineId(id);
        Collections.sort(list, new Comparator<SingleRegistration>() {

            public int compare(SingleRegistration t, SingleRegistration r) {
                // abort on first inequality (!= 0)
                // compare first name first
                // then compare last name
                // in the end compare id -> no two single regs are compared to 0
                int compFirst = t.getFirstName().compareToIgnoreCase(r.getFirstName());
                if (compFirst == 0) {
                    int compLast = t.getLastName().compareToIgnoreCase(r.getLastName());
                    if (compLast == 0) {
                        return new Integer(t.getId()).compareTo(r.getId());
                    } else {
                        return compLast;
                    }
                } else {
                    return compFirst;
                }
            }
        });
        return list;
    }

    private List<GroupRegistration> getGroupRegistrationList(int id) {
        List<GroupRegistration> list = GroupRegistration.OBJECTS.getAllByRunAndDineId(id);
        Collections.sort(list, new Comparator<GroupRegistration>() {

            public int compare(GroupRegistration t, GroupRegistration r) {
                // abort on first inequality (!= 0)
                // compare first name1 first
                // then compare last name1
                // then compare first name2
                // then compare last name2
                // in the end compare id1 -> no two single regs are compared to 0
                int compFirst1 = t.getFirstName1().compareToIgnoreCase(r.getFirstName1());
                if (compFirst1 == 0) {
                    int compLast1 = t.getLastName1().compareToIgnoreCase(r.getLastName1());
                    if (compLast1 == 0) {
                        int compFirst2 = t.getFirstName2().compareToIgnoreCase(r.getFirstName2());
                        if (compFirst2 == 0) {
                            int compLast2 = t.getLastName2().compareToIgnoreCase(r.getLastName2());
                            if (compLast2 == 0) {
                                return new Integer(t.getId()).compareTo(r.getId());
                            } else {
                                return compLast2;
                            }
                        } else {
                            return compFirst2;
                        }
                    } else {
                        return compLast1;
                    }
                } else {
                    return compFirst1;
                }
            }
        });
        return list;
    }
}
