/*
 * Copyright 2017 MovingBlocks
 *
 * 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.terasology.config.flexible;

import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.config.flexible.constraints.SettingConstraint;
import org.terasology.engine.SimpleUri;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.MessageFormat;
import java.util.Set;

/**
 * {@inheritDoc}
 *
 * @param <T> The type of the value this {@link SettingImpl} contains.
 */
public class SettingImpl<T> implements Setting<T> {
    private static final Logger LOGGER = LoggerFactory.getLogger(SettingImpl.class);
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

    private final SimpleUri id;
    private final String warningFormatString;

    private final T defaultValue;
    private final Class<T> valueClass;

    protected T value;

    private String humanReadableName;
    private String description;

    private SettingConstraint<T> constraint;
    private Set<PropertyChangeListener> subscribers;

    /**
     * Creates a new {@link SettingImpl} with the given id and default value but no constraint.
     *
     * @param id           the id of the setting.
     * @param defaultValue the default value of the setting.
     */
    public SettingImpl(SimpleUri id, T defaultValue) {
        this(id, defaultValue, null);
    }

    /**
     * Creates a new {@link SettingImpl} with the given id, default value and constraint.
     * @param id           the id of the setting.
     * @param defaultValue the default value of the setting.
     * @param constraint   the constraint that the setting values must satisfy.
     */
    @SuppressWarnings("unchecked")
    public SettingImpl(SimpleUri id, T defaultValue, SettingConstraint<T> constraint) {
        this.id = id;
        this.warningFormatString = MessageFormat.format("Setting {0}: '{'0}'", this.id);

        this.constraint = constraint;

        if (isConstraintUnsatisfiedBy(defaultValue)) {
            throw new IllegalArgumentException("The default value must be a valid value. " +
                    "Check the logs for more information.");
        }

        Preconditions.checkNotNull(defaultValue, formatWarning("The default value cannot be null."));

        this.defaultValue = defaultValue;
        this.value = this.defaultValue;
        this.valueClass = (Class<T>) defaultValue.getClass();
    }

    private String formatWarning(String s) {
        return MessageFormat.format(warningFormatString, s);
    }

    private void dispatchChangedEvent(PropertyChangeEvent event) {
        if (subscribers != null) {
            for (PropertyChangeListener subscriber : subscribers) {
                subscriber.propertyChange(event);
            }
        }
    }

    private boolean isConstraintUnsatisfiedBy(T theValue) {
        if (constraint == null || constraint.isSatisfiedBy(theValue)) {
            return false;
        } else {
            constraint.warnUnsatisfiedBy(theValue);
            return true;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean subscribe(PropertyChangeListener listener) {
        if (subscribers == null) {
            subscribers = Sets.newHashSet();
        }

        if (listener == null) {
            LOGGER.warn(formatWarning("A null subscriber cannot be added."));

            return false;
        }
        if (subscribers.contains(listener)) {
            LOGGER.warn(formatWarning("The listener has already been subscribed."));

            return false;
        }

        subscribers.add(listener);

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean unsubscribe(PropertyChangeListener listener) {
        if (!subscribers.contains(listener)) {
            LOGGER.warn(formatWarning("The listener does not exist in the subscriber list."));
            return false;
        }

        subscribers.remove(listener);

        if (subscribers.size() <= 0) {
            subscribers = null;
        }

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean hasSubscribers() {
        return subscribers != null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SimpleUri getId() {
        return id;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SettingConstraint<T> getConstraint() {
        return constraint;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public T getDefaultValue() {
        return defaultValue;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public T getValue() {
        return value;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean setValue(T newValue) {
        Preconditions.checkNotNull(newValue, formatWarning("The value of a setting cannot be null."));

        if (isConstraintUnsatisfiedBy(newValue)) {
            return false;
        }

        PropertyChangeEvent event = new PropertyChangeEvent(this, id.toString(), this.value, newValue);
        this.value = newValue;
        dispatchChangedEvent(event);

        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getHumanReadableName() {
        return humanReadableName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void setValueFromJson(String json) {
        value = GSON.fromJson(json, valueClass);
    }

    @Override
    public JsonElement getValueAsJson() {
        return GSON.toJsonTree(value);
    }
}

@@ -1746,16 +1746,22 @@
 private 
+final 
 Set%3CProp
@@ -1791,16 +1791,36 @@
 scribers
+ = Sets.newHashSet()
 ;%0A%0A    /
@@ -3373,47 +3373,8 @@
 ) %7B%0A
-        if (subscribers != null) %7B%0A    
     
@@ -3437,36 +3437,32 @@
 ) %7B%0A            
-    
 subscriber.prope
@@ -3483,22 +3483,8 @@
 t);%0A
-            %7D%0A
     
@@ -3884,99 +3884,8 @@
 ) %7B%0A
-        if (subscribers == null) %7B%0A            subscribers = Sets.newHashSet();%0A        %7D%0A%0A
     
@@ -3982,33 +3982,32 @@
 t be added.%22));%0A
-%0A
             retu
@@ -4022,24 +4022,25 @@
 ;%0A        %7D%0A
+%0A
         if (
@@ -4154,25 +4154,24 @@
 cribed.%22));%0A
-%0A
             
@@ -4598,90 +4598,8 @@
 );%0A%0A
-        if (subscribers.size() %3C= 0) %7B%0A            subscribers = null;%0A        %7D%0A%0A
     
@@ -4726,16 +4726,17 @@
  return 
+!
 subscrib
@@ -4742,16 +4742,18 @@
 bers
- != null
+.isEmpty()
 ;%0A  

Remove null usage in SettingImpl.subscribers, Make field final