package be.iminds.ilabt.jfed.lowlevel.user;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

/**
 * hack to be able to see if preferences changed.
 * not using a real Preferences implementation, only the very small subset that is used is in here
 *
 */
public class UserLoginModelPreferences {

    private final Map<String, Object> map = new HashMap<>();

    public UserLoginModelPreferences() {
    }

    public void save(PropertiesConfiguration config) {
        try {
            config.clear();

            for (Map.Entry<String, Object> e : map.entrySet()) {
                if (e.getValue() == null) continue;
                if (e.getValue() instanceof Boolean) {
                    Boolean v = (Boolean) e.getValue();
                    config.setProperty(e.getKey(), v);
                } else {
                    config.setProperty(e.getKey(), e.getValue().toString());
                }
            }

            config.save();
        } catch (ConfigurationException e) {
            throw new RuntimeException("Problem saving login configuration", e);
        }
    }

    public void load(PropertiesConfiguration config) {
        Iterator<String> keys = config.getKeys();

        map.clear();

        while (keys.hasNext()) {
            String key = keys.next();
            Object val = config.getProperty(key);
            if (val != null && val instanceof String) {
                String sval = (String) val;
                if (sval.equalsIgnoreCase("true") || sval.equalsIgnoreCase("false")) {
                    map.put(key, sval.equalsIgnoreCase("true"));
                } else {
                    map.put(key, sval);
                }
            }
            if (val != null && val instanceof Boolean) {
                Boolean bval = (Boolean) val;
                map.put(key, bval);

            }
        }
    }

    public void put(String key, String value) {
        map.put(key, value);
    }

    public void putBoolean(String key, boolean value) {
        map.put(key, value);
    }

    public String get(String key, String defaultValue) {
        Object r = map.get(key);
        if (r == null) return defaultValue;
        return r.toString();
    }

    public String get(String key) {
        Object r = map.get(key);
        if (r != null)
            return r.toString();
        else
            return null;
    }

    public Boolean getBoolean(String key, Boolean defaultValue) {
        Object v = map.get(key);
        if (v != null && v instanceof Boolean)
            return (Boolean) v;
        return defaultValue;
    }

    public boolean contains(String key) {
        return map.containsKey(key);
    }

    public void remove(String key) {
        map.remove(key);
    }

    @Override
    public String toString() {
        return "UserLoginModelPreferences" + map + "";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserLoginModelPreferences that = (UserLoginModelPreferences) o;

        if (!Objects.equals(map, that.map)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return map.hashCode();
    }
}
