package com.libreecm.db.jpa.properties;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.libreecm.commonAPI.model.objects.ExtensionsData;
import com.libreecm.commonAPI.model.objects.properties.PropertyDefinition;

/**
 * Defines a property
 * @author Jordi Pérez
 *
 * @param <T>
 */
public abstract class PropertyData<T>  extends ExtensionsData{

    private static final long serialVersionUID = 1L;

    private String id;
    private String displayName;
    private String localName;
    private String queryName;
    private PropertyDefinition<T> propDef;

    private List<T> values = Collections.emptyList();

    public PropertyDefinition<T> getPropertyDefinition() {
        return propDef;
    }

    public void setPropertyDefinition(PropertyDefinition<T> propDef) {
        this.propDef = propDef;
        if (propDef != null) {
            this.id = propDef.getId();
            this.displayName = propDef.getDisplayName();
            this.localName = propDef.getLocalName();
            this.queryName = propDef.getQueryName();
        } else {
            this.id = null;
            this.displayName = null;
            this.localName = null;
            this.queryName = null;
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getLocalName() {
        return localName;
    }

    public void setLocalName(String localName) {
        this.localName = localName;
    }

    public String getQueryName() {
        return queryName;
    }

    public void setQueryName(String queryName) {
        this.queryName = queryName;
    }

    public List<T> getValues() {
        return values;
    }

    public void setValues(List<T> values) {
        if (values == null) {
            this.values = Collections.emptyList();
        } else {
            this.values = values;
        }
    }

    public void setValue(T value) {
        if (value == null) {
            values = Collections.emptyList();
        } else {
            values = new ArrayList<T>(1);
            values.add(value);
        }
    }

    public T getFirstValue() {
        if (values != null && !values.isEmpty()) {
            return values.get(0);
        }

        return null;
    }

    @Override
    public String toString() {
        return "Property [id=" + id + ", display Name=" + displayName + ", local name=" + localName + ", query name="
                + queryName + ", values=" + values + "]" + super.toString();
    }

}
