/*
 * Copyright (C) 2015 LibreCCM Foundation.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */
package org.libreccm.configuration;

import static org.libreccm.core.CoreConstants.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Objects;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;

/**
 * A setting class for storing a set of strings. This can be used to generate
 * enums which can be configured by the administrator.
 *
 * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
 */
@Entity
public class EnumSetting
    extends AbstractSetting<Set<String>> implements Serializable {

    private static final long serialVersionUID = 1763168269981687340L;

    @ElementCollection(fetch = FetchType.EAGER)
    @JoinTable(name = "SETTINGS_ENUM_VALUES",
               schema = DB_SCHEMA,
               joinColumns = {
                   @JoinColumn(name = "ENUM_ID")})
    private Set<String> value;

    @Override
    public Set<String> getValue() {
        if (value == null) {
            return null;
        } else {
            return new HashSet<>(value);
        }
    }

    @Override
    public void setValue(final Set<String> value) {
        this.value = value;
    }

    public void addEnumValue(final String value) {
        if (value == null) {
            this.value = new HashSet<>();
        }
        this.value.add(value);
    }

    public void removeEnumValue(final String value) {
        if (this.value != null) {
            this.value.remove(value);
        }
    }

    @Override
    public int hashCode() {
        int hash = super.hashCode();
        hash = 89 * hash + Objects.hashCode(value);
        return hash;
    }

    @Override
    public boolean equals(final Object obj) {
        if (!super.equals(obj)) {
            return false;
        }
        if (obj == null) {
            return false;
        }

        if (!(obj instanceof EnumSetting)) {
            return false;
        }
        final EnumSetting other = (EnumSetting) obj;
        if (!other.canEqual(this)) {
            return false;
        }

        return Objects.equals(value, other.getValue());
    }

    @Override
    public boolean canEqual(final Object obj) {
        return obj instanceof EnumSetting;
    }

    @Override
    public String toString(final String data) {
        final StringBuffer enumValues = new StringBuffer();
        enumValues.append("{ ");
        if (value != null) {
            final List<String> values = new ArrayList<>(value);
            values.forEach((String v) -> {
                enumValues.append('\"').append(v).append('\"');
                if (values.indexOf(v) != values.size() - 1) {
                    enumValues.append(", ");
                }
            });
        }
        enumValues.append(" }");

        return super.toString(String.format(", value = %s%s",
                                            enumValues.toString(),
                                            data));
    }

}
