package dev.crucifixbeta.mod.modules.impl.player;

import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.UpdateEvent;
import dev.crucifixbeta.api.utils.math.Timer;
import dev.crucifixbeta.mod.modules.Module;
import dev.crucifixbeta.mod.modules.settings.impl.EnumSetting;
import dev.crucifixbeta.mod.modules.settings.impl.SliderSetting;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.SlotActionType;

@Environment(EnvType.CLIENT)
public class Replenish extends Module {
   private final EnumSetting<Replenish.Mode> mode = this.add(new EnumSetting<>("Mode", Replenish.Mode.QuickMove));
   private final SliderSetting delay = this.add(new SliderSetting("Delay", 2.0, 0.0, 5.0, 0.01).setSuffix("s"));
   private final SliderSetting min = this.add(new SliderSetting("Min", 50, 1, 100)).setSuffix("%");
   private final SliderSetting forceDelay = this.add(new SliderSetting("ForceDelay", 0.2, 0.0, 4.0, 0.01).setSuffix("s"));
   private final SliderSetting forceMin = this.add(new SliderSetting("ForceMin", 16, 1, 100)).setSuffix("%");
   private final Timer timer = new Timer();

   public Replenish() {
      super("Replenish", Module.Category.Player);
      this.setChinese("\u7269\u54c1\u680f\u8865\u5145");
   }

   @EventListener
   public void onUpdate(UpdateEvent event) {
      for (int var2 = 0; var2 < 9; var2++) {
         if (this.replenish(var2)) {
            this.timer.reset();
            return;
         }
      }
   }

   private boolean replenish(int slot) {
      ItemStack var2 = mc.player.getInventory().getStack(slot);
      if (var2.isEmpty()) {
         return false;
      } else if (!var2.isStackable()) {
         return false;
      } else {
         int var3 = (int)((double)var2.getCount() / (double)var2.getMaxCount() * 100.0);
         if ((double)var3 > this.min.getValue()) {
            return false;
         } else {
            for (int var4 = 9; var4 < 36; var4++) {
               ItemStack var5 = mc.player.getInventory().getStack(var4);
               if (!var5.isEmpty() && Sorter.canMerge(var2, var5)) {
                  if ((float)var3 > this.forceMin.getValueFloat() ? this.timer.passedS(this.delay.getValue()) : this.timer.passedS(this.forceDelay.getValue())) {
                     switch ((Replenish.Mode)this.mode.getValue()) {
                        case QuickMove:
                           mc.interactionManager.clickSlot(mc.player.playerScreenHandler.syncId, var4, 0, SlotActionType.QUICK_MOVE, mc.player);
                           break;
                        case ClickSlot:
                           mc.interactionManager.clickSlot(mc.player.playerScreenHandler.syncId, var4, 0, SlotActionType.PICKUP, mc.player);
                           mc.interactionManager.clickSlot(mc.player.playerScreenHandler.syncId, slot + 36, 0, SlotActionType.PICKUP, mc.player);
                           mc.interactionManager.clickSlot(mc.player.playerScreenHandler.syncId, var4, 0, SlotActionType.PICKUP, mc.player);
                     }

                     return true;
                  }

                  return false;
               }
            }

            return false;
         }
      }
   }
@Environment(EnvType.CLIENT)
   public static enum Mode {
      QuickMove,
      ClickSlot;

}
}
