/*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
package org.opendaylight.controller.yang.model.util;

import java.net.URI;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.opendaylight.controller.yang.common.QName;
import org.opendaylight.controller.yang.model.api.SchemaPath;
import org.opendaylight.controller.yang.model.api.Status;
import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition;

/**
 * The <code>default</code> implementation of Enumertaion Type Definition interface.
 * 
 * @see EnumTypeDefinition
 */
public class EnumerationType implements EnumTypeDefinition {

    private final QName name = BaseTypes.constructQName("enumeration");
    private final SchemaPath path;
    private final String description = "The enumeration built-in type represents values from a set of assigned names.";
    private final String reference = "https://tools.ietf.org/html/rfc6020#section-9.6";

    private final EnumPair defaultEnum;
    private final List<EnumPair> enums;
    private String units = "";
    private final EnumTypeDefinition baseType;
    
    private EnumerationType(final List<EnumPair> enums) {
        this.path = BaseTypes.schemaPath(name);
        this.enums = Collections.unmodifiableList(enums);
        this.defaultEnum = null;
        baseType = this;
    }
    
    public EnumerationType(final List<String> actualPath, final URI namespace,
            final Date revision, final List<EnumPair> enums) {
        super();
        this.path = BaseTypes.schemaPath(actualPath, namespace, revision);
        this.enums = Collections.unmodifiableList(enums);
        this.defaultEnum = null;
        baseType = new EnumerationType(enums);
    }

    public EnumerationType(final List<String> actualPath, final URI namespace,
            final Date revision, final EnumTypeDefinition baseType, final EnumPair defaultEnum,
            final List<EnumPair> enums, final String units) {
        super();
        this.path = BaseTypes.schemaPath(actualPath, namespace, revision);
        this.baseType = baseType;
        this.defaultEnum = defaultEnum;
        this.enums = Collections.unmodifiableList(enums);
        this.units = units;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.opendaylight.controller.yang.model.api.TypeDefinition#getBaseType()
     */
    @Override
    public EnumTypeDefinition getBaseType() {
        return baseType;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.opendaylight.controller.yang.model.api.TypeDefinition#getUnits()
     */
    @Override
    public String getUnits() {
        return units;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.opendaylight.controller.yang.model.api.TypeDefinition#getDefaultValue()
     */
    @Override
    public Object getDefaultValue() {
        return defaultEnum;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.opendaylight.controller.yang.model.api.SchemaNode#getQName()
     */
    @Override
    public QName getQName() {
        return name;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.opendaylight.controller.yang.model.api.SchemaNode#getPath()
     */
    @Override
    public SchemaPath getPath() {
        return path;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.opendaylight.controller.yang.model.api.SchemaNode#getDescription()
     */
    @Override
    public String getDescription() {
        return description;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.opendaylight.controller.yang.model.api.SchemaNode#getReference()
     */
    @Override
    public String getReference() {
        return reference;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.opendaylight.controller.yang.model.api.SchemaNode#getStatus()
     */
    @Override
    public Status getStatus() {
        return Status.CURRENT;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.opendaylight.controller.yang.model.base.type.api.EnumTypeDefinition#getValues()
     */
    @Override
    public List<EnumPair> getValues() {
        return enums;
    }

    @Override
    public List<UnknownSchemaNode> getUnknownSchemaNodes() {
        return Collections.emptyList();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((defaultEnum == null) ? 0 : defaultEnum.hashCode());
        result = prime * result
                + ((description == null) ? 0 : description.hashCode());
        result = prime * result + ((enums == null) ? 0 : enums.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((path == null) ? 0 : path.hashCode());
        result = prime * result
                + ((reference == null) ? 0 : reference.hashCode());
        result = prime * result + ((units == null) ? 0 : units.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        EnumerationType other = (EnumerationType) obj;
        if (defaultEnum == null) {
            if (other.defaultEnum != null) {
                return false;
            }
        } else if (!defaultEnum.equals(other.defaultEnum)) {
            return false;
        }
        if (description == null) {
            if (other.description != null) {
                return false;
            }
        } else if (!description.equals(other.description)) {
            return false;
        }
        if (enums == null) {
            if (other.enums != null) {
                return false;
            }
        } else if (!enums.equals(other.enums)) {
            return false;
        }
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        if (path == null) {
            if (other.path != null) {
                return false;
            }
        } else if (!path.equals(other.path)) {
            return false;
        }
        if (reference == null) {
            if (other.reference != null) {
                return false;
            }
        } else if (!reference.equals(other.reference)) {
            return false;
        }
        if (units == null) {
            if (other.units != null) {
                return false;
            }
        } else if (!units.equals(other.units)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("EnumerationType [name=");
        builder.append(name);
        builder.append(", path=");
        builder.append(path);
        builder.append(", description=");
        builder.append(description);
        builder.append(", reference=");
        builder.append(reference);
        builder.append(", defaultEnum=");
        builder.append(defaultEnum);
        builder.append(", enums=");
        builder.append(enums);
        builder.append(", units=");
        builder.append(units);
        builder.append("]");
        return builder.toString();
    }
}
