/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2026 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.module.modules.exploit.disabler.disablers

import net.ccbluex.liquidbounce.config.autoconfig.AutoConfig
import net.ccbluex.liquidbounce.config.types.group.ToggleableValueGroup
import net.ccbluex.liquidbounce.event.tickHandler
import net.ccbluex.liquidbounce.event.waitTicks
import net.ccbluex.liquidbounce.features.module.modules.exploit.disabler.ModuleDisabler
import net.ccbluex.liquidbounce.lang.translation
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.ccbluex.liquidbounce.utils.item.getEnchantment
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket
import net.minecraft.network.protocol.game.ServerboundUseItemPacket
import net.minecraft.world.InteractionHand
import net.minecraft.world.item.Items
import net.minecraft.world.item.enchantment.Enchantments

/**
 * Exploits the Riptide except of Vulcan in order to disable any movement checks
 *
 * This seems to be patched in newer versions of Vulcan
 * This disabler was added in 02/07/23, which indicates it was probably made for Vulcan 2.7.5
 */
internal object DisablerVulcanRiptide : ToggleableValueGroup(ModuleDisabler, "VulcanRiptide", false) {

    override fun onEnabled() {
        if (AutoConfig.loadingNow) {
            return
        }

        if (!checkItem()) {
            chat(markAsError(translation("liquidbounce.module.disabler.messages.vulcanRiptideMessage")))
            enabled = false
        }
    }

    @Suppress("unused")
    val repeatable = tickHandler {
        if (checkItem()) {
            waitTicks(20)
            interaction.startPrediction(world) { sequence ->
                ServerboundUseItemPacket(InteractionHand.OFF_HAND, sequence, player.yRot, player.xRot)
            }
            network.send(
                ServerboundPlayerActionPacket(
                    ServerboundPlayerActionPacket.Action.RELEASE_USE_ITEM, BlockPos.ZERO, Direction.DOWN
                )
            )
        }
    }

    private fun checkItem(): Boolean {
        val offhand = player.offhandItem
        return (offhand.item == Items.TRIDENT && offhand.isEnchanted &&
            offhand.getEnchantment(Enchantments.RIPTIDE) != 0)
    }
}
