/*
 * 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 kotlinx.coroutines.delay
import net.ccbluex.liquidbounce.config.types.list.Tagged
import net.ccbluex.liquidbounce.event.events.ChatReceiveEvent
import net.ccbluex.liquidbounce.event.waitMatchesWithTimeout
import net.ccbluex.liquidbounce.features.module.ClientModule
import net.ccbluex.liquidbounce.features.module.ModuleCategories
import net.ccbluex.liquidbounce.utils.client.ServerObserver
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.text.joinToText
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.ccbluex.liquidbounce.utils.client.regular
import net.ccbluex.liquidbounce.utils.client.variable
import net.ccbluex.liquidbounce.utils.kotlin.EventPriorityConvention.FIRST_PRIORITY
import kotlin.time.Duration.Companion.seconds

/**
 * Plugins module
 *
 * Allows you to see a server's plugins.
 */
object ModulePlugins : ClientModule("Plugins", ModuleCategories.EXPLOIT, disableOnQuit = true) {

    private val tryState by multiEnumChoice("Try", TryState.entries)

    override suspend fun enabledEffect() {
        // Works only on servers that did not block the plugins command.
        if (TryState.SLASH in tryState) {
            network.sendCommand("plugins")
            val event = waitPluginsMessage()

            if (event == null) {
                delay(1.seconds)
            } else {
                enabled = false
                return
            }
        }

        // Use bukkit:plugins to get plugins, this might work on some servers if they blocked the plugins command.
        if (TryState.COLON_SLASH in tryState) {
            network.sendCommand("bukkit:plugins")
            val event = waitPluginsMessage()

            if (event == null) {
                delay(1.seconds)
            } else {
                enabled = false
                return
            }
        }

        // Exploits tab completion to get plugins - this will not show all of them, but it will show some.
        if (TryState.TAB_COMPLETE in tryState) {
            // Timeout after 5 seconds.
            if (ServerObserver.captureCommandSuggestions(5.seconds)) {
                val plugins = ServerObserver.formattedPluginList!!
                chat(regular(message(
                    "plugins",
                    variable(plugins.size.toString()),
                    plugins.joinToText(separator = regular(", ")))
                ))
            } else {
                chat(markAsError(message("noPluginsFound")))
            }
            enabled = false
            return
        }

        // If none of the methods worked out, disable the module.
        chat(message("timeOut"))
        enabled = false
    }

    private suspend fun waitPluginsMessage() =
        waitMatchesWithTimeout<ChatReceiveEvent>(timeout = 5.seconds, priority = FIRST_PRIORITY) {
            // Only handle game messages. It is unlikely that any server will use a player for the chat game.
            it.type == ChatReceiveEvent.ChatType.GAME_MESSAGE &&
                // Server has already responded with a list of plugins.
                it.message.contains("Plugins (")

            // Sadly we cannot use the takeInput here, because we cannot cancel the message with this event.
            // Might change this in the future.
        }

    private enum class TryState(override val tag: String) : Tagged {
        SLASH("Slash"),
        COLON_SLASH("ColonSlash"),
        TAB_COMPLETE("TabComplete")
    }
}
