package org.monospark.spongematchers.matcher.sponge;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

import org.monospark.spongematchers.matcher.SpongeMatcher;
import org.spongepowered.api.data.DataQuery;
import org.spongepowered.api.data.DataView;

import com.google.common.collect.Maps;

public final class DataViewMatcher implements SpongeMatcher<DataView> {

    public static DataViewMatcher create(SpongeMatcher<Map<String, Object>> matcher) {
        return new DataViewMatcher(matcher);
    }
    
    private SpongeMatcher<Map<String, Object>> matcher;

    private DataViewMatcher(SpongeMatcher<Map<String, Object>> matcher) {
        this.matcher = matcher;
    }

    @Override
    public boolean matches(DataView v) {
        return matcher.matches(toMap(v));
    }
    
    private Map<String, Object> toMap(DataView view) {
        Map<String, Object> map = Maps.newHashMap();
        for (Entry<DataQuery, Object> entry : view.getValues(false).entrySet()) {
            String name = entry.getKey().getParts().get(entry.getKey().getParts().size() - 1);
            Optional<DataView> dataView = view.getView(entry.getKey());
            if (dataView.isPresent()) {
                map.put(name, dataView.get());
            } else {
                map.put(name, entry.getValue());
            }
        }
        return map;
    }
}

@@ -1303,16 +1303,30 @@
 t(name, 
+makeMatchable(
 entry.ge
@@ -1334,16 +1334,17 @@
 Value())
+)
 ;%0A      
@@ -1387,10 +1387,451 @@
 ;%0A    %7D%0A
+    %0A    private Object makeMatchable(Object o) %7B%0A        if (o instanceof Byte) %7B%0A            return ((Byte) o).longValue();%0A        %7D else if (o instanceof Short) %7B%0A            return ((Short) o).longValue();%0A        %7D else if (o instanceof Integer) %7B%0A            return ((Integer) o).longValue();%0A        %7D else if(o instanceof Float) %7B%0A            return ((Float) o).doubleValue();%0A        %7D else %7B%0A            return o;%0A        %7D%0A    %7D%0A
 %7D%0A

Fix bugs with some primitive values in DataViews