package org.jabref.gui.preferences;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.jabref.logic.preferences.CliPreferences;

import org.jspecify.annotations.NonNull;

public class PreferencesFilter {

    private final CliPreferences preferences;

    public PreferencesFilter(GuiPreferences preferences) {
        this.preferences = preferences;
    }

    public List<PreferenceOption> getPreferenceOptions() {
        Map<String, Object> defaults = new HashMap<>(preferences.getDefaults());
        Map<String, Object> prefs = preferences.getPreferences();

        return prefs.entrySet().stream()
                    .map(entry -> new PreferenceOption(entry.getKey(), entry.getValue(), defaults.get(entry.getKey())))
                    .collect(Collectors.toList());
    }

    public List<PreferenceOption> getDeviatingPreferences() {
        return getPreferenceOptions().stream()
                                     .filter(PreferenceOption::isChanged)
                                     .sorted()
                                     .collect(Collectors.toList());
    }

    public enum PreferenceType {
        BOOLEAN, INTEGER, DOUBLE, STRING, LIST, SET, MAP
    }

    public static class PreferenceOption implements Comparable<PreferenceOption> {

        private final String key;
        private final Object value;
        private final Optional<Object> defaultValue;
        private final PreferenceType type;

        public PreferenceOption(@NonNull String key,
                                @NonNull Object value,
                                Object defaultValue) {
            this.key = key;
            this.value = value;
            this.defaultValue = Optional.ofNullable(defaultValue);
            this.type = Objects.requireNonNull(getType(value));

            if ((defaultValue != null) && !Objects.equals(this.type, getType(defaultValue))) {
                throw new IllegalStateException("types must match between default value and value");
            }
        }

        private PreferenceType getType(Object value) {
            if (value instanceof Boolean) {
                return PreferenceType.BOOLEAN;
            } else if (value instanceof Integer) {
                return PreferenceType.INTEGER;
            } else if (value instanceof Double) {
                return PreferenceType.DOUBLE;
            } else if (value instanceof Map) {
                return PreferenceType.MAP;
            } else if (value instanceof Set) {
                return PreferenceType.SET;
            } else if (value instanceof List) {
                return PreferenceType.LIST;
            } else {
                return PreferenceType.STRING;
            }
        }

        public boolean isUnchanged() {
            return Objects.equals(value, defaultValue.orElse(null));
        }

        public boolean isChanged() {
            return !isUnchanged();
        }

        @Override
        public String toString() {
            return "%s: %s=%s (%s)".formatted(type, key, value, defaultValue.orElse("NULL"));
        }

        public String getKey() {
            return key;
        }

        public Object getValue() {
            return value;
        }

        public Optional<Object> getDefaultValue() {
            return defaultValue;
        }

        public PreferenceType getType() {
            return type;
        }

        @Override
        public int compareTo(PreferenceOption o) {
            return Objects.compare(this.key, o.key, String::compareTo);
        }
    }
}
