/*
 * 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.types.group.ToggleableValueGroup
import net.ccbluex.liquidbounce.event.events.BlinkPacketEvent
import net.ccbluex.liquidbounce.event.events.TickPacketProcessEvent
import net.ccbluex.liquidbounce.event.events.TransferOrigin
import net.ccbluex.liquidbounce.event.events.WorldChangeEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.features.blink.BlinkManager
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.warning
import net.minecraft.network.protocol.common.ClientboundKeepAlivePacket
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.minecraft.network.protocol.common.ClientboundPingPacket
import net.minecraft.network.protocol.game.ClientboundPlayerPositionPacket

/**
 * CubeCraft Full Ping Spoof Disabler
 * Working as of  June 13, 2026
 */

internal object DisablerCubeCraft : ToggleableValueGroup(ModuleDisabler, "CubeCraft", false) {

    override fun onEnabled() {
        chat(warning(translation("liquidbounce.module.disabler.messages.cubecraftDisablerEnableMessage")))
    }

    override fun onDisabled() {
        BlinkManager.flush(TransferOrigin.INCOMING)
    }

    @Suppress("unused")
    private val worldChangeHandler = handler<WorldChangeEvent> {
        BlinkManager.packetQueue.removeIf { it.origin == TransferOrigin.INCOMING }
    }

    @Suppress("unused")
    private val blinkPacketHandler = handler<BlinkPacketEvent> { event ->
        if (event.origin != TransferOrigin.INCOMING) return@handler

        val packet = event.packet

        if (packet is ClientboundKeepAlivePacket || packet is ClientboundPingPacket) {
            event.action = BlinkManager.Action.QUEUE
        } else if (packet !is ClientboundPlayerPositionPacket) {
            event.action = BlinkManager.Action.PASS
        }
    }

    @Suppress("unused")
    private val tickHandler = handler<TickPacketProcessEvent> {
        if (mc.hasSingleplayerServer()) {
            chat(markAsError(translation("liquidbounce.module.disabler.messages.cubecraftCantEnableInSingleplayer")))
            enabled = false
            return@handler
        }

        val currentTime = System.currentTimeMillis()
        val delay = when {
            player.tickCount < 150 -> 5000L
            player.tickCount < 300 -> 10000L
            else -> 20000L
        }

        BlinkManager.flush { snapshot ->
            snapshot.origin == TransferOrigin.INCOMING && currentTime - snapshot.timestamp >= delay
        }
    }
}
