/*
 * 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

import net.ccbluex.liquidbounce.config.types.list.Tagged
import net.ccbluex.liquidbounce.features.module.ClientModule
import net.ccbluex.liquidbounce.features.module.ModuleCategories
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.ccbluex.liquidbounce.utils.network.sendHeldItemChange
import net.ccbluex.liquidbounce.utils.text.PlainText
import net.minecraft.network.protocol.game.ServerboundAttackPacket
import net.minecraft.network.protocol.game.ServerboundInteractPacket
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket
import net.minecraft.world.InteractionHand
import net.minecraft.world.phys.Vec3
import kotlin.random.Random

/**
 * Kick module
 *
 * Kick yourself from the server.
 */
object ModuleKick : ClientModule("Kick", ModuleCategories.EXPLOIT, disableActivation = true) {

    init {
        doNotIncludeAlways()
    }

    private val mode by enumChoice("Mode", KickModeEnum.QUIT)

    override fun onEnabled() {
        if (mc.hasSingleplayerServer()) {
            chat(markAsError(message("cantEnableInSingleplayer")))
            return
        }

        kick(mode)
    }

    /**
     * Kick client player
     *
     * @warning Only use on multiplayer! This function can cause the game to crash when used on an integrated server.
     */
    fun kick(mode: KickModeEnum) = mode.run()

    @Suppress("unused")
    enum class KickModeEnum(override val tag: String, val run: () -> Unit) : Tagged {
        QUIT("Quit", {
            world.disconnect(PlainText.EMPTY)
        }),
        INVALID_PACKET("InvalidPacket", {
            network.send(
                ServerboundMovePlayerPacket.Pos(
                    Double.NaN,
                    Double.NEGATIVE_INFINITY,
                    Double.POSITIVE_INFINITY,
                    !player.onGround(),
                    !player.horizontalCollision
                )
            )
        }),
        SELF_HURT("SelfHurt", {
            network.send(ServerboundAttackPacket(player.id))
        }),
        ILLEGAL_CHAT("IllegalChat", {
            network.sendChat("§${Random.nextInt()}§${Random.nextInt()}§")
        }),
        ILLEGAL_SWTTCHITEM("IllegalSwitchitem", {
            network.sendHeldItemChange(-1)
        }),
        ILLEGAL_INTERACT("IllegalInteract", {
            network.send(
                ServerboundInteractPacket(player.id, InteractionHand.MAIN_HAND, Vec3.ZERO, player.isShiftKeyDown)
            )
        }),
    }

}
