package net.minecraft.launcher;

import com.mojang.launcher.OperatingSystem;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CompatibilityRule {
   private CompatibilityRule.Action action;
   private CompatibilityRule.OSRestriction os;
   private Map<String, Object> features;

   public CompatibilityRule() {
      this.action = CompatibilityRule.Action.ALLOW;
   }

   public CompatibilityRule(CompatibilityRule compatibilityRule) {
      this.action = CompatibilityRule.Action.ALLOW;
      this.action = compatibilityRule.action;
      if (compatibilityRule.os != null) {
         this.os = new CompatibilityRule.OSRestriction(compatibilityRule.os);
      }

      if (compatibilityRule.features != null) {
         this.features = compatibilityRule.features;
      }

   }

   public CompatibilityRule.Action getAppliedAction(CompatibilityRule.FeatureMatcher featureMatcher) {
      if (this.os != null && !this.os.isCurrentOperatingSystem()) {
         return null;
      } else {
         if (this.features != null) {
            if (featureMatcher == null) {
               return null;
            }

            Iterator var2 = this.features.entrySet().iterator();

            while(var2.hasNext()) {
               Entry<String, Object> feature = (Entry)var2.next();
               if (!featureMatcher.hasFeature((String)feature.getKey(), feature.getValue())) {
                  return null;
               }
            }
         }

         return this.action;
      }
   }

   public CompatibilityRule.Action getAction() {
      return this.action;
   }

   public CompatibilityRule.OSRestriction getOs() {
      return this.os;
   }

   public Map<String, Object> getFeatures() {
      return this.features;
   }

   public String toString() {
      return "Rule{action=" + this.action + ", os=" + this.os + ", features=" + this.features + '}';
   }

   public class OSRestriction {
      private OperatingSystem name;
      private String version;
      private String arch;

      public OSRestriction() {
      }

      public OperatingSystem getName() {
         return this.name;
      }

      public String getVersion() {
         return this.version;
      }

      public String getArch() {
         return this.arch;
      }

      public OSRestriction(CompatibilityRule.OSRestriction osRestriction) {
         this.name = osRestriction.name;
         this.version = osRestriction.version;
         this.arch = osRestriction.arch;
      }

      public boolean isCurrentOperatingSystem() {
         if (this.name != null && this.name != OperatingSystem.getCurrentPlatform()) {
            return false;
         } else {
            Pattern pattern;
            Matcher matcher;
            if (this.version != null) {
               try {
                  pattern = Pattern.compile(this.version);
                  matcher = pattern.matcher(System.getProperty("os.version"));
                  if (!matcher.matches()) {
                     return false;
                  }
               } catch (Throwable var4) {
               }
            }

            if (this.arch != null) {
               try {
                  pattern = Pattern.compile(this.arch);
                  matcher = pattern.matcher(System.getProperty("os.arch"));
                  if (!matcher.matches()) {
                     return false;
                  }
               } catch (Throwable var3) {
               }
            }

            return true;
         }
      }

      public String toString() {
         return "OSRestriction{name=" + this.name + ", version='" + this.version + '\'' + ", arch='" + this.arch + '\'' + '}';
      }
   }

   public interface FeatureMatcher {
      boolean hasFeature(String var1, Object var2);
   }

   public static enum Action {
      ALLOW,
      DISALLOW;
   }
}
