package com.voxelmodpack.hdskins.mixin;

import com.voxelmodpack.hdskins.HDSkinManager;
import net.minecraft.client.renderer.IImageBuffer;
import net.minecraft.client.renderer.ImageBufferDownload;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.At.Shift;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Surrogate;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

@Mixin(ImageBufferDownload.class)
public abstract class MixinImageBufferDownload implements IImageBuffer {

    @Inject(
            method = "parseUserSkin(Ljava/awt/image/BufferedImage;)Ljava/awt/image/BufferedImage;",
            locals = LocalCapture.CAPTURE_FAILEXCEPTION,
            at = @At(
                    value = "INVOKE",
                    shift = Shift.BEFORE,
                    target = "Ljava/awt/Graphics;dispose()V",
                    remap = false))
    private void update(BufferedImage image, CallbackInfo ci, BufferedImage image2) {
        // convert skins from mojang server
        boolean isLegacy = image.getHeight() == 32;
        Graphics graphics = image2.getGraphics();
        if (isLegacy) {
            HDSkinManager.INSTANCE.convertSkin(image2, graphics);
        }
    }

    // development
    @Surrogate
    private void update(BufferedImage image, CallbackInfo ci, BufferedImage image2, Graphics graphics, boolean isLegacy) {
        if (isLegacy) {
            HDSkinManager.INSTANCE.convertSkin(image2, graphics);
        }
    }

    // for optifine
    @Surrogate
    private void update(BufferedImage image, CallbackInfo ci, int w, int h, int k, BufferedImage image2, Graphics graphics, boolean isLegacy) {
        // convert skins from mojang server
        if (isLegacy) {
            HDSkinManager.INSTANCE.convertSkin(image2, graphics);
        }
    }

}

@@ -330,260 +330,91 @@
 ion.
-At.Shift;%0Aimport org.spongepowered.asm.mixin.injection.Inject;%0Aimport org.spongepowered.asm.mixin.injection.Surrogate;%0Aimport org.spongepowered.asm.mixin.injection.callback.CallbackInfo;%0Aimport org.spongepowered.asm.mixin.injection.callback.LocalCaptur
+Inject;%0Aimport org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnabl
 e;%0A%0A
@@ -714,251 +714,50 @@
     
-locals = LocalCapture.CAPTURE_FAILEXCEPTION,%0A            at = @At(%0A                    value = %22INVOKE%22,%0A                    shift = Shift.BEFORE,%0A                    target = %22Ljava/awt/Graphics;dispose()V%22,%0A                    remap = false)
+at = @At(%22RETURN%22),%0A    cancellable = true
 )%0A  
@@ -803,37 +803,43 @@
 ge, CallbackInfo
- ci, 
+Returnable%3C
 BufferedImage im
@@ -835,23 +835,20 @@
 redImage
- image2
+%3E ci
 ) %7B%0A    
@@ -899,85 +899,84 @@
     
-boolean isLegacy = image.getHeight() == 32;%0A        Graphics graphics
+BufferedImage image2 = ci.getReturnValue();%0A        boolean isLegacy
  = image
 2.ge
@@ -975,23 +975,26 @@
 mage
-2
 .get
-Graphics()
+Height() == 32
 ;%0A  
@@ -1031,205 +1031,46 @@
     
-HDSkinManager.INSTANCE.convertSkin(image2, graphics);%0A        %7D%0A    %7D%0A%0A    // development%0A    @Surrogate%0A    private void update(BufferedImage image, CallbackInfo ci, BufferedImage
+Graphics graphics =
  image2
-, 
+.get
 Graphics
  gra
@@ -1069,62 +1069,11 @@
 hics
- graphics, boolean isLegacy) %7B%0A        if (isLegacy) %7B
+();
 %0A   
@@ -1147,327 +1147,29 @@
     
-%7D%0A
     
-%7D%0A%0A    // for optifine%0A    @Surrogate%0A    private void update(BufferedImage image, CallbackInfo ci, int w, int h, int k, BufferedImage image2, Graphics graphics, boolean isLegacy) %7B%0A        // convert skins from mojang server%0A        if (isLegacy) %7B%0A            HDSkinManager.INSTANCE.convertSkin(image2, graphics
+graphics.dispose(
 );%0A 

Fix missing wing when using optifine. Why didn't I think of this earlier?