package me.isuzutsuki.betterfonts;

import net.md_5.jbeat.PatcherIO;
import net.minecraft.launchwrapper.IClassTransformer;

import java.io.*;
import java.util.Arrays;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public final class BTFClassTransformer implements IClassTransformer {

//    private static final String PATCH_FILE_PATH = "/patch/FontRenderer.java.patch";
    private static final String PATCH_FILE_PATH = "/patch/FontRenderer.java.bps";

    /**
     * @param arg0 Deobfuscated name (usually the Searge class name)
     * @param arg1 Obfuscated name
     * @param arg2 The class' data.
     *
     * @return The data for the class.
     */
    @Override
    public byte[] transform(String arg0, String arg1, final byte[] arg2) {
        if (arg2 == null || arg2.length <= 0) return null;//Check if the class data is valid.
        byte[] returnData = arg2;
        if (arg0.equals("net.minecraft.client.gui.FontRenderer")) {
            BetterFontsCore.BETTER_FONTS_LOGGER.info("Patching class \"{}\"...", arg0);
            returnData = beatPatch(arg0, arg2);
        }
        if (returnData == null || returnData.length <= 0) {//Patching failed
            returnData = arg2;
            BetterFontsCore.BETTER_FONTS_LOGGER.error("Failed to patch class \"{}\".");
        }
        return returnData;
    }

    private byte[] beatPatch(final String className, final byte[] data) {
        InputStream patchFileInputStream = BTFClassTransformer.class.getResourceAsStream(BTFClassTransformer.PATCH_FILE_PATH);
        if (patchFileInputStream == null) {
            BetterFontsCore.BETTER_FONTS_LOGGER.error("Failed to find patch file \"{}\"!", BTFClassTransformer.PATCH_FILE_PATH);
            return data;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        PatcherIO patcherIO = new PatcherIO(new ByteArrayInputStream(data), patchFileInputStream, byteArrayOutputStream);
        try {
            patcherIO.patch();
        } catch (IOException e) {
            BetterFontsCore.BETTER_FONTS_LOGGER.error("Caught exception \"{}\" when attempting to patch class \"{}\"!", e, className);
            return data;
        }
        BetterFontsCore.BETTER_FONTS_LOGGER.info("Patched class \"{}\"!", className);
        return byteArrayOutputStream.toByteArray();
    }

}

@@ -488,50 +488,31 @@
 ram 
-arg0 Deobfuscated name (usually the Searge
+obfClassName Obfuscated
  cla
@@ -518,17 +518,16 @@
 ass name
-)
 %0A     * 
@@ -537,14 +537,21 @@
 ram 
-arg1 O
+className Deo
 bfus
@@ -556,24 +556,56 @@
 uscated name
+ (usually the Searge class name)
 %0A     * @par
@@ -607,20 +607,21 @@
  @param 
-arg2
+bytes
  The cla
@@ -737,25 +737,38 @@
 ing 
-arg0, String arg1
+obfClassName, String className
 , fi
@@ -778,20 +778,21 @@
  byte%5B%5D 
-arg2
+bytes
 ) %7B%0A    
@@ -799,20 +799,21 @@
     if (
-arg2
+bytes
  == null
@@ -816,20 +816,21 @@
 null %7C%7C 
-arg2
+bytes
 .length 
@@ -903,36 +903,37 @@
 %5B%5D returnData = 
-arg2
+bytes
 ;%0A        if (ar
@@ -930,20 +930,25 @@
     if (
-arg0
+className
 .equals(
@@ -1118,18 +1118,24 @@
 tch(
-arg0, arg2
+className, bytes
 );%0A 
@@ -1249,12 +1249,13 @@
 a = 
-arg2
+bytes
 ;%0A  

fix transformer arguments order