/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.llamalad7.mixinextras.injector.ModifyReturnValue
 *  net.minecraft.class_11907
 *  net.minecraft.class_11908
 *  net.minecraft.class_11909
 *  net.minecraft.class_1703
 *  net.minecraft.class_1713
 *  net.minecraft.class_1735
 *  net.minecraft.class_1799
 *  net.minecraft.class_2561
 *  net.minecraft.class_332
 *  net.minecraft.class_364
 *  net.minecraft.class_3936
 *  net.minecraft.class_4185$class_7840
 *  net.minecraft.class_437
 *  net.minecraft.class_465
 *  net.minecraft.class_5684
 *  org.jetbrains.annotations.Nullable
 *  org.spongepowered.asm.mixin.Mixin
 *  org.spongepowered.asm.mixin.Shadow
 *  org.spongepowered.asm.mixin.injection.At
 *  org.spongepowered.asm.mixin.injection.Inject
 *  org.spongepowered.asm.mixin.injection.callback.CallbackInfo
 *  org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
 */
package meteordevelopment.meteorclient.mixin;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.systems.modules.Modules;
import meteordevelopment.meteorclient.systems.modules.misc.InventoryTweaks;
import meteordevelopment.meteorclient.systems.modules.render.BetterTooltips;
import meteordevelopment.meteorclient.systems.modules.render.ItemHighlight;
import net.minecraft.class_11907;
import net.minecraft.class_11908;
import net.minecraft.class_11909;
import net.minecraft.class_1703;
import net.minecraft.class_1713;
import net.minecraft.class_1735;
import net.minecraft.class_1799;
import net.minecraft.class_2561;
import net.minecraft.class_332;
import net.minecraft.class_364;
import net.minecraft.class_3936;
import net.minecraft.class_4185;
import net.minecraft.class_437;
import net.minecraft.class_465;
import net.minecraft.class_5684;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(value={class_465.class})
public abstract class HandledScreenMixin<T extends class_1703>
extends class_437
implements class_3936<T> {
    @Shadow
    protected class_1735 field_2787;
    @Shadow
    protected int field_2776;
    @Shadow
    protected int field_2800;
    @Shadow
    private boolean field_2783;

    @Shadow
    @Nullable
    protected abstract class_1735 method_64240(double var1, double var3);

    @Shadow
    public abstract T method_17577();

    @Shadow
    protected abstract void method_2383(class_1735 var1, int var2, int var3, class_1713 var4);

    @Shadow
    public abstract void method_25419();

    public HandledScreenMixin(class_2561 title) {
        super(title);
    }

    @Inject(method={"method_25426"}, at={@At(value="TAIL")})
    private void onInit(CallbackInfo info) {
        InventoryTweaks invTweaks = Modules.get().get(InventoryTweaks.class);
        if (invTweaks.isActive() && invTweaks.showButtons() && invTweaks.canSteal((class_1703)this.method_17577())) {
            this.method_37063((class_364)new class_4185.class_7840((class_2561)class_2561.method_43470((String)"Steal"), button -> invTweaks.steal((class_1703)this.method_17577())).method_46433(this.field_2776, this.field_2800 - 22).method_46437(40, 20).method_46431());
            this.method_37063((class_364)new class_4185.class_7840((class_2561)class_2561.method_43470((String)"Dump"), button -> invTweaks.dump((class_1703)this.method_17577())).method_46433(this.field_2776 + 42, this.field_2800 - 22).method_46437(40, 20).method_46431());
        }
    }

    @Inject(method={"method_25403"}, at={@At(value="TAIL")})
    private void onMouseDragged(class_11909 click, double offsetX, double offsetY, CallbackInfoReturnable<Boolean> cir) {
        if (click.method_74245() != 0 || this.field_2783 || !Modules.get().get(InventoryTweaks.class).mouseDragItemMove()) {
            return;
        }
        class_1735 slot = this.method_64240(click.comp_4798(), click.comp_4799());
        if (slot != null && slot.method_7681() && MeteorClient.mc.method_74187()) {
            this.method_2383(slot, slot.field_7874, click.method_74245(), class_1713.field_7794);
        }
    }

    @Inject(method={"method_25402"}, at={@At(value="HEAD")}, cancellable=true)
    private void mouseClicked(class_11909 click, boolean doubled, CallbackInfoReturnable<Boolean> cir) {
        BetterTooltips tooltips = Modules.get().get(BetterTooltips.class);
        if (tooltips.shouldOpenContents((class_11907)click) && this.field_2787 != null && !this.field_2787.method_7677().method_7960() && this.method_17577().method_34255().method_7960() && tooltips.openContent(this.field_2787.method_7677())) {
            cir.setReturnValue((Object)true);
        }
    }

    @Inject(method={"method_25404"}, at={@At(value="HEAD")}, cancellable=true)
    private void keyPressed(class_11908 input, CallbackInfoReturnable<Boolean> cir) {
        BetterTooltips tooltips = Modules.get().get(BetterTooltips.class);
        if (tooltips.shouldOpenContents((class_11907)input) && this.field_2787 != null && !this.field_2787.method_7677().method_7960() && this.method_17577().method_34255().method_7960() && tooltips.openContent(this.field_2787.method_7677())) {
            cir.setReturnValue((Object)true);
        }
    }

    @Inject(method={"method_2385"}, at={@At(value="HEAD")})
    private void onDrawSlot(class_332 context, class_1735 slot, int mouseX, int mouseY, CallbackInfo ci) {
        int color = Modules.get().get(ItemHighlight.class).getColor(slot.method_7677());
        if (color != -1) {
            context.method_25294(slot.field_7873, slot.field_7872, slot.field_7873 + 16, slot.field_7872 + 16, color);
        }
    }

    @ModifyReturnValue(method={"method_62001"}, at={@At(value="RETURN")})
    private boolean isTooltipSticky(boolean original, class_1799 item) {
        Object var4_3 = item.method_32347().orElse(null);
        if (var4_3 instanceof class_5684) {
            class_5684 component = var4_3;
            return original || component.method_62003();
        }
        return original;
    }
}

