/*
 * Copyright 2016 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.collect.Lists;
import org.terasology.config.flexible.validators.SettingValueValidator;
import org.terasology.engine.SimpleUri;
import org.terasology.utilities.subscribables.GeneralSubscribable;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

public class Setting<T> implements GeneralSubscribable {
    private final T defaultValue;

    private SimpleUri id;

    private T value;

    private String name;

    private String description;
    private SettingValueValidator<T> validator;
    private List<PropertyChangeListener> subscribers;

    public Setting(SimpleUri id, T defaultValue) {
        this(id, defaultValue, null);
    }

    public Setting(SimpleUri id, T defaultValue, SettingValueValidator<T> validator) {
        this.id = id;

        this.validator = validator;

        if (!validate(defaultValue))
            throw new IllegalArgumentException("The default value must be a valid value.");

        this.defaultValue = defaultValue;
        this.value = this.defaultValue;

        this.subscribers = null;
    }

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

    private boolean validate(T value) {
        return validator == null || validator.validate(value);
    }

    public void subscribe(PropertyChangeListener listener) {
        if (subscribers == null) {
            subscribers = Lists.newArrayList();
        }

        subscribers.add(listener);
    }

    public void unsubscribe(PropertyChangeListener listener) {
        subscribers.remove(listener);

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

    public boolean hasSubscribers() {
        return subscribers != null;
    }

    public SimpleUri getId() {
        return id;
    }

    public SettingValueValidator<T> getValidator() {
        return validator;
    }

    public T getDefaultValue() {
        return defaultValue;
    }

    public T getValue() {
        return value;
    }

    public boolean setValue(T value) {
        if (!validator.validate(value))
            return false;

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

        this.value = value;

        dispatchChangedEvent(event);

        return true;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }
}

@@ -1116,17 +1116,30 @@
  String 
-n
+humanReadableN
 ame;%0A%0A  
@@ -2832,25 +2832,28 @@
  setValue(T 
-v
+newV
 alue) %7B%0A    
@@ -2876,25 +2876,28 @@
 or.validate(
-v
+newV
 alue))%0A     
@@ -3011,17 +3011,20 @@
 .value, 
-v
+newV
 alue);%0A%0A
@@ -3044,17 +3044,20 @@
 value = 
-v
+newV
 alue;%0A%0A 
@@ -3142,16 +3142,29 @@
 ring get
+HumanReadable
 Name() %7B
@@ -3179,17 +3179,30 @@
  return 
-n
+humanReadableN
 ame;%0A   

Rename Setting.name to humanReadableName