/*
 * Copyright Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags and
 * the COPYRIGHT.txt file distributed with this work.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.komodo.metadata.internal;

import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import org.komodo.spi.constants.StringConstants;
import org.komodo.spi.runtime.TeiidTranslator;
import org.teiid.adminapi.AdminObject;
import org.teiid.adminapi.Translator;
import org.teiid.core.util.ArgCheck;

public class TeiidTranslatorImpl implements Comparable<TeiidTranslatorImpl>, TeiidTranslator, StringConstants {

    private String name;

    private String type;

    private String description;

    private Properties properties = new Properties();

    /**
     * @param translator
     * @param propDefs
     * @param teiidInstance
     */
    public TeiidTranslatorImpl(Translator translator) {
        ArgCheck.isNotNull(translator, "translator"); //$NON-NLS-1$

        this.name = translator.getName();
        this.type = translator.getType();
        this.description = translator.getDescription();

        setProperties(translator.getProperties());
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(TeiidTranslatorImpl translator) {
        ArgCheck.isNotNull(translator, "translator"); //$NON-NLS-1$
        return getName().compareTo(translator.getName());
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        TeiidTranslatorImpl other = (TeiidTranslatorImpl)obj;
        if (description == null) {
            if (other.description != null)
                return false;
        } else if (!description.equals(other.description))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (properties == null) {
            if (other.properties != null)
                return false;
        } else if (!properties.equals(other.properties))
            return false;
        if (type == null) {
            if (other.type != null)
                return false;
        } else if (!type.equals(other.type))
            return false;
        return true;
    }

    /**
     * {@inheritDoc}
     * 
     * @see AdminObject#getName()
     */
    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    /**
     * {@inheritDoc}
     * 
     * @see AdminObject#getProperties()
     */
    @Override
    public Properties getProperties() {
        return properties;
    }

    /**
     * @return type
     */
    @Override
    public String getType() {
        return type;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((description == null) ? 0 : description.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((properties == null) ? 0 : properties.hashCode());
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }

    /**
     * @param changedProperties the list of properties that are being changed (never <code>null</code> or empty)
     * @throws Exception if there is a problem changing the properties
     *
     */
    @Override
    public void setProperties(Properties changedProperties) {
        ArgCheck.isNotNull(changedProperties, "changedProperties"); //$NON-NLS-1$
        Set<Entry<Object, Object>> entrySet = changedProperties.entrySet();
        ArgCheck.isNotEmpty(entrySet, "changedProperties"); //$NON-NLS-1$

        for (Entry<Object, Object> entry : entrySet) {
            properties.setProperty((String)entry.getKey(), (String)entry.getValue());
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return getName() + SPACE + COLON + SPACE + getType();
    }
}
