package org.batfish.datamodel.questions;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.batfish.common.BatfishException;
import org.batfish.datamodel.Interface;

/**
 * Enables specification of groups of interfaces in various questions.
 *
 * <p>Currently supported example specifiers:
 *
 * <ul>
 *   <li>Loopback.* —> all interfaces with matching names
 *   <li>desc:KK.* -> all interfaces whose description matches KK.*
 * </ul>
 *
 * <p>In the future, we might need other tags and boolean expressions over tags.
 */
public class InterfacesSpecifier {

  public enum Type {
    DESC,
    NAME,
    VRF
  }

  public static InterfacesSpecifier ALL = new InterfacesSpecifier(".*");

  private final String _expression;

  private final Pattern _regex;

  private final Type _type;

  public InterfacesSpecifier(String expression) {
    _expression = expression;

    String[] parts = expression.split(":");

    if (parts.length == 1) {
      _type = Type.NAME;
      _regex = Pattern.compile(_expression);
    } else if (parts.length == 2) {
      try {
        _type = Type.valueOf(parts[0].toUpperCase());
        _regex = Pattern.compile(parts[1]);
      } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
            "Illegal InterfacesSpecifier filter "
                + parts[0]
                + ".  Should be one of "
                + Arrays.stream(Type.values())
                    .map(v -> v.toString())
                    .collect(Collectors.joining(", ")));
      }
    } else {
      throw new IllegalArgumentException("Cannot parse InterfacesSpecifier " + expression);
    }
  }

  @JsonIgnore
  public Pattern getRegex() {
    return _regex;
  }

  @JsonIgnore
  public Type getType() {
    return _type;
  }

  public boolean matches(Interface iface) {
    switch (_type) {
    case DESC:
      return _regex.matcher(iface.getDescription()).matches();
    case NAME:
      return _regex.matcher(iface.getName()).matches();
    case VRF:
      return _regex.matcher(iface.getVrfName()).matches();
    default:
      throw new BatfishException("Unhandled InterfacesSpecifier type: " + _type);
    }
  }

  @JsonValue
  public String toString() {
    return _expression;
  }
}

@@ -1983,24 +1983,26 @@
 type) %7B%0A    
+  
 case DESC:%0A 
@@ -2000,16 +2000,18 @@
 e DESC:%0A
+  
       re
@@ -2065,24 +2065,26 @@
 ches();%0A    
+  
 case NAME:%0A 
@@ -2082,16 +2082,18 @@
 e NAME:%0A
+  
       re
@@ -2140,24 +2140,26 @@
 ches();%0A    
+  
 case VRF:%0A  
@@ -2156,16 +2156,18 @@
 se VRF:%0A
+  
       re
@@ -2221,16 +2221,18 @@
 ();%0A    
+  
 default:
@@ -2232,16 +2232,18 @@
 efault:%0A
+  
       th

fix java format