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

import cn.hutool.core.collection.ListUtil;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Pulsar 信息
 *
 * @author lanyuanxiaoyao
 * @date 2023-04-28
 */
public class PulsarInfo {
    public static final List<PulsarInfo> DEFAULT_INFOS = ListUtil.of(
            new PulsarInfo(
                    "main",
                    ListUtil.of(
                            "132.121.204.145:16680",
                            "132.121.204.147:16680",
                            "132.121.204.148:16680",
                            "132.121.204.82:16680",
                            "132.121.204.83:16680",
                            "132.121.204.85:16680",
                            "132.121.204.86:16680",
                            "132.121.204.94:16680",
                            "132.121.204.101:16680",
                            "132.121.223.3:16680"
                    ),
                    "132.121.204.101:58081",
                    "132.121.204.101:18110"
            ),
            new PulsarInfo(
                    "acct",
                    ListUtil.of(
                            "132.122.113.167:16650",
                            "132.122.113.168:16650",
                            "132.122.113.169:16650",
                            "132.122.113.170:16650",
                            "132.122.113.171:16650",
                            "132.122.113.172:16650"
                    ),
                    "132.122.113.167:58081",
                    "132.122.113.167:18088"
            ),
            new PulsarInfo(
                    "order",
                    ListUtil.of(
                            "132.122.113.171:16660",
                            "132.122.113.172:16660",
                            "132.122.113.173:16660",
                            "132.122.113.174:16660",
                            "132.122.113.175:16660",
                            "132.122.113.176:16660"
                    ),
                    "132.122.113.171:58081",
                    "132.122.113.171:18098"
            ),
            new PulsarInfo(
                    "cust",
                    ListUtil.of(
                            "132.122.113.169:16670",
                            "132.122.113.170:16670",
                            "132.122.113.173:16670",
                            "132.122.113.174:16670",
                            "132.122.113.175:16670",
                            "132.122.113.176:16670"
                    ),
                    "132.122.113.169:58081",
                    "132.122.113.169:18108"
            )
    );
    public static final Map<String, PulsarInfo> PULSAR_CLIENT_INFO_MAPPING = DEFAULT_INFOS.stream()
            .flatMap(info -> info.getBrokers()
                    .stream()
                    .map(url -> new Pair<>(url, info)))
            .collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
    public static final Map<String, String> PULSAR_CLIENT_NAME_MAPPING = PULSAR_CLIENT_INFO_MAPPING.entrySet()
            .stream()
            .map(entry -> new Pair<>(entry.getKey(), entry.getValue().name))
            .collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
    public static final Map<String, String> PULSAR_CLIENT_ADMIN_MAPPING = PULSAR_CLIENT_INFO_MAPPING.entrySet()
            .stream()
            .map(entry -> new Pair<>(entry.getKey(), entry.getValue().admin))
            .collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
    public static final Map<String, String> PULSAR_CLIENT_PRESTO_MAPPING = PULSAR_CLIENT_INFO_MAPPING.entrySet()
            .stream()
            .map(entry -> new Pair<>(entry.getKey(), entry.getValue().presto))
            .collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
    public static final Map<String, PulsarInfo> PULSAR_NAME_INFO_MAPPING = DEFAULT_INFOS.stream()
            .collect(Collectors.toMap(PulsarInfo::getName, info -> info));
    private String name;
    private List<String> brokers;
    private String presto;
    private String admin;
    public PulsarInfo() {
    }

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

    public String getName() {
        return name;
    }

    public List<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;
        }
    }
}
