package com.suse.salt.netapi.calls.modules;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.suse.salt.netapi.calls.LocalCall;

import com.google.gson.reflect.TypeToken;

/**
 * salt.modules.saltutil
 */
public class SaltUtil {

    public static LocalCall<List<String>> syncGrains(
            Optional<Boolean> refresh, Optional<String> saltenv) {
        LinkedHashMap<String, Object> args = syncArgs(refresh, saltenv);
        return new LocalCall<>("saltutil.sync_grains", Optional.empty(),
                Optional.of(args), new TypeToken<List<String>>() {
                });
    }

    public static LocalCall<List<String>> syncModules(
            Optional<Boolean> refresh, Optional<String> saltenv) {
        LinkedHashMap<String, Object> args = syncArgs(refresh, saltenv);
        return new LocalCall<>("saltutil.sync_modules", Optional.empty(),
                Optional.of(args), new TypeToken<List<String>>() {
                });
    }

    public static LocalCall<Map<String, Object>> syncAll(
            Optional<Boolean> refresh, Optional<String> saltenv) {
        LinkedHashMap<String, Object> args = syncArgs(refresh, saltenv);
        return new LocalCall<>("saltutil.sync_all", Optional.empty(),
                Optional.of(args), new TypeToken<Map<String, Object>>() {
                });
    }

    public static LocalCall<Boolean> refreshPillar(
            Optional<Boolean> refresh, Optional<String> saltenv) {
        LinkedHashMap<String, Object> args = syncArgs(refresh, saltenv);
        return new LocalCall<>("saltutil.refresh_pillar", Optional.empty(),
                Optional.of(args), new TypeToken<Boolean>() {
                });
    }

    private static LinkedHashMap<String, Object> syncArgs(
            Optional<Boolean> refresh, Optional<String> saltenv) {
        LinkedHashMap<String, Object> args = new LinkedHashMap<>();
        refresh.ifPresent(value -> args.put("refresh", value));
        saltenv.ifPresent(value -> args.put("saltenv", value));
        return args;
    }

}

@@ -139,24 +139,111 @@
 .Optional;%0A%0A
+import com.google.gson.JsonElement;%0Aimport com.google.gson.annotations.SerializedName;%0A
 import com.s
@@ -317,16 +317,63 @@
 peToken;
+%0Aimport com.suse.salt.netapi.parser.JsonParser;
 %0A%0A/**%0A *
@@ -2224,10 +2224,1344 @@
 %0A    %7D%0A%0A
+    /**%0A     * Info about a running job on a minion%0A     */%0A    public static class RunningInfo %7B%0A        private String jid;%0A        private String fun;%0A        private int pid;%0A        private String target;%0A        @SerializedName(%22tgt_type%22)%0A        private String targetType;%0A        private String user;%0A        private Optional%3CJsonElement%3E metadata = Optional.empty();%0A%0A        public %3CR%3E Optional%3CR%3E getMetadata(Class%3CR%3E type) %7B%0A            return metadata.map(json -%3E JsonParser.GSON.fromJson(json, type));%0A        %7D%0A%0A        public %3CR%3E Optional%3CR%3E getMetadata(TypeToken%3CR%3E type) %7B%0A            return metadata.map(json -%3E JsonParser.GSON.fromJson(json, type.getType()));%0A        %7D%0A%0A        public String getJid() %7B%0A            return jid;%0A        %7D%0A%0A        public String getFun() %7B%0A            return fun;%0A        %7D%0A%0A        public int getPid() %7B%0A            return pid;%0A        %7D%0A%0A        public String getTarget() %7B%0A            return target;%0A        %7D%0A%0A        public String getTargetType() %7B%0A            return targetType;%0A        %7D%0A%0A        public String getUser() %7B%0A            return user;%0A        %7D%0A    %7D%0A%0A    public static LocalCall%3CList%3CRunningInfo%3E%3E running() %7B%0A        return new LocalCall%3C%3E(%22saltutil.running%22,%0A                Optional.empty(), Optional.empty(), new TypeToken%3CList%3CRunningInfo%3E%3E() %7B%7D);%0A    %7D%0A%0A
 %7D%0A

add saltutil.running