package org.cobbzilla.util.dns;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.Set;

import static org.cobbzilla.util.daemon.ZillaRuntime.empty;

@NoArgsConstructor @Accessors(chain=true) @ToString(of={"pattern", "fqdns", "subdomain"}, callSuper=true)
public class DnsRecordMatch extends DnsRecordBase {

    @Getter @Setter private String pattern;
    @Getter @Setter private Set<String> fqdns;
    public boolean hasFqdns () { return fqdns != null && !fqdns.isEmpty(); }

    public DnsRecordMatch(Set<String> fqdns) { this.fqdns = fqdns; }

    public boolean hasPattern() { return !empty(pattern); }

    @Getter @Setter private String subdomain;
    public boolean hasSubdomain() { return !empty(subdomain); }

    public DnsRecordMatch(DnsRecordBase record) {
        super(record.getFqdn(), record.getType(), record.getValue());
    }

    public DnsRecordMatch(DnsType type, String fqdn) {
        setType(type);
        setFqdn(fqdn);
    }

    public DnsRecordMatch(String fqdn) { this(null, fqdn); }

    public boolean matches (DnsRecord record) {
        if (hasType() && !getType().equals(record.getType())) return false;
        if (hasFqdn() && !getFqdn().equals(record.getFqdn())) return false;
        if (hasSubdomain() && record.hasFqdn() && !record.getFqdn().endsWith(getSubdomain())) return false;
        if (hasPattern() && record.hasFqdn() && !record.getFqdn().matches(getPattern())) return false;
        if (hasFqdns() && record.hasFqdn() && !getFqdns().contains(record.getFqdn())) return false;
        return true;
    }

}

@@ -22,24 +22,76 @@
 .util.dns;%0A%0A
+import com.fasterxml.jackson.annotation.JsonIgnore;%0A
 import lombo
@@ -96,24 +96,24 @@
 bok.Getter;%0A
-
 import lombo
@@ -239,16 +239,48 @@
 til.Set;
+%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -493,16 +493,16 @@
 Base %7B%0A%0A
-
     @Get
@@ -537,16 +537,137 @@
 attern;%0A
+    @JsonIgnore @Getter(lazy=true) private final Pattern _pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);%0A%0A
     @Get
@@ -1683,45 +1683,53 @@
 && !
-record.getFqdn().matches(getPattern()
+get_pattern().matcher(record.getFqdn()).find(
 )) r

improve pattern matching in DnsRecordMatch