package org.rundeck.client.tool.commands.enterprise.cluster;

import picocli.CommandLine;
import org.rundeck.client.api.model.ExecutionMode;
import org.rundeck.client.tool.InputError;
import org.rundeck.client.tool.commands.enterprise.BaseExtension;
import org.rundeck.client.tool.commands.enterprise.api.EnterpriseApi;
import org.rundeck.client.tool.commands.enterprise.api.model.EnterpriseModeResponse;
import org.rundeck.client.tool.extension.RdTool;
import retrofit2.Call;

import java.io.IOException;
import java.util.function.BiFunction;

@CommandLine.Command(
        name = "mode"
)
public class Mode extends BaseExtension {
    @CommandLine.Option(names = {"-q", "--quiet"}, description = "Reduce output.")
    boolean quiet;
    @CommandLine.Option(names = {"-u", "--uuid"}, description = "Cluster member UUID", required = true)
    String uuid;

    @CommandLine.Command(description = "Set cluster member execution mode Active")
    public boolean active() throws IOException, InputError {
        return changeMode(ExecutionMode.active, EnterpriseApi::executionModeEnable);
    }


    @CommandLine.Command(description = "Set cluster member execution mode Passive")
    public boolean passive() throws IOException, InputError {
        return changeMode(ExecutionMode.passive, EnterpriseApi::executionModeDisable);
    }

    boolean changeMode(
            final ExecutionMode expected,
            final BiFunction<EnterpriseApi, String, Call<EnterpriseModeResponse>> operation
    )
            throws InputError, IOException {
        RdTool.apiVersionCheck("change cluster member execution mode", 41, getClient().getApiVersion());

        if (!quiet) {
            getOutput().info(String.format("Setting Execution Mode to %s for cluster member %s...", expected, uuid));
        }

        EnterpriseModeResponse mode = getClient().apiCall((e) -> operation.apply(e, uuid));

        if (!quiet) {
            getOutput().info(String.format("Execution Mode change is %s for cluster member %s:", mode.getStatus(), uuid));
            getOutput().output(mode.getExecutionMode());
        }

        return expected.equals(mode.getExecutionMode());
    }

}

@@ -55,16 +55,60 @@
 uster;%0A%0A
+import lombok.Getter;%0Aimport lombok.Setter;%0A
 import p
@@ -629,53 +629,170 @@
 ode%22
-%0A)%0Apublic class Mode extends BaseExtension %7B%0A
+,%0A        description = %22Change Execution Mode of Cluster Members%22%0A)%0Apublic class Mode extends BaseExtension %7B%0A    @Getter @Setter%0A    static class Options %7B%0A    
     
@@ -870,16 +870,20 @@
 tput.%22)%0A
+    
     bool
@@ -893,16 +893,20 @@
  quiet;%0A
+    
     @Com
@@ -1001,16 +1001,20 @@
 = true)%0A
+    
     Stri
@@ -1021,16 +1021,22 @@
 ng uuid;
+%0A    %7D
 %0A%0A    @C
@@ -1134,24 +1134,58 @@
 lean active(
+@CommandLine.Mixin Options options
 ) throws IOE
@@ -1250,24 +1250,33 @@
 Mode.active,
+ options,
  EnterpriseA
@@ -1420,16 +1420,50 @@
 passive(
+@CommandLine.Mixin Options options
 ) throws
@@ -1537,16 +1537,25 @@
 passive,
+ options,
  Enterpr
@@ -1658,16 +1658,51 @@
 pected,%0A
+            final Options options,%0A
         
@@ -1947,37 +1947,49 @@
 ;%0A%0A        if (!
-q
+options.isQ
 uiet
+()
 ) %7B%0A            
@@ -2086,20 +2086,33 @@
 pected, 
-u
+options.getU
 uid
+()
 ));%0A    
@@ -2202,20 +2202,33 @@
 pply(e, 
-u
+options.getU
 uid
+()
 ));%0A%0A   
@@ -2237,21 +2237,33 @@
    if (!
-q
+options.isQ
 uiet
+()
 ) %7B%0A    
@@ -2373,20 +2373,33 @@
 atus(), 
-u
+options.getU
 uid
+()
 ));%0A    

update cluster mode options behavior