package com.github.games647.scoreboardstats.variables;

import com.github.games647.scoreboardstats.ScoreboardStats;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.SimplePluginManager;
import org.bukkit.plugin.messaging.StandardMessenger;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@PrepareForTest({Bukkit.class, SimplePluginManager.class, Plugin.class, ScoreboardStats.class})
@RunWith(PowerMockRunner.class)
public class ReplaceManagerTest {

    private static final String SAMPLE_VARIABLE = "sample";

    @Test
    public void testUnregister() throws Exception {
        PowerMockito.mockStatic(Bukkit.class);
        Mockito.when(Bukkit.getPluginManager()).thenReturn(PowerMockito.mock(SimplePluginManager.class));
        Mockito.when(Bukkit.getMessenger()).thenReturn(PowerMockito.mock(StandardMessenger.class));
        Mockito.when(Bukkit.getScheduler()).thenReturn(PowerMockito.mock(BukkitScheduler.class));

        ScoreboardStats plugin = PowerMockito.mock(ScoreboardStats.class);
        PowerMockito.when(plugin.getLogger()).thenReturn(Logger.getGlobal());

        ReplaceManager replaceManager = new ReplaceManager(null, plugin);

        testLegacy(replaceManager);
        testAbstract(plugin, replaceManager);
    }

    private void testAbstract(Plugin plugin, ReplaceManager replaceManager) {
        VariableReplaceAdapter<?> replaceAdapter = new VariableReplaceAdapter<Plugin>(plugin, SAMPLE_VARIABLE) {

            @Override
            public void onReplace(Player player, String variable, ReplaceEvent replaceEvent) {
                throw new UnsupportedOperationException("Not supported yet");
            }
        };

        replaceManager.register(replaceAdapter);
        Assert.assertTrue(replaceManager.unregister(replaceAdapter));
    }

    @SuppressWarnings("deprecation")
    private void testLegacy(ReplaceManager replaceManager) {
        Replaceable legacyReplaceable = (Player player, String variable) -> {
            throw new UnsupportedOperationException("Not supported yet");
        };

        LegacyReplaceWrapper legacyWrapper = new LegacyReplaceWrapper(Bukkit
                .getPluginManager().getPlugin("pluginName"), legacyReplaceable);
        legacyWrapper.getVariables().add(SAMPLE_VARIABLE);
        replaceManager.register(legacyWrapper);
        Assert.assertTrue(replaceManager.unregister(legacyReplaceable));
    }
}

@@ -2289,16 +2289,94 @@
 eable = 
+new Replaceable() %7B%0A            @Override%0A            public int getScoreValue
 (Player 
@@ -2404,13 +2404,14 @@
 le) 
--%3E %7B%0A
+%7B%0A    
     
@@ -2468,36 +2468,51 @@
 ot supported yet
+.
 %22);%0A
+            %7D%0A
         %7D;%0A%0A    

Fix tests