package zenith.pl.client.modules.impl.player;
import com.darkmagician6.eventapi.EventTarget;
import net.minecraft.block.Blocks;
import net.minecraft.block.CarrotsBlock;
import net.minecraft.item.Items;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import zenith.pl.base.events.impl.player.EventUpdate;
import zenith.pl.client.modules.api.Category;
import zenith.pl.client.modules.api.Module;
import zenith.pl.client.modules.api.ModuleAnnotation;
import zenith.pl.client.modules.api.setting.impl.BooleanSetting;
import zenith.pl.utility.game.player.PlayerInventoryUtil;
@ModuleAnnotation(name = "AutoFarm", category = Category.PLAYER, description = "Фармит морковь с помощью мотыги и зачарования посев")
public class AutoFarm extends Module {
    public static final AutoFarm INSTANCE = new AutoFarm();
    public static final BooleanSetting posevEnch = new BooleanSetting("Зачарование Посев", false);
    int mealSlot = -1;
    int carrotSlot = -1;
    private int getHoeSlot() {
        if (PlayerInventoryUtil.searchHotbarItem(Items.NETHERITE_HOE) != -1) {
            return PlayerInventoryUtil.searchHotbarItem(Items.NETHERITE_HOE);
        } else if (PlayerInventoryUtil.searchHotbarItem(Items.DIAMOND_HOE) != -1) {
            return PlayerInventoryUtil.searchHotbarItem(Items.DIAMOND_AXE);
        } else if (PlayerInventoryUtil.searchHotbarItem(Items.GOLDEN_HOE) != -1) {
            return PlayerInventoryUtil.searchHotbarItem(Items.GOLDEN_HOE);
        } else if (PlayerInventoryUtil.searchHotbarItem(Items.IRON_HOE) != -1) {
            return PlayerInventoryUtil.searchHotbarItem(Items.IRON_HOE);
        } else if (PlayerInventoryUtil.searchHotbarItem(Items.STONE_HOE) != -1) {
            return PlayerInventoryUtil.searchHotbarItem(Items.STONE_HOE);
        } else if (PlayerInventoryUtil.searchHotbarItem(Items.WOODEN_HOE) != -1) {
            return PlayerInventoryUtil.searchHotbarItem(Items.WOODEN_HOE);
        }
        return -1;
    }
    @EventTarget
    public void onUpdate(EventUpdate event) {
        mealSlot = PlayerInventoryUtil.searchHotbarItem(Items.BONE_MEAL);
        carrotSlot = PlayerInventoryUtil.searchHotbarItem(Items.CARROT);
        if (getHoeSlot() == -1) return;
        if (carrotSlot == -1) return;
        if (mealSlot == -1 && !posevEnch.isEnabled()) return;
        if (mc.crosshairTarget instanceof BlockHitResult hitResult) {
            if (mc.world.getBlockState(hitResult.getBlockPos()).getBlock() == Blocks.FARMLAND) {
                mc.player.getInventory().selectedSlot = carrotSlot;
                mc.interactionManager.interactBlock(mc.player, Hand.MAIN_HAND, hitResult);
            } else if (mc.world.getBlockState(hitResult.getBlockPos()).getBlock() == Blocks.CARROTS) {
                BlockPos carrotsPos = hitResult.getBlockPos();
                if (mc.world.getBlockState(carrotsPos).getBlock() instanceof CarrotsBlock carrotsBlock) {
                    if (carrotsBlock.getAge(mc.world.getBlockState(carrotsPos)) == 7) {
                        mc.interactionManager.attackBlock(hitResult.getBlockPos(), hitResult.getSide());
                        mc.player.swingHand(Hand.MAIN_HAND);
                    } else {
                        if (!posevEnch.isEnabled()) {
                            mc.player.getInventory().selectedSlot = mealSlot;
                        } else mc.player.getInventory().selectedSlot = getHoeSlot();
                        mc.interactionManager.interactBlock(mc.player, Hand.MAIN_HAND, hitResult);
                        mc.player.swingHand(Hand.MAIN_HAND);
                    }
                }
            }
        }
    }
}