/*
 * Copyright (C) 2011 0xlab - http://0xlab.org/
 *
 * 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.
 *
 * Authored by Kan-Ru Chen <kanru@0xlab.org>
 */

package org.zeroxlab.aster;

import org.zeroxlab.aster.AsterOperation;
import org.zeroxlab.wookieerunner.ScriptRunner;

import com.android.monkeyrunner.MonkeyRunnerOptions;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.script.SimpleBindings;

import org.python.core.PyException;

public abstract class AsterCommand {
    private static ScriptRunner mRunner;
    protected static int mSeqNext = 0;
    protected int mSerial = 0;
    protected boolean mLandscape = false;
    protected BufferedImage mImage = null;
    protected AsterOperation[] mOps;

    public class ExecutionResult {
        public boolean mSuccess;
        public String mMessage;

        public ExecutionResult() {
        }

        public ExecutionResult(boolean success, String message) {
            mSuccess = success;
            mMessage = message;
        }
    }

    static public void setScriptRunner(ScriptRunner runner) {
        mRunner = runner;
    }

    protected String[] splitArgs(String argline) {
        String[] args = argline.split(",");
        for (int i = 0; i < args.length; ++i)
            args[i] = args[i].replaceAll("^[() ]+","").replaceAll("[() ]$","");
        return args;
    }

    protected String stripQuote(String src) {
        return src.replaceAll("'", "").replaceAll("\"", "");
    }

    public BufferedImage getImage() {
        return mImage;
    }

    public void saveImage(String prefix) throws IOException {
        if (mImage != null) {
            File pngfile = new File(prefix, String.format("%d.png", mSerial));
            pngfile.delete();
            ImageIO.write(mImage, "png", pngfile);
        }
    }

    /* Return operations that stored in this Command */
    public AsterOperation[] getOperations() {
        return mOps;
    }

    /* Get name of command */
    public abstract String getName();

    /* Get settings of a command */
    public abstract SimpleBindings getSettings();

    /* Set settings of a command */
    public abstract void fillSettings(SimpleBindings settings);

    /* Dump command to script text */
    protected abstract String toScript();

    /* Execute command */
    public ExecutionResult execute() {
        try {
            mRunner.runStringLocal(toScript());
        } catch (PyException e) {
            return new ExecutionResult(false, e.toString());
        }
        return new ExecutionResult(true, "");
    }

    public interface CommandListener {
        public void commandFinished(AsterCommand whichOne);
    }
}

@@ -1314,16 +1314,58 @@
 %5B%5D mOps;
+%0A    protected boolean mExecuting = false;
 %0A%0A    pu
@@ -1739,32 +1739,210 @@
  runner;%0A    %7D%0A%0A
+    synchronized public void setExecuting(boolean status) %7B%0A        mExecuting = status;%0A    %7D%0A%0A    synchronized public boolean isExecuting() %7B%0A        return mExecuting;%0A    %7D%0A%0A
     protected St
@@ -3009,16 +3009,35 @@
 ettings)
+ throws IOException
 ;%0A%0A    /
@@ -3173,24 +3173,52 @@
 execute() %7B%0A
+        setExecuting(true);%0A
         try 
@@ -3297,24 +3297,57 @@
 eption e) %7B%0A
+            setExecuting(false);%0A
             
@@ -3401,24 +3401,53 @@
 ;%0A        %7D%0A
+        setExecuting(false);%0A
         retu
@@ -3526,16 +3526,16 @@
 tener %7B%0A
-
         
@@ -3591,11 +3591,123 @@
 );%0A    %7D
+%0A%0A    public interface CommandExecutionLister %7B%0A        public void processResult(ExecutionResult result);%0A    %7D
 %0A%7D%0A

Add Execution state.