package net.minecraft.launcher.updater;

import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.launcher.game.process.GameProcessBuilder;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.minecraft.launcher.CompatibilityRule;
import org.apache.commons.lang3.text.StrSubstitutor;

public class Argument {
   private final String[] values;
   private final List<CompatibilityRule> compatibilityRules;

   public Argument(String[] values, List<CompatibilityRule> compatibilityRules) {
      this.values = values;
      this.compatibilityRules = compatibilityRules;
   }

   public void apply(GameProcessBuilder output, CompatibilityRule.FeatureMatcher featureMatcher, StrSubstitutor substitutor) {
      if (this.appliesToCurrentEnvironment(featureMatcher)) {
         String[] var4 = this.values;
         int var5 = var4.length;

         for(int var6 = 0; var6 < var5; ++var6) {
            String value = var4[var6];
            output.withArguments(substitutor.replace(value));
         }
      }

   }

   public boolean appliesToCurrentEnvironment(CompatibilityRule.FeatureMatcher featureMatcher) {
      if (this.compatibilityRules == null) {
         return true;
      } else {
         CompatibilityRule.Action lastAction = CompatibilityRule.Action.DISALLOW;
         Iterator var3 = this.compatibilityRules.iterator();

         while(var3.hasNext()) {
            CompatibilityRule compatibilityRule = (CompatibilityRule)var3.next();
            CompatibilityRule.Action action = compatibilityRule.getAppliedAction(featureMatcher);
            if (action != null) {
               lastAction = action;
            }
         }

         return lastAction == CompatibilityRule.Action.ALLOW;
      }
   }

   public static class Serializer implements JsonDeserializer<Argument> {
      public Argument deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
         if (json.isJsonPrimitive()) {
            return new Argument(new String[]{json.getAsString()}, (List)null);
         } else {
            JsonArray array;
            try {
               if (json.isJsonObject()) {
                  JsonObject obj = json.getAsJsonObject();
                  JsonElement value = obj.get("values");
                  String[] values;
                  if (value.isJsonPrimitive()) {
                     values = new String[]{value.getAsString()};
                  } else {
                     JsonArray array = value.getAsJsonArray();
                     values = new String[array.size()];

                     for(int i = 0; i < array.size(); ++i) {
                        values[i] = array.get(i).getAsString();
                     }
                  }

                  List<CompatibilityRule> rules = new ArrayList();
                  if (obj.has("compatibilityRules")) {
                     array = obj.getAsJsonArray("compatibilityRules");
                     Iterator var20 = array.iterator();

                     while(var20.hasNext()) {
                        JsonElement element = (JsonElement)var20.next();
                        rules.add((CompatibilityRule)context.deserialize(element, CompatibilityRule.class));
                     }
                  }

                  return new Argument(values, rules);
               }
            } catch (Exception var12) {
               if (json.isJsonObject()) {
                  JsonObject obj = json.getAsJsonObject();
                  JsonElement value = obj.get("value");
                  String[] values;
                  if (value.isJsonPrimitive()) {
                     values = new String[]{value.getAsString()};
                  } else {
                     array = value.getAsJsonArray();
                     values = new String[array.size()];

                     for(int i = 0; i < array.size(); ++i) {
                        values[i] = array.get(i).getAsString();
                     }
                  }

                  List<CompatibilityRule> rules = new ArrayList();
                  if (obj.has("rules")) {
                     JsonArray array = obj.getAsJsonArray("rules");
                     Iterator var10 = array.iterator();

                     while(var10.hasNext()) {
                        JsonElement element = (JsonElement)var10.next();
                        rules.add((CompatibilityRule)context.deserialize(element, CompatibilityRule.class));
                     }
                  }

                  return new Argument(values, rules);
               }
            }

            throw new JsonParseException("Invalid argument, must be object or string");
         }
      }
   }
}
