/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.syncope.client.console.wicket.markup.html.form;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

/**
 * This empty class must exist because there not seems to be alternative to provide specialized HTML for edit links.
 *
 * @param <T> model object type.
 */
public final class ActionsPanel<T extends Serializable> extends Panel {

    private static final long serialVersionUID = 322966537010107771L;

    private final List<Action<T>> actions = new ArrayList<>();

    private IModel<T> model;

    public ActionsPanel(final String componentId, final IModel<T> model) {
        super(componentId, model);
        setOutputMarkupId(true);
        this.model = model;

        add(new ListView<Action<T>>("actionRepeater", actions) {

            private static final long serialVersionUID = -9180479401817023838L;

            @Override
            protected void populateItem(final ListItem<Action<T>> item) {
                item.add(new ActionPanel<>(ActionsPanel.this.model, item.getModelObject()));
            }

        }.setRenderBodyOnly(true));
    }

    public Action<T> add(
            final ActionLink<T> link,
            final ActionLink.ActionType type,
            final String entitlements) {
        return add(link, type, entitlements, false);
    }

    public Action<T> add(
            final ActionLink<T> link,
            final ActionLink.ActionType type,
            final String entitlements,
            final boolean onConfirm) {

        final Action<T> action = new Action<>(link, type);
        action.setEntitlements(entitlements);
        action.setOnConfirm(onConfirm);
        actions.add(action);
        return action;
    }

    public ActionsPanel<T> clone(final String componentId, final IModel<T> model) {
        final ActionsPanel<T> panel = new ActionsPanel<>(componentId, model);
        panel.actions.addAll(actions);
        return panel;
    }

    /**
     * Use this with toggle panels.
     * @param componentId Component Id.
     * @param model Model.
     * @return Actions panel.
     */
    public ActionsPanel<T> cloneWithLabels(final String componentId, final IModel<T> model) {
        final ActionsPanel<T> panel = new ActionsPanel<>(componentId, model);
        for (Action<T> action : actions) {
            panel.actions.add(action.showLabel());
        }
        return panel;
    }

    public boolean isEmpty() {
        return this.actions.isEmpty();
    }
}