// 
// Decompiled by Procyon v0.6.0
// 

package org.bson.codecs.pojo;

import java.util.Iterator;
import org.bson.codecs.configuration.CodecConfigurationException;
import org.bson.codecs.pojo.annotations.BsonProperty;
import java.util.HashMap;
import java.util.Map;

final class InstanceCreatorImpl<T> implements InstanceCreator<T>
{
    private final CreatorExecutable<T> creatorExecutable;
    private final Map<PropertyModel<?>, Object> cachedValues;
    private final Map<String, Integer> properties;
    private final Object[] params;
    private T newInstance;
    
    InstanceCreatorImpl(final CreatorExecutable<T> creatorExecutable) {
        this.creatorExecutable = creatorExecutable;
        if (creatorExecutable.getProperties().isEmpty()) {
            this.cachedValues = null;
            this.properties = null;
            this.params = null;
            this.newInstance = creatorExecutable.getInstance();
        }
        else {
            this.cachedValues = new HashMap<PropertyModel<?>, Object>();
            this.properties = new HashMap<String, Integer>();
            for (int i = 0; i < creatorExecutable.getProperties().size(); ++i) {
                if (creatorExecutable.getIdPropertyIndex() != null && creatorExecutable.getIdPropertyIndex() == i) {
                    this.properties.put("_id", creatorExecutable.getIdPropertyIndex());
                }
                else {
                    this.properties.put(creatorExecutable.getProperties().get(i).value(), i);
                }
            }
            this.params = new Object[this.properties.size()];
        }
    }
    
    @Override
    public <S> void set(final S value, final PropertyModel<S> propertyModel) {
        if (this.newInstance != null) {
            propertyModel.getPropertyAccessor().set(this.newInstance, value);
        }
        else {
            if (!this.properties.isEmpty()) {
                String propertyName = propertyModel.getWriteName();
                if (!this.properties.containsKey(propertyName)) {
                    propertyName = propertyModel.getName();
                }
                final Integer index = this.properties.get(propertyName);
                if (index != null) {
                    this.params[index] = value;
                }
                this.properties.remove(propertyName);
            }
            if (this.properties.isEmpty()) {
                this.constructInstanceAndProcessCachedValues();
            }
            else {
                this.cachedValues.put(propertyModel, value);
            }
        }
    }
    
    @Override
    public T getInstance() {
        if (this.newInstance == null) {
            try {
                for (final Map.Entry<String, Integer> entry : this.properties.entrySet()) {
                    this.params[entry.getValue()] = null;
                }
                this.constructInstanceAndProcessCachedValues();
            }
            catch (final CodecConfigurationException e) {
                throw new CodecConfigurationException(String.format("Could not construct new instance of: %s. Missing the following properties: %s", this.creatorExecutable.getType().getSimpleName(), this.properties.keySet()), e);
            }
        }
        return this.newInstance;
    }
    
    private void constructInstanceAndProcessCachedValues() {
        try {
            this.newInstance = this.creatorExecutable.getInstance(this.params);
        }
        catch (final Exception e) {
            throw new CodecConfigurationException(e.getMessage(), e);
        }
        for (final Map.Entry<PropertyModel<?>, Object> entry : this.cachedValues.entrySet()) {
            this.setPropertyValue(entry.getKey(), entry.getValue());
        }
    }
    
    private <S> void setPropertyValue(final PropertyModel<S> propertyModel, final Object value) {
        this.set(value, (PropertyModel<Object>)propertyModel);
    }
}
