package me.daansander.serverchecker.plugin;

import me.daansander.serverchecker.Logger;
import me.daansander.serverchecker.ServerChecker;
import me.daansander.serverchecker.eventhandler.events.PluginLoadEvent;
import me.daansander.serverchecker.io.Loader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandlerFactory;
import java.util.ArrayList;

/**
 * Created by Daan on 28-6-2015.
 */
public class ServerPlugin extends URLClassLoader {

    private static Logger l;
    private Loader loader = new Loader();
    private ArrayList<PluginBase> allPlugins = new ArrayList<>();
    private boolean isEnabled = false;

    public ServerPlugin(URL[] urls, ClassLoader parent) {
        super(urls, parent);
    }

    public ServerPlugin(URL[] urls) {
        super(urls);
    }

    public ServerPlugin(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) {
        super(urls, parent, factory);
    }

    @Deprecated
    public ServerPlugin() throws MalformedURLException {
        super(new URL[]{new URL("file:plugins\\ServerPlugin.jar")});


    }

    protected URL[] getClassPath() {
        URL[] classpath = new URL[1];
        try {
            classpath = new URL[]{new URL("file:plugins\\ServerPlugin.jar")};
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return classpath;
    }


//    public void onEnable() {
//
//    }

    public void start() {
        File pluginFolder = new File("plugins");
        loadAllPlugins();
        this.isEnabled = true;
    }

    public void registerPlugin(String name) {

    }

    public Class<?> loadClass(String name) throws ClassNotFoundException {
        Class loadedClass = findLoadedClass(name);

        if (loadedClass == null) {
            try {
                loadedClass = findClass(name);
            } catch (ClassNotFoundException e) {
            }
            if (loadedClass == null) {
                loadedClass = super.loadClass(name);
            }
        }
        return loadedClass;
    }

//    @Deprecated
//    public ArrayList<PluginBase> getPlugins(File pluginFolder) {
//        ArrayList<PluginBase> bases = new ArrayList<>();
//        for (File f : pluginFolder.listFiles()) {
//            bases.add(new PluginBase(f.getName()));
//        }
//        return bases;
//    }

    public ArrayList<PluginBase> getPlugins() {
        File pluginFolder = new File("plugins");
        ArrayList<PluginBase> bases = new ArrayList<>();
        for (File f : pluginFolder.listFiles()) {
            bases.add(new PluginBase(f.getName()));
        }
        return bases;
    }

    public void loadAllPlugins(ArrayList<PluginBase> bases) {
        for (PluginBase b : bases) {
            Plugin p = loadPlugin(b.getName());
            p.onEnable();
        }
    }

    public void loadAllPlugins() {
        for (PluginBase b : getPlugins()) {
            Plugin p = loadPlugin(b.getName());
            ServerChecker.getServerChecker().getServerSystem().getEvent().runEvent(new PluginLoadEvent(b.getName(), b.getPathToClass()));
            p.onEnable();
        }
    }

    public Plugin loadPlugin(String name) {
        URL[] classpath = new URL[1];
        try {
            classpath = new URL[]{new URL("file:plugins\\" + name + "")};
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        ServerPlugin classLoader = new ServerPlugin(classpath);
        Plugin plugin = null;
        try {
            InputStream pluginIn = classLoader.getResourceAsStream("plugin.txt");
            plugin = (Plugin) classLoader.loadClass(loader.loadPluginFile(pluginIn)).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return plugin;
    }

    @Deprecated
    public void reload() throws MalformedURLException {
        allPlugins.clear();
        start();
    }

}
