package com.lanyuanxiaoyao.service.web.entity;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lanyuanxiaoyao.service.configuration.entity.pulsar.PulsarTopic;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.map.ImmutableMap;

/**
 * Topic 展示
 *
 * @author lanyuanxiaoyao
 * @date 2023-04-28
 */
public class TopicVO {
    @JsonIgnore
    private final PulsarTopic pulsarTopic;

    private final ImmutableList<SubscriptionStateVO> subscriptionStateVOS;

    public TopicVO(PulsarTopic pulsarTopic) {
        this.pulsarTopic = pulsarTopic;

        if (ObjectUtil.isNotNull(pulsarTopic.getState()) && ObjectUtil.isNotNull(pulsarTopic.getState().getSubscriptions())) {
            subscriptionStateVOS = Maps.immutable.ofAll(pulsarTopic.getState().getSubscriptions())
                    .collectValues(SubscriptionStateVO::new)
                    .toList()
                    .toImmutable();
        } else {
            subscriptionStateVOS = Lists.immutable.empty();
        }

    }

    public String getName() {
        return pulsarTopic.getName();
    }

    public PulsarTopic.State getState() {
        return pulsarTopic.getState();
    }

    public String getLastMessageId() {
        return pulsarTopic.getLastMessageId();
    }

    public ImmutableList<String> getSubscriptions() {
        return Lists.immutable.ofAll(pulsarTopic.getSubscriptions());
    }

    public ImmutableList<SubscriptionStateVO> getSubscriptionStateVOS() {
        return subscriptionStateVOS;
    }

    @Override
    public String toString() {
        return "TopicVO{" +
                "pulsarTopic=" + pulsarTopic +
                ", subscriptionStateVOS=" + subscriptionStateVOS +
                '}';
    }

    public static final class SubscriptionStateVO {
        private final PulsarTopic.State.SubscriptionState state;

        private final String name;

        public SubscriptionStateVO(String name, PulsarTopic.State.SubscriptionState state) {
            this.name = name;
            this.state = state;
        }

        public String getName() {
            return name;
        }

        public Double getMessageRateOut() {
            return state.getMessageRateOut();
        }

        public Double getMessageThroughputOut() {
            return state.getMessageThroughputOut();
        }

        public Long getBytesOutCounter() {
            return state.getBytesOutCounter();
        }

        public Long getMessageOutCounter() {
            return state.getMessageOutCounter();
        }

        public Double getMessageRateRedeliver() {
            return state.getMessageRateRedeliver();
        }

        public Integer getChunkedMessageRate() {
            return state.getChunkedMessageRate();
        }

        public Long getMessageBacklog() {
            return state.getMessageBacklog();
        }

        public Long getBacklogSize() {
            return state.getBacklogSize();
        }

        public Long getMessageBacklogNoDelayed() {
            return state.getMessageBacklogNoDelayed();
        }

        public Boolean isBlockedSubscriptionOnUnackedMessages() {
            return state.isBlockedSubscriptionOnUnackedMessages();
        }

        public Long getMessageDelayed() {
            return state.getMessageDelayed();
        }

        public Long getUnackedMessages() {
            return state.getUnackedMessages();
        }

        public String getType() {
            return state.getType();
        }

        public String getActiveConsumerName() {
            return state.getActiveConsumerName();
        }

        public Double getMessageRateExpired() {
            return state.getMessageRateExpired();
        }

        public Long getTotalMessageExpired() {
            return state.getTotalMessageExpired();
        }

        public Long getLastExpireTimestamp() {
            return state.getLastExpireTimestamp();
        }

        public Long getLastConsumedFlowTimestamp() {
            return state.getLastConsumedFlowTimestamp();
        }

        public Long getLastConsumedTimestamp() {
            return state.getLastConsumedTimestamp();
        }

        public Long getLastAckedTimestamp() {
            return state.getLastAckedTimestamp();
        }

        public Long getLastMarkDeleteAdvancedTimestamp() {
            return state.getLastMarkDeleteAdvancedTimestamp();
        }

        public ImmutableList<PulsarTopic.State.ConsumerState> getConsumers() {
            return Lists.immutable.ofAll(state.getConsumers());
        }

        public Boolean isDurable() {
            return state.isDurable();
        }

        public Boolean isReplicated() {
            return state.isReplicated();
        }

        public ImmutableMap<String, String> getConsumersAfterMarkDeletePosition() {
            return Maps.immutable.ofAll(state.getConsumersAfterMarkDeletePosition());
        }

        @Override
        public String toString() {
            return "SubscriptionStateVO{" +
                    "state=" + state +
                    '}';
        }
    }
}
