use libp2p::gossipsub::{
    Sha256Topic,
    Topic,
    TopicHash,
};

use super::messages::{
    GossipTopicTag,
    GossipsubBroadcastRequest,
};

pub const NEW_TX_GOSSIP_TOPIC: &str = "new_tx";
pub const TX_PRECONFIRMATIONS_GOSSIP_TOPIC: &str = "tx_preconfirmations";

/// Holds used Gossipsub Topics
/// Each field contains TopicHash of existing topics
/// in order to avoid converting topics to TopicHash on each received message
#[derive(Debug)]
pub struct GossipsubTopics {
    new_tx_topic: TopicHash,
    tx_preconfirmations_topic: TopicHash,
}

impl GossipsubTopics {
    pub fn new(network_name: &str) -> Self {
        let new_tx_topic: Sha256Topic =
            Topic::new(format!("{NEW_TX_GOSSIP_TOPIC}/{network_name}"));
        let tx_preconfirmations_topic: Sha256Topic =
            Topic::new(format!("{TX_PRECONFIRMATIONS_GOSSIP_TOPIC}/{network_name}"));
        Self {
            new_tx_topic: new_tx_topic.hash(),
            tx_preconfirmations_topic: tx_preconfirmations_topic.hash(),
        }
    }

    /// Given a TopicHash it will return a matching GossipTopicTag
    pub fn get_gossipsub_tag(
        &self,
        incoming_topic: &TopicHash,
    ) -> Option<GossipTopicTag> {
        match incoming_topic {
            hash if hash == &self.new_tx_topic => Some(GossipTopicTag::NewTx),
            hash if hash == &self.tx_preconfirmations_topic => {
                Some(GossipTopicTag::TxPreconfirmations)
            }
            _ => None,
        }
    }

    /// Given a `GossipsubBroadcastRequest` returns a `TopicHash`
    /// which is broadcast over the network with the serialized inner value of `GossipsubBroadcastRequest`
    pub fn get_gossipsub_topic_hash(
        &self,
        outgoing_request: &GossipsubBroadcastRequest,
    ) -> TopicHash {
        match outgoing_request {
            GossipsubBroadcastRequest::NewTx(_) => self.new_tx_topic.clone(),
            GossipsubBroadcastRequest::TxPreConfirmations(_) => {
                self.tx_preconfirmations_topic.clone()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fuel_core_types::fuel_tx::Transaction;
    use libp2p::gossipsub::Topic;
    use std::sync::Arc;

    #[test]
    fn test_gossipsub_topics() {
        let network_name = "fuel_test_network";
        let new_tx_topic: Sha256Topic =
            Topic::new(format!("{NEW_TX_GOSSIP_TOPIC}/{network_name}"));

        let gossipsub_topics = GossipsubTopics::new(network_name);

        // Test matching Topic Hashes
        assert_eq!(gossipsub_topics.new_tx_topic, new_tx_topic.hash());

        // Test given a TopicHash that `get_gossipsub_tag()` returns matching `GossipTopicTag`
        assert_eq!(
            gossipsub_topics.get_gossipsub_tag(&new_tx_topic.hash()),
            Some(GossipTopicTag::NewTx)
        );

        // Test given a `GossipsubBroadcastRequest` that `get_gossipsub_topic_hash()` returns matching `TopicHash`
        let broadcast_req =
            GossipsubBroadcastRequest::NewTx(Arc::new(Transaction::default_test_tx()));
        assert_eq!(
            gossipsub_topics.get_gossipsub_topic_hash(&broadcast_req),
            new_tx_topic.hash()
        );
    }
}
