package lc.stack.dns;

import lc.mecha.log.MechaLogger;
import lc.mecha.log.MechaLoggerFactory;
import org.xbill.DNS.Message;
import org.xbill.DNS.Type;

import java.io.IOException;
import java.net.InetAddress;

/**
 * An emergency implementation of {@link NameStore} to meet the needs for the LEGIH&CO network following the
 * implosion of FreeIPA.
 * <p>
 * Note that there is something of a quirk in this implementation in that if a host is not discovered here,
 * it will still forward the address to the Internet. In the case of site-local DNS this means that it will
 * fall back to public DNS. Feature or bug? You decide.
 *
 * @author Alex Leigh
 */
public class LeighCoZone implements NameStore {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(LeighCoZone.class);

    @Override
    public Message doQuery(Message request) throws IOException {
        int t = request.getQuestion().getType();
        String name = request.getQuestion().getName().toString();

        if (t == Type.A) {
            return switch (name) {
                case "b0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.16"));
                case "bungie.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.35"));
                case "db1.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.22"));
                case "db2.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.21"));
                case "dream.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.29"));
                case "esp0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.30"));
                case "esp1.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.64"));
                case "fs1.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.34"));
                case "ghibli.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.44"));
                case "gitlab.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.26"));
                case "helium.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.24"));
                case "irc0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.67"));
                case "leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.51"));
                case "m3.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.68"));
                case "massive.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.28"));
                case "mc5.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.31"));
                case "mctest0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.33"));
                case "mine0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.15"));
                case "moon.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.32"));
                case "mq0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.62"));
                case "mx0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.27"));
                case "mx1.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("52.42.175.17"));
                case "n0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.14"));
                case "n1.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.59"));
                case "ns0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.11"));
                case "pixel.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.19"));
                case "slb0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.51"));
                case "tik0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.33"));
                case "web0.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.20"));
                case "world.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.5"));
                case "zebra.leigh-co.com." -> DNSUtil.buildReply(request, InetAddress.getByName("10.52.4.17"));
                default -> null;
            };
        } else if (t == Type.PTR) {
            return switch (name) {
                case "24.4.52.10.in-addr.arpa." -> DNSUtil.buildReply(request, "helium.leigh-co.com.");
                case "67.4.52.10.in-addr.arpa." -> DNSUtil.buildReply(request, "irc0.leigh-co.com.");
                default -> null;
            };
        }
        return null;
    }
}
