package codechicken.cctweaks;

import codechicken.cctweaks.registry.TweakRegistry;
import codechicken.cctweaks.tweak.TGameTweak;
import codechicken.cctweaks.tweak.TSideTweak;
import codechicken.cctweaks.util.LogHelper;
import codechicken.lib.config.ConfigFile;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.DummyModContainer;
import net.minecraftforge.fml.common.LoadController;
import net.minecraftforge.fml.common.MetadataCollection;
import net.minecraftforge.fml.common.ModMetadata;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.versioning.VersionParser;
import net.minecraftforge.fml.common.versioning.VersionRange;
import net.minecraftforge.fml.relauncher.FMLInjectionData;
import net.minecraftforge.fml.relauncher.FMLLaunchHandler;
import net.minecraftforge.fml.relauncher.Side;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by covers1624 on 8/10/2016.
 */
public class CCTweaks extends DummyModContainer {

    public static ConfigFile configFile;

    private static ArrayList<String> delayedEventHandlers = new ArrayList<String>();

    public CCTweaks() {
        super(MetadataCollection.from(MetadataCollection.class.getResourceAsStream("/cctmod.info"), "CCTweaks").getMetadataForId("CCTweaks", null));
        initConfig();
    }

    public static void initConfig() {
        if (configFile == null) {
            configFile = new ConfigFile(new File((File) FMLInjectionData.data()[6], "config/CodeChickenTweaks.cfg")).setComment("CodeChickenTweaks configuration file.\n" + "Deleting any element will restore it to it's default value");
        }
    }

    public static void registerDelayedEventHandler(String clazz) {
        delayedEventHandlers.add(clazz);
    }

    @Override
    public ModMetadata getMetadata() {
        ModMetadata metadata = super.getMetadata();
        ModMetadata newMetadata = new ModMetadata();

        newMetadata.modId = metadata.modId;
        newMetadata.name = metadata.name;
        newMetadata.description = metadata.description;
        newMetadata.version = metadata.version;
        newMetadata.url = metadata.url;
        newMetadata.authorList = metadata.authorList;

        List<TGameTweak> activeTweaks = TweakRegistry.getActiveTweakers();
        List<TGameTweak> allTweaks = TweakRegistry.getAllTweakers();

        StringBuilder builder = new StringBuilder(newMetadata.description);
        builder.append("\n");
        builder.append("\n");

        builder.append(TextFormatting.GOLD.toString());
        builder.append("Active Tweaks:");
        for (TGameTweak tweak : allTweaks) {
            if (!tweak.isEnabled()) {
                continue;
            }
            builder.append("\n");
            builder.append("    ");
            builder.append(tweak.getTweakName());
        }

        builder.append("\n");
        builder.append("\n");
        builder.append(TextFormatting.BLUE.toString());
        builder.append("Installed Tweaks:");
        for (TGameTweak tweak : allTweaks) {
            builder.append("\n");
            builder.append("    ");
            builder.append(tweak.getTweakName());
        }

        builder.append("\n");
        builder.append("\n");
        builder.append(TextFormatting.GRAY.toString());
        builder.append("Disabled Tweaks:");
        for (TGameTweak tweak : allTweaks) {
            if (tweak.isEnabled()) {
                continue;
            }
            builder.append("\n");
            builder.append("    ");
            builder.append(tweak.getTweakName());
            builder.append("\n");
            builder.append("        ");
            builder.append("Disabled because of config.");
            if (tweak instanceof TSideTweak) {
                TSideTweak sideTweak = ((TSideTweak) tweak);
                if (sideTweak.getSide() != Side.CLIENT) {
                    builder.append("\n");
                    builder.append("        ");
                    builder.append("Disabled because this tweak is only for the server.");
                }
            }
        }

        newMetadata.description = builder.toString();

        return newMetadata;
    }

    @Override
    public boolean registerBus(EventBus bus, LoadController controller) {
        bus.register(this);
        return true;
    }

    @Subscribe
    public void preInit(FMLPreInitializationEvent event) {
        try {
            for (String clazz : delayedEventHandlers) {
                Class eventHandler = Class.forName(clazz);
                MinecraftForge.EVENT_BUS.register(eventHandler.newInstance());
            }
        } catch (Exception e) {
            LogHelper.errorError("Exception registering delayed event handler!", e);
        }
    }

    @Override
    public VersionRange acceptableMinecraftVersionRange() {
        return VersionParser.parseRange("[1.10.2]");
    }
}