package org.safehaus.subutai.core.environment.api.helper;


import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.UUID;

import org.safehaus.subutai.common.util.UUIDUtil;
import org.safehaus.subutai.core.peer.api.ContainerHost;


public class Environment
{

    private UUID id;
    private String name;
    private Set<ContainerHost> containers;
    private EnvironmentStatusEnum status;
    private long creationTimestamp;


    public Environment( String name )
    {
        this.name = name;
        this.id = UUIDUtil.generateTimeBasedUUID();
        this.containers = new HashSet<>();
        this.status = EnvironmentStatusEnum.EMPTY;
        this.creationTimestamp = System.currentTimeMillis();
    }


    public long getCreationTimestamp()
    {
        return creationTimestamp;
    }


    public EnvironmentStatusEnum getStatus()
    {
        return status;
    }


    public void setStatus( final EnvironmentStatusEnum status )
    {
        this.status = status;
    }


    public void addContainer( ContainerHost container )
    {
        container.setEnvironmentId( id );
        this.containers.add( container );
    }


    public Set<ContainerHost> getContainers()
    {
        return containers;
    }


    public void setContainers( final Set<ContainerHost> containers )
    {
        this.containers = containers;
    }


    //TODO implement this method after migrating to new domain model
    public Set<ContainerHost> getContainerHosts()
    {
        return Collections.EMPTY_SET;
    }


    public String getName()
    {
        return name;
    }


    public UUID getId()
    {
        return id;
    }


    public ContainerHost getContainerHostByUUID( final UUID uuid ) {
        Iterator<ContainerHost> iterator = containers.iterator();
        while ( iterator.hasNext() ) {
            ContainerHost containerHost = iterator.next();
            if ( containerHost.getAgent().getUuid().equals( uuid ) )
                return containerHost;
        }
        return null;
    }


    public ContainerHost getContainerHostByHostname( String hostname )
    {
        Iterator<ContainerHost> iterator = containers.iterator();
        while ( iterator.hasNext() ) {
            ContainerHost containerHost = iterator.next();
            if ( containerHost.getHostname().equals( hostname ) )
                return containerHost;
        }
        return null;
    }


    public ContainerHost getContainerHostByHostname( final String hostname )
    {
        Iterator<ContainerHost> iterator = containers.iterator();
        iterator.next();
        while ( iterator.hasNext() ) {
            ContainerHost containerHost = iterator.next();
            if ( containerHost.getHostname().equals( hostname ) )
                return containerHost;
        }
        return null;
    }


    /*public void invoke( PeerCommandMessage commandMessage )
    {
        try
        {
            EnvironmentManager environmentManager = this.serviceLocator.getServiceNoCache( EnvironmentManager.class );
            environmentManager.invoke( commandMessage );
        }
        catch ( NamingException e )
        {
            commandMessage.setProccessed( true );
            commandMessage.setExceptionMessage( e.toString() );
            //            commandMessage.setSuccess( false );
        }
    }*/
}

@@ -2470,423 +2470,8 @@
 %7D%0A%0A%0A
-    public ContainerHost getContainerHostByHostname( final String hostname )%0A    %7B%0A        Iterator%3CContainerHost%3E iterator = containers.iterator();%0A        iterator.next();%0A        while ( iterator.hasNext() ) %7B%0A            ContainerHost containerHost = iterator.next();%0A            if ( containerHost.getHostname().equals( hostname ) )%0A                return containerHost;%0A        %7D%0A        return null;%0A    %7D%0A%0A%0A
     

fix merge conflicts in Environment class