package me.MiniDigger.RideThaMob;

import me.MiniDigger.RideThaMob.api.FancyMessageSender;
import me.MiniDigger.RideThaMob.api.NMSHandler;
import me.MiniDigger.RideThaMob.api.RideThaMob;
import me.MiniDigger.RideThaMob.fanciful.FancyMessage;
import me.MiniDigger.RideThaMob.v1_10_R1.fancyful.FancyMessageSender_v1_10_R1;
import me.MiniDigger.RideThaMob.v1_10_R1.util.NMSHandler_1_10_R1;
import me.MiniDigger.RideThaMob.v1_8_R3.fancyful.FancyMessageSender_v1_8_R3;
import me.MiniDigger.RideThaMob.v1_8_R3.util.NMSHandler_1_8_R3;
import me.MiniDigger.RideThaMob.v1_9_R1.fancyful.FancyMessageSender_v1_9_R1;
import me.MiniDigger.RideThaMob.v1_9_R1.util.NMSHandler_1_9_R1;
import me.MiniDigger.RideThaMob.v1_9_R2.fancyful.FancyMessageSender_v1_9_R2;
import me.MiniDigger.RideThaMob.v1_9_R2.util.NMSHandler_1_9_R2;
import org.apache.commons.lang.ArrayUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.regex.Pattern;

public class RideThaMobPlugin extends JavaPlugin implements Listener, RideThaMob {

    public static final String[] supportedVersions = { "v1_8_R3", "v1_9_R1", "v1_9_R2", "v1_10_R1" };

    private static RideThaMobPlugin INSTANCE;

    private String nmsVersion;
    private NMSHandler nmsHandler;
    private FancyMessageSender fancyMessageSender;
    private ConfigHandlerImpl config;
    private int updateVal = 1;

    @Override
    public void onLoad() {
        INSTANCE = this;
        nmsVersion = getNMSVersion();
    }

    @Override
    public void onEnable() {
        if ( !enableNMSHandler() ) {
            getLogger().severe( "Disabling Plugin!" );
            getPluginLoader().disablePlugin( this );
            return;
        }

        nmsHandler.registerEntites();

        reloadConfig();

        fixEntities();

        RideThaMobCommands cmd = new RideThaMobCommands();
        getCommand( "rtm" ).setExecutor( cmd );

        new FancyMessage( "Test" ).color( ChatColor.RED ).then( "This is a test" ).color( ChatColor.GREEN ).send( Bukkit.getConsoleSender() );
    }

    @Override
    public void onDisable() {
        INSTANCE = null;
    }

    @Override
    public void fixEntities() {
        // TODO replace ALL entities in the world with rideable ones, only if the user enables it
    }

    @Override
    public void reloadConfig() {
        config = new ConfigHandlerImpl( new File( getDataFolder(), "config.yml" ), this );
        config.defaultValues();
        config.loadStuff();
        config.save();
    }

    @Override
    public ConfigHandlerImpl getConfigHandler() {
        return config;
    }

    @Override
    public void setShouldUpdate() {
        updateVal++;
    }

    @Override
    public int getUpdateVal() {
        return updateVal;
    }

    @Override
    public boolean enableNMSHandler() {
        getLogger().info( "Found server version " + nmsVersion );

        if ( ArrayUtils.contains( supportedVersions, nmsVersion ) ) {
            switch ( nmsVersion ) {
                case "v1_8_R3":
                    fancyMessageSender = new FancyMessageSender_v1_8_R3();
                    nmsHandler = new NMSHandler_1_8_R3( this );
                    break;
                case "v1_9_R1":
                    fancyMessageSender = new FancyMessageSender_v1_9_R1();
                    nmsHandler = new NMSHandler_1_9_R1( this );
                    break;
                case "v1_9_R2":
                    fancyMessageSender = new FancyMessageSender_v1_9_R2();
                    nmsHandler = new NMSHandler_1_9_R2( this );
                    break;
                case "v1_10_R1":
                    fancyMessageSender = new FancyMessageSender_v1_10_R1();
                    nmsHandler = new NMSHandler_1_10_R1( this );
                    break;
                default:
                    getLogger().severe( "Your server version (" + nmsVersion + ") is not supported!" );
                    return false;
            }
        } else {
            getLogger().severe( "Your server version (" + nmsVersion + ") is not supported!" );
            return false;
        }

        getLogger().info( "Enabled nms handler!" );

        return true;
    }

    @Override
    public String getNMSVersion() {
        String version;
        try {
            version = Bukkit.getServer().getClass().getPackage().getName().split( Pattern.quote( "." ) )[3];
        } catch ( Exception ex ) {
            return "UNKNOWN";
        }
        return version;
    }

    @Override
    public NMSHandler getNMSHanlder() {
        return nmsHandler;
    }

    @Override
    public FancyMessageSender getFancyMessageSender() {
        return fancyMessageSender;
    }

    public static RideThaMobPlugin getInstance() {
        return INSTANCE;
    }

 /*   public void test() {
        int i = 0;
        List<String> keys = new ArrayList<>();
        Player p = null;

        //first
        ComponentBuilder builder = new ComponentBuilder( i + ". " );
        builder.color( net.md_5.bungee.api.ChatColor.YELLOW ).bold( true );

        //event
        builder.event( new ClickEvent( ClickEvent.Action.RUN_COMMAND,"f who " + keys.get(i)));
        TextComponent hover = getHover(keys.get(i),values.get(i));
        builder.event( new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[] {hover}));

        //faction
        builder.append( keys.get( i ) );
        if ( true ) {
            builder.color( net.md_5.bungee.api.ChatColor.WHITE );
        } else {
            builder.color( net.md_5.bungee.api.ChatColor.RED );
        }

        //second
        builder.append( " - " ).color( net.md_5.bungee.api.ChatColor.RESET );

        //third
        builder.append( "$" + "smth" ).color( net.md_5.bungee.api.ChatColor.YELLOW ).bold( true );

        //send
        p.spigot().sendMessage( builder.create() );
    }*/
}
@@ -4854,1090 +4854,6 @@
   %7D%0A
-%0A /*   public void test() %7B%0A        int i = 0;%0A        List%3CString%3E keys = new ArrayList%3C%3E();%0A        Player p = null;%0A%0A        //first%0A        ComponentBuilder builder = new ComponentBuilder( i + %22. %22 );%0A        builder.color( net.md_5.bungee.api.ChatColor.YELLOW ).bold( true );%0A%0A        //event%0A        builder.event( new ClickEvent( ClickEvent.Action.RUN_COMMAND,%22f who %22 + keys.get(i)));%0A        TextComponent hover = getHover(keys.get(i),values.get(i));%0A        builder.event( new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent%5B%5D %7Bhover%7D));%0A%0A        //faction%0A        builder.append( keys.get( i ) );%0A        if ( true ) %7B%0A            builder.color( net.md_5.bungee.api.ChatColor.WHITE );%0A        %7D else %7B%0A            builder.color( net.md_5.bungee.api.ChatColor.RED );%0A        %7D%0A%0A        //second%0A        builder.append( %22 - %22 ).color( net.md_5.bungee.api.ChatColor.RESET );%0A%0A        //third%0A        builder.append( %22$%22 + %22smth%22 ).color( net.md_5.bungee.api.ChatColor.YELLOW ).bold( true );%0A%0A        //send%0A        p.spigot().sendMessage( builder.create() );%0A    %7D*/%0A
 %7D
+%0A

remove whatever that was