/*
 * 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.archive;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.aegee.runanddine.BasePage;
import org.aegee.runanddine.pathfinder.OptGroup;
import org.aegee.runanddine.runanddine.RunAndDine;
import org.aegee.runanddine.util.DateFormat;
import org.aegee.runanddine.util.YesNoLabel;
import org.aegee.runanddine.util.data.ModelNotExistingException;
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.PageableListView;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.request.mapper.parameter.PageParameters;

/**
 *
 * @author s_wolff
 */
public class ArchivePage extends BasePage {

    public ArchivePage(PageParameters pageParameters) {
        this(pageParameters, 0, 0);
    }

    public ArchivePage(PageParameters parameters, int pageSub, int pagePart) {
        super(parameters);

        List<ArchiveEntry> subscribers = ArchiveEntryManager.getInstance().getAll();
        final PageableListView<ArchiveEntry> subscriberListView = new PageableListView<ArchiveEntry>("subscribers", subscribers, 40) {

            @Override
            protected void populateItem(ListItem<ArchiveEntry> li) {
                ArchiveEntry entry = li.getModelObject();
                li.add(new Label("firstName", entry.getFirstname()));
                li.add(new Label("lastName", entry.getLastname()));
                li.add(new Label("email", entry.getEmail()));
                li.add(new YesNoLabel("flagRaD", entry.isRadMailFlag()));
                li.add(new YesNoLabel("flagAegee", entry.isAegeeMailFlag()));
            }
        };
        this.add(subscriberListView);
        RepeatingView srv = new RepeatingView("sLink");
        final List<Link> sl = new ArrayList<Link>(subscriberListView.getPageCount());
        for (int i = 0; i < subscriberListView.getPageCount(); i++) {
            final int num = i;
            Link l = new Link(srv.newChildId()) {

                @Override
                public void onClick() {
                    sl.get(subscriberListView.getCurrentPage()).setEnabled(true);
                    this.setEnabled(false);
                    subscriberListView.setCurrentPage(num);
                }
            };
            sl.add(l);
            l.setEnabled(i != subscriberListView.getCurrentPage());
            l.add(new Label("sLinkText", "" + (i + 1)));
            srv.add(l);
        }
        this.add(srv);


        List<Participant> participants = new LinkedList<Participant>();
        for (OptGroup g : OptGroup.OBJECTS.getAll()) {
            Participant.create(g, participants);
        }
        Collections.sort(participants);
        final PageableListView<Participant> participantListView = new PageableListView<Participant>("participants", participants, 40) {

            @Override
            protected void populateItem(ListItem<Participant> li) {
                Participant og = li.getModelObject();
                li.add(new Label("firstName", og.getFirstName()));
                li.add(new Label("lastName", og.getLastName()));
                li.add(new Label("email", og.getEmail()));
                li.add(new Label("rad", DateFormat.format(og.getDate())));
            }
        };
        this.add(participantListView);
        RepeatingView prv = new RepeatingView("pLink");
        final List<Link> pl = new ArrayList<Link>(participantListView.getPageCount());
        for (int i = 0; i < participantListView.getPageCount(); i++) {
            final int num = i;
            Link l = new Link(prv.newChildId()) {

                @Override
                public void onClick() {
                    pl.get(participantListView.getCurrentPage()).setEnabled(true);
                    this.setEnabled(false);
                    participantListView.setCurrentPage(num);
                }
            };
            pl.add(l);
            l.setEnabled(i != participantListView.getCurrentPage());
            l.add(new Label("pLinkText", "" + (i + 1)));
            prv.add(l);
        }
        this.add(prv);

        this.add(new BookmarkablePageLink("export", ArchiveExportPage.class));
    }

    private static class Participant implements Serializable, Comparable<Participant> {

        private String firstName;
        private String lastName;
        private String email;
        private Date rad;

        private Participant(String firstName, String lastName, String email, Date rad) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.email = email;
            this.rad = rad;
        }

        private static void create(OptGroup g, List<Participant> all) {
            try {
                RunAndDine rd = RunAndDine.OBJECTS.getById(g.getRunAndDineId());
                all.add(new Participant(g.getFirstName1(), g.getLastName1(), g.getEmail1(), rd.getDate()));
                all.add(new Participant(g.getFirstName2(), g.getLastName2(), g.getEmail2(), rd.getDate()));
            } catch (ModelNotExistingException e) {
            }
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public String getEmail() {
            return email;
        }

        public Date getDate() {
            return rad;
        }

        @Override
        public int compareTo(Participant t) {
            /*
             * negative integer, zero, or a positive integer as this object is
             * less than, equal to, or greater than the specified object.
             */
            int compareFirstNames = this.firstName.compareToIgnoreCase(t.getFirstName());
            if (compareFirstNames == 0) {
                int compareLastNames = this.lastName.compareToIgnoreCase(t.getLastName());
                if (compareLastNames == 0) {
                    return this.rad.compareTo(t.getDate());
                } else {
                    return compareLastNames;
                }
            } else {
                return compareFirstNames;
            }
        }
    }
}
