package de.jotomo.ruffyscripter.commands;

import org.monkey.d.ruffy.ruffy.driver.display.MenuAttribute;
import org.monkey.d.ruffy.ruffy.driver.display.MenuType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.jotomo.ruffyscripter.RuffyScripter;

// TODO robustness: can a TBR run out, whilst we're trying to cancel it?
public class CancelTbrCommand implements Command {
    private static final Logger log = LoggerFactory.getLogger(CancelTbrCommand.class);

    @Override
    public CommandResult execute(RuffyScripter scripter) {
        scripter.verifyMenuIsDisplayed(MenuType.MAIN_MENU);
        Double tbrPercentage = (Double) scripter.currentMenu.getAttribute(MenuAttribute.TBR);
        boolean runtimeDisplayed = scripter.currentMenu.attributes().contains(MenuAttribute.RUNTIME);
        if (tbrPercentage == 100 && !runtimeDisplayed) {
            return new CommandResult()
                    .success(true)
                    .enacted(true) // technically, nothing was enacted, but AAPS needs this to recover
                                   // when there was an issue and AAPS thinks a TBR is still active
                    .message("No TBR active");
        }
        log.debug("Cancelling active TBR of " + tbrPercentage + "% with " + runtimeDisplayed + "min remaining");
        return new SetTbrCommand(100, 0).execute(scripter);
    }

    @Override
    public String toString() {
        return "CancelTbrCommand{}";
    }
}

@@ -541,24 +541,42 @@
 scripter) %7B%0A
+        try %7B%0A    
         scri
@@ -631,16 +631,20 @@
         
+  
+  
 Double t
@@ -721,16 +721,20 @@
 e.TBR);%0A
+    
         
@@ -827,16 +827,20 @@
 NTIME);%0A
+    
         
@@ -892,32 +892,36 @@
 ) %7B%0A            
+    
 return new Comma
@@ -951,16 +951,20 @@
         
+    
 .success
@@ -990,16 +990,20 @@
         
+ 
+   
 .enacted
@@ -1101,27 +1101,16 @@
         
-       
-    
 // when 
@@ -1186,16 +1186,20 @@
         
+    
 .message
@@ -1225,18 +1225,26 @@
         
+ 
-%7D%0A
+   %7D%0A    
         
@@ -1344,24 +1344,113 @@
 emaining%22);%0A
+        %7D catch (CommandException e) %7B%0A            return e.toCommandResult();%0A        %7D%0A
         retu

Make CancelTbrCommand more robust.