package org.bukkit.plugin.messaging;

import static org.bukkit.support.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Collection;
import org.bukkit.entity.Player;
import org.bukkit.plugin.TestPlugin;
import org.junit.jupiter.api.Test;

public class StandardMessengerTest {
    public StandardMessenger getMessenger() {
        return new StandardMessenger();
    }

    private int count = 0;
    public TestPlugin getPlugin() {
        return new TestPlugin("" + count++);
    }

    @Test
    public void testRemoveFromOutgoing_LastPluginCleansUpChannelMap() {
        StandardMessenger messenger = getMessenger();
        TestPlugin plugin1 = getPlugin();
        TestPlugin plugin2 = getPlugin();

        messenger.registerOutgoingPluginChannel(plugin1, "test:channel");
        messenger.registerOutgoingPluginChannel(plugin2, "test:channel");
        messenger.unregisterOutgoingPluginChannel(plugin1, "test:channel");

        assertTrue(messenger.hasOutgoingChannelMapEntry("test:channel"),
                "Channel map should still contain entry after removing first plugin");

        assertTrue(messenger.isOutgoingChannelRegistered(plugin2, "test:channel"),
                "Second plugin should still be registered");
    }

    @Test
    public void testRemoveFromOutgoing_LastChannelCleansUpPluginMap() {
        StandardMessenger messenger = getMessenger();
        TestPlugin plugin = getPlugin();

        messenger.registerOutgoingPluginChannel(plugin, "test:channel");
        messenger.unregisterOutgoingPluginChannel(plugin, "test:channel");

        assertFalse(messenger.hasOutgoingPluginMapEntry(plugin),
                "Plugin map entry should be removed when last channel unregisters");

        assertEquals(0, messenger.getOutgoingChannelCountForPlugin(plugin),
                "Plugin should have 0 channels");
    }

}