package com.cimeyclust.addonreader.resources;

import cn.nukkit.block.Block;
import cn.nukkit.block.customblock.CustomBlock;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.custom.CustomEntity;
import cn.nukkit.entity.provider.CustomClassEntityProvider;
import cn.nukkit.item.Item;
import cn.nukkit.item.customitem.CustomItem;
import cn.nukkit.plugin.PluginBase;
import org.reflections.Reflections;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class Plugin extends PluginBase {
    private static Plugin instance;
    private List<Class <? extends Entity>> entities;

    @Override
    public void onLoad() {
        instance = this;
        entities = new ArrayList<>();
    }

    @Override
    public void onEnable() {
        // Register Blocks
        Reflections blockReflections = new Reflections("com.cimeyclust.addonreader.resources.custom_blocks");
        Set<Class<? extends CustomBlock>> blocks = blockReflections.getSubTypesOf(Object.class).stream()
                .filter(clazz -> clazz.getSuperclass() == CustomBlock.class)
                .map(clazz -> (Class<? extends CustomBlock>) clazz)
                .collect(Collectors.toSet());

        for (Class<? extends CustomBlock> clazz : blocks) {
            getLogger().info("§aLoading custom block " + clazz + "...");
            try {
                Block.registerCustomBlock(Collections.singletonList(clazz)).assertOK();
            } catch (AssertionError e) {
                e.printStackTrace();
            }
        }

        // Register Items
        Reflections itemReflections = new Reflections("com.cimeyclust.addonreader.resources.custom_items");
        Set<Class<? extends CustomItem>> items = itemReflections.getSubTypesOf(Object.class).stream()
                .filter(clazz -> clazz.getSuperclass() == CustomItem.class)
                .map(clazz -> (Class<? extends CustomItem>) clazz)
                .collect(Collectors.toSet());

        for (Class<? extends CustomItem> clazz : items) {
            try {
                Item.registerCustomItem(clazz).assertOK();
            } catch (AssertionError e) {
                e.printStackTrace();
            }
        }

        // Register Entities
        Reflections entityReflections = new Reflections("com.cimeyclust.addonreader.resources.custom_entities");
        Set<Class<? extends Entity>> entities = itemReflections.getSubTypesOf(Object.class).stream()
                .filter(clazz -> clazz.getSuperclass() == Entity.class)
                .map(clazz -> (Class<? extends Entity>) clazz)
                .collect(Collectors.toSet());

        for (Class<? extends Entity> clazz : entities) {
            try {
                Entity.registerCustomEntity(new CustomClassEntityProvider(clazz)).assertOK();
                entities.add(clazz);
            } catch (AssertionError e) {
                e.printStackTrace();
            }
        }
    }

    public static Plugin getInstance() {
        return instance;
    }

    public List<Class<? extends Entity>> getEntities() {
        return entities;
    }
}
