package com.extendedae_plus.mixin.advancedae.menu;

import appeng.menu.AEBaseMenu;
import appeng.menu.guisync.GuiSync;
import com.extendedae_plus.api.smartDoubling.IPatternProviderMenuDoublingSync;
import com.extendedae_plus.api.smartDoubling.ISmartDoublingHolder;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.MenuType;
import net.pedroksl.advanced_ae.common.logic.AdvPatternProviderLogic;
import net.pedroksl.advanced_ae.common.logic.AdvPatternProviderLogicHost;
import net.pedroksl.advanced_ae.gui.advpatternprovider.AdvPatternProviderMenu;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(AdvPatternProviderMenu.class)
public abstract class AdvPatternProviderMenuDoublingMixin implements IPatternProviderMenuDoublingSync {
    @Final
    @Shadow(remap = false)
    protected AdvPatternProviderLogic logic;

    @Unique
    @GuiSync(21)
    public boolean eap$SmartDoubling = false;
    @Unique
    @GuiSync(22)
    public int eap$PerProviderScalingLimit = 0; // 0 = no limit

    @Inject(method = "broadcastChanges", at = @At("HEAD"))
    private void eap$syncSmartDoubling(CallbackInfo ci) {
        if (!((AEBaseMenu) (Object) this).isClientSide()) {
            var l = this.logic;
            if (l instanceof ISmartDoublingHolder holder) {
                this.eap$SmartDoubling = holder.eap$getSmartDoubling();
                this.eap$PerProviderScalingLimit = holder.eap$getProviderSmartDoublingLimit();
            }
        }
    }

    @Inject(method = "<init>(ILnet/minecraft/world/entity/player/Inventory;Lnet/pedroksl/advanced_ae/common/logic/AdvPatternProviderLogicHost;)V", at = @At("TAIL"), remap = false)
    private void eap$initSmartSync_Public(int id, Inventory playerInventory, AdvPatternProviderLogicHost host, CallbackInfo ci) {
        try {
            var l = this.logic;
            if (l instanceof ISmartDoublingHolder holder) {
                this.eap$SmartDoubling = holder.eap$getSmartDoubling();
                this.eap$PerProviderScalingLimit = holder.eap$getProviderSmartDoublingLimit();
            }
        } catch (Throwable ignored) {}
    }

    @Inject(method = "<init>(Lnet/minecraft/world/inventory/MenuType;ILnet/minecraft/world/entity/player/Inventory;Lnet/pedroksl/advanced_ae/common/logic/AdvPatternProviderLogicHost;)V", at = @At("TAIL"), remap = false)
    private void eap$initSmartSync_Protected(MenuType menuType, int id, Inventory playerInventory, AdvPatternProviderLogicHost host, CallbackInfo ci) {
        try {
            var l = this.logic;
            if (l instanceof ISmartDoublingHolder holder) {
                this.eap$SmartDoubling = holder.eap$getSmartDoubling();
                this.eap$PerProviderScalingLimit = holder.eap$getProviderSmartDoublingLimit();
            }
        } catch (Throwable ignored) {}
    }

    @Override
    public boolean eap$getSmartDoublingSynced() {
        return this.eap$SmartDoubling;
    }

    @Override
    public int eap$getScalingLimit() {
        return this.eap$PerProviderScalingLimit;
    }
}
