package com.lanyuanxiaoyao.service.configuration.entity.pulsar;

import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.map.ImmutableMap;

/**
 * Pulsar 信息
 *
 * @author lanyuanxiaoyao
 * @date 2023-04-28
 */
public class PulsarInfo {
    public static final ImmutableList<PulsarInfo> DEFAULT_INFOS = Lists.immutable.of(
            new PulsarInfo(
                    "main",
                    Lists.immutable.of(
                            "132.122.113.167:16680",
                            "132.122.113.168:16680",
                            "132.122.113.169:16680",
                            "132.122.113.170:16680",
                            "132.122.113.171:16680",
                            "132.122.113.172:16680",
                            "132.122.115.163:16680",
                            "132.122.115.164:16680",
                            "132.122.115.165:16680",
                            "132.122.115.166:16680"
                    ),
                    "",
                    "132.122.113.167:18110"
            ),
            new PulsarInfo(
                    "acct",
                    Lists.immutable.of(
                            "132.122.115.158:16650",
                            "132.122.115.159:16650",
                            "132.122.115.160:16650",
                            "132.122.115.161:16650",
                            "132.122.115.167:16650",
                            "132.122.115.168:16650"
                    ),
                    "",
                    "132.122.115.158:18088"
            ),
            new PulsarInfo(
                    "order",
                    Lists.immutable.of(
                            "132.122.115.158:16660",
                            "132.122.115.159:16660",
                            "132.122.115.160:16660",
                            "132.122.115.161:16660",
                            "132.122.115.167:16660",
                            "132.122.115.168:16660"
                    ),
                    "",
                    "132.122.115.158:18098"
            ),
            new PulsarInfo(
                    "cust",
                    Lists.immutable.of(
                            "132.122.113.167:16670",
                            "132.122.113.168:16670",
                            "132.122.113.169:16670",
                            "132.122.113.170:16670",
                            "132.122.113.171:16670",
                            "132.122.113.172:16670",
                            "132.122.115.163:16670",
                            "132.122.115.164:16670",
                            "132.122.115.165:16670",
                            "132.122.115.166:16670"
                    ),
                    "",
                    "132.122.113.167:18108"
            )
    );
    public static final ImmutableMap<String, PulsarInfo> PULSAR_CLIENT_INFO_MAPPING = DEFAULT_INFOS
            .flatCollect(info -> info.getBrokers()
                    .collect(url -> new Pair<>(url, info)))
            .toMap(Pair::getLeft, Pair::getRight)
            .toImmutable();
    public static final ImmutableMap<String, String> PULSAR_CLIENT_NAME_MAPPING = PULSAR_CLIENT_INFO_MAPPING.collectValues((key, value) -> value.getName());
    public static final ImmutableMap<String, String> PULSAR_CLIENT_ADMIN_MAPPING = PULSAR_CLIENT_INFO_MAPPING.collectValues((key, value) -> value.getAdmin());
    public static final ImmutableMap<String, String> PULSAR_CLIENT_PRESTO_MAPPING = PULSAR_CLIENT_INFO_MAPPING.collectValues((key, value) -> value.getPresto());
    public static final ImmutableMap<String, PulsarInfo> PULSAR_NAME_INFO_MAPPING = DEFAULT_INFOS.toMap(PulsarInfo::getName, info -> info).toImmutable();
    private String name;
    private ImmutableList<String> brokers;
    private String presto;
    private String admin;

    public PulsarInfo() {
    }

    public PulsarInfo(String name, ImmutableList<String> brokers, String presto, String admin) {
        this.name = name;
        this.brokers = brokers;
        this.presto = presto;
        this.admin = admin;
    }

    public String getName() {
        return name;
    }

    public ImmutableList<String> getBrokers() {
        return brokers;
    }

    public String getPresto() {
        return presto;
    }

    public String getAdmin() {
        return admin;
    }

    @Override
    public String toString() {
        return "PulsarInfo{" +
               "name='" + name + '\'' +
               ", brokers=" + brokers +
               ", presto='" + presto + '\'' +
               ", admin='" + admin + '\'' +
               '}';
    }

    private static final class Pair<L, R> {
        private final L left;
        private final R right;

        private Pair(L left, R right) {
            this.left = left;
            this.right = right;
        }

        public L getLeft() {
            return left;
        }

        public R getRight() {
            return right;
        }
    }
}
