package ru.kirillius.pf.sdn.core.Networking;

import lombok.*;
import ru.kirillius.json.JSONArrayProperty;
import ru.kirillius.json.JSONProperty;
import ru.kirillius.json.JSONSerializable;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Container for grouped network identifiers used to configure filtering and subscriptions.
 */
@AllArgsConstructor
@NoArgsConstructor
@Builder
@JSONSerializable
public class NetworkResourceBundle {
    @Getter
    @Setter
    @JSONProperty(required = false)
    private String description = "";
    @Getter
    @Setter
    @JSONArrayProperty(type = Integer.class)
    private List<Integer> ASN = new ArrayList<>();
    @Getter
    @Setter
    @JSONArrayProperty(type = IPv4Subnet.class, serializer = IPv4Subnet.Serializer.class)
    private List<IPv4Subnet> subnets = new ArrayList<>();
    @Getter
    @Setter
    @JSONArrayProperty(type = String.class)
    private List<String> domains = new ArrayList<>();

    @Getter
    @Setter
    @JSONProperty(required = false)
    private boolean resolveDomains = false;

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof NetworkResourceBundle that)) return false;
        return Objects.equals(ASN, that.ASN) && Objects.equals(subnets, that.subnets) && Objects.equals(domains, that.domains);
    }

    @Override
    public int hashCode() {
        return Objects.hash(ASN, subnets, domains);
    }

    /**
     * Clears all stored network identifiers.
     */
    public void clear() {
        ASN.clear();
        subnets.clear();
        domains.clear();
    }

    /**
     * Adds all resources from the provided bundle into this bundle.
     */
    public void add(NetworkResourceBundle networkResourceBundle) {
        ASN.addAll(networkResourceBundle.getASN());
        subnets.addAll(networkResourceBundle.getSubnets());
        domains.addAll(networkResourceBundle.getDomains());
    }

}
