package org.safehaus.subutai.core.peer.impl.container;


import java.util.concurrent.Callable;

import org.safehaus.subutai.common.command.CommandUtil;
import org.safehaus.subutai.common.command.RequestBuilder;
import org.safehaus.subutai.core.peer.api.ResourceHost;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;


public class DestroyContainerTask implements Callable
{
    //    private static final String CONTAINER_DOES_NOT_EXIST = "Container \"%s\" does NOT exist";
    //    private static final String CONTAINER_DESTROYED = "Destruction of \"%s\" completed successfully";
    private static final int DESTROY_TIMEOUT = 180;

    private final ResourceHost resourceHost;
    private final String hostname;
    private CommandUtil commandUtil = new CommandUtil();


    public DestroyContainerTask( final ResourceHost resourceHost, final String hostname )
    {
        Preconditions.checkNotNull( resourceHost );
        Preconditions.checkArgument( !Strings.isNullOrEmpty( hostname ) );

        this.resourceHost = resourceHost;
        this.hostname = hostname;
    }


    @Override
    public Object call() throws Exception
    {
        //        final Semaphore semaphore = new Semaphore( 0 );

//        final StringBuilder out = new StringBuilder();

        RequestBuilder destroyCommand =
                new RequestBuilder( "subutai destroy" ).withCmdArgs( Lists.newArrayList( hostname ) )
                                                       .withTimeout( DESTROY_TIMEOUT );

        commandUtil.execute( destroyCommand, resourceHost );

        //        commandUtil.executeAsync( destroyCommand, resourceHost, new CommandUtil.StoppableCallback()
        //        {
        //            @Override
        //            public void onResponse( final Response response, final CommandResult commandResult )
        //            {
        //                out.append( commandResult.getStdOut() );
        //                if ( commandResult.getStdOut().contains( String.format( CONTAINER_DESTROYED, hostname ) )
        //                        || commandResult.getStdOut().contains( String.format( CONTAINER_DOES_NOT_EXIST,
        // hostname ) ) )
        //                {
        //                    semaphore.release();
        //                    stop();
        //                }
        //            }
        //        } );


        //        if ( !semaphore.tryAcquire( DESTROY_TIMEOUT + 3, TimeUnit.SECONDS ) )
        //        {
        //            throw new ContainerDestructionException(
        //                    String.format( "Unexpected command result while destroying container: %s", out ) );
        //        }

        return null;
    }
}

@@ -447,216 +447,8 @@
 e%0A%7B%0A
-    //    private static final String CONTAINER_DOES_NOT_EXIST = %22Container %5C%22%25s%5C%22 does NOT exist%22;%0A    //    private static final String CONTAINER_DESTROYED = %22Destruction of %5C%22%25s%5C%22 completed successfully%22;%0A
     
@@ -495,16 +495,16 @@
  = 180;%0A
+
 %0A    pri
@@ -1008,132 +1008,8 @@
    %7B
-%0A        //        final Semaphore semaphore = new Semaphore( 0 );%0A%0A//        final StringBuilder out = new StringBuilder();
 %0A%0A  
@@ -1233,24 +1233,24 @@
 TIMEOUT );%0A%0A
+
         comm
@@ -1303,1124 +1303,8 @@
 );%0A%0A
-        //        commandUtil.executeAsync( destroyCommand, resourceHost, new CommandUtil.StoppableCallback()%0A        //        %7B%0A        //            @Override%0A        //            public void onResponse( final Response response, final CommandResult commandResult )%0A        //            %7B%0A        //                out.append( commandResult.getStdOut() );%0A        //                if ( commandResult.getStdOut().contains( String.format( CONTAINER_DESTROYED, hostname ) )%0A        //                        %7C%7C commandResult.getStdOut().contains( String.format( CONTAINER_DOES_NOT_EXIST,%0A        // hostname ) ) )%0A        //                %7B%0A        //                    semaphore.release();%0A        //                    stop();%0A        //                %7D%0A        //            %7D%0A        //        %7D );%0A%0A%0A        //        if ( !semaphore.tryAcquire( DESTROY_TIMEOUT + 3, TimeUnit.SECONDS ) )%0A        //        %7B%0A        //            throw new ContainerDestructionException(%0A        //                    String.format( %22Unexpected command result while destroying container: %25s%22, out ) );%0A        //        %7D%0A%0A
     

Fix bug with IP & destroy