/*
 * Copyright 2017 Red Hat, Inc. and/or its affiliates.
 *
 * 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.jbpm.workbench.common.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.jboss.errai.common.client.api.annotations.NonPortable;
import org.uberfire.mvp.Command;
import org.uberfire.paging.AbstractPageRow;

public abstract class GenericSummary<T> extends AbstractPageRow implements Serializable {

    private static final long serialVersionUID = -8709434225517887911L;

    protected T id;
    protected String name;
    private transient List<LabeledCommand> callbacks;

    public GenericSummary() {
    }

    public GenericSummary(final T id,
                          final String name) {
        this.id = id;
        this.name = name;
    }

    public T getId() {
        return id;
    }

    public void setId(T id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<LabeledCommand> getCallbacks() {
        return callbacks;
    }

    public void setCallbacks(List<LabeledCommand> callbacks) {
        this.callbacks = callbacks;
    }

    public boolean hasCallbacks() {
        return callbacks != null && callbacks.size() > 0;
    }

    public void addCallback(String label, Command command) {
        if (callbacks == null) {
            callbacks = new ArrayList<>();
        }
        callbacks.add(new LabeledCommand(label, command));
    }

    @Override
    @SuppressWarnings("PMD.AvoidMultipleUnaryOperators")
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = ~~result;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = ~~result;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        GenericSummary other = (GenericSummary) obj;
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "GenericSummary{" +
                "id=" + id +
                ", name='" + name + '\'' +
                "} " + super.toString();
    }

    @NonPortable
    public class LabeledCommand {

        private String label;

        private Command command;

        public LabeledCommand() {
        }

        public LabeledCommand(String label,
                              Command command) {
            this.label = label;
            this.command = command;
        }

        public Command getCommand() {
            return command;
        }

        public String getLabel() {
            return label;
        }
    }
}