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

import java.util.Collection;

import org.aegee.runanddine.BasePage;
import org.aegee.runanddine.pathfinder.OptGroup;
import org.aegee.runanddine.pathfinder.groupbuilding.GroupBuilderOverviewPage;
import org.aegee.runanddine.pathfinder.optimizers.PathOptimizer;
import org.aegee.runanddine.runanddine.RunAndDine;
import org.aegee.runanddine.status.RunAndDineStatus;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;

/**
 * Displays current status of assignment building for a RunAndDine
 * @author s_wolff
 */
public class OptimizationOverviewPage extends BasePage {

    public OptimizationOverviewPage(final RunAndDine event) {
        super(null);

        Collection<OptGroup> optGroups = OptGroup.OBJECTS.getAllByRunAndDineId(event.getId());

        WebMarkupContainer succ = new WebMarkupContainer("goOnWrapper");
        succ.add(new Label("numGroups", "" + optGroups.size()));
        succ.add(new Link("linkGoOn") {

            @Override
            public void onClick() {
                RunAndDineStatus.setStatusInformation(event.getId(), OptimizationOverviewPage.class, "Optimization running...");
                PathOptimizer.optimizeGroupAssignment(event);
                RunAndDineStatus.setStatusInformation(event.getId(), OptimizationOverviewPage.class, "Optimization finished.");
                this.setResponsePage(new GroupsAndAssignmentEditor(event));
            }
        });
        this.add(succ);

        WebMarkupContainer err = new WebMarkupContainer("error");
        err.add(new Link("linkGB") {

            @Override
            public void onClick() {
                this.setResponsePage(new GroupBuilderOverviewPage(event));
            }
        });
        this.add(err);

        succ.setVisible(optGroups.size() % 3 == 0);
        err.setVisible(!succ.isVisible());
    }
}
