/**
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered.org <http://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.mod.asm.transformers;

import static org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;
import static org.objectweb.asm.ClassWriter.COMPUTE_MAXS;

import java.util.HashMap;
import java.util.Map;

import net.minecraft.launchwrapper.IClassTransformer;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.spongepowered.api.event.Event;
import org.spongepowered.api.event.state.InitializationEvent;
import org.spongepowered.api.event.state.PreInitializationEvent;
import org.spongepowered.api.event.state.ServerStartingEvent;
import org.spongepowered.api.event.voxel.VoxelEvent;

import cpw.mods.fml.relauncher.FMLRelaunchLog;

public class EventTransformer implements IClassTransformer {
    
    private final static Map<String, Class<?>> events = new HashMap<String, Class<?>>();
    
    static {
        events.put("cpw.mods.fml.common.event.FMLPreInitializationEvent", PreInitializationEvent.class);
        events.put("cpw.mods.fml.common.event.FMLInitializationEvent", InitializationEvent.class);
        events.put("cpw.mods.fml.common.event.FMLServerStartingEvent", ServerStartingEvent.class);
        
        events.put("net.minecraftforge.event.world.BlockEvent$BreakEvent", VoxelEvent.class);
    }
    
    @Override
    public byte[] transform(String name, String transformedName, byte[] bytes) {
        
        if (bytes == null || name.startsWith("net.minecraft.") || name.indexOf('.') == -1) {
            return bytes;
        }

        if (!events.containsKey(name)) {
            return bytes;
        }

        Class<?> interf = events.get(name);
        if (interf == null || !interf.isInterface()) {
            FMLRelaunchLog.warning(name + " cannot be processed");
            return bytes;
        }

        String interfaceName = Type.getInternalName(interf);
        
        try {
            ClassReader cr = new ClassReader(bytes);
            ClassNode classNode = new ClassNode();
            cr.accept(classNode, 0);
            classNode.interfaces.add(interfaceName);
            
            if (Event.class.isAssignableFrom(interf)) {
                classNode.methods.add(createGetGameMethod());
            }
            
            ClassWriter cw = new ClassWriter(cr, COMPUTE_MAXS | COMPUTE_FRAMES);
            classNode.accept(cw);
            return cw.toByteArray();
        } catch (Throwable t) {
            t.printStackTrace();
            return bytes;
        }
    }
    
    private MethodNode createGetGameMethod() {
        MethodNode methodNode = new MethodNode(Opcodes.ASM4, Opcodes.ACC_PUBLIC, "getGame", "()Lorg/spongepowered/api/Game;", null, null);
        methodNode.instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, "org/spongepowered/mod/SpongeMod", "instance", "Lorg/spongepowered/mod/SpongeMod;"));
        methodNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "org/spongepowered/mod/SpongeMod", "getGame", "()Lorg/spongepowered/mod/SpongeGame;", false));
        methodNode.instructions.add(new InsnNode(Opcodes.ARETURN));
        methodNode.maxLocals = 1;
        methodNode.maxStack = 1;
        return methodNode;
    }
}

@@ -1413,16 +1413,37 @@
 _MAXS;%0A%0A
+import java.io.File;%0A
 import j
@@ -1538,16 +1538,56 @@
 ormer;%0A%0A
+import org.apache.logging.log4j.Logger;%0A
 import o
@@ -2264,16 +2264,65 @@
 unchLog;
+%0Aimport org.spongepowered.mod.asm.util.ASMHelper;
 %0A%0Apublic
@@ -3838,24 +3838,648 @@
            %7D
+%0A%0A            // TODO: This is a temporary thing to make PreInit work. The different things needed to make different events work should be abstracted.%0A            if(PreInitializationEvent.class.isAssignableFrom(interf)) %7B%0A                ASMHelper.generateSelfForwardingMethod(classNode, %22getConfigurationDirectory%22, %22getModConfigurationDirectory%22,%0A                                                       Type.getType(File.class));%0A                ASMHelper.generateSelfForwardingMethod(classNode, %22getPluginLog%22, %22getModLog%22,%0A                                                       Type.getType(Logger.class));%0A            %7D
 %0A           

Implement forwarding methods for PreInit event