package models.jsondb;

import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import edu.emory.mathcs.backport.java.util.Arrays;

public class PropKey implements Comparable<PropKey> {
    
    public String name;
    public boolean collection;
    public boolean injected;
    public String type;
    public Set<String> annotations = new LinkedHashSet<>();
    public boolean function;
    public String table;

    public static PropKey[] getParts(String propertyName) {
        String[] parts = propertyName.trim().split("\\.");
        PropKey[] key = new PropKey[parts.length];
        for (int i = 0; i < parts.length; i++) {
            key[i] = new PropKey(parts[i]);
        }
        return key;
    }
    
    public PropKey(String key) {
        String typePart = StringUtils.substringBefore(key, "@");
        String annotationsPart = StringUtils.substringAfter(key, "@");
        annotations.addAll(Arrays.asList(annotationsPart.split("@")));
        collection = typePart.contains("[]");
        function = typePart.contains("()");
        injected = typePart.contains("*");
        typePart = StringUtils.remove(typePart, "[]");
        typePart = StringUtils.remove(typePart, "()");
        typePart = StringUtils.remove(typePart, "*");
        String[] typeParts = typePart.split(":");
        name = typeParts[0];
        type = (typeParts.length>1)?typeParts[1]:"";
        table = (typeParts.length>2)?typeParts[2]:null;
    }

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

    @Override
    public boolean equals(Object obj) {
        return name.equals(((PropKey) obj).name);
    }

    @Override
    public int compareTo(PropKey o) {
        return name.compareTo(o.name);
    }
}