/*
 * 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.creativetab.tabs

import net.ccbluex.fastutil.mapToArray
import net.ccbluex.liquidbounce.features.creativetab.CustomCreativeModeTab
import net.ccbluex.liquidbounce.utils.text.asPlainText
import net.ccbluex.liquidbounce.utils.text.textOf
import net.ccbluex.liquidbounce.utils.item.createSplashPotion
import net.ccbluex.liquidbounce.utils.kotlin.toTypedArray
import net.minecraft.core.component.DataComponentPatch
import net.minecraft.core.component.DataComponents
import net.minecraft.core.registries.BuiltInRegistries
import net.minecraft.tags.ItemTags
import net.minecraft.world.effect.MobEffectInstance
import net.minecraft.world.effect.MobEffects
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.minecraft.world.item.component.BlocksAttacks
import net.minecraft.world.level.block.Blocks
import java.util.Optional

/**
 * https://github.com/ViaVersion/ViaVersion/blob/081fc0db41a06faa4465402ec25c794392539f2a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21_4to1_21_5/rewriter/BlockItemPacketRewriter1_21_5.java#L514
 */
private val OLD_SWORD_BLOCKING_COMPONENT = BlocksAttacks(
    0f,
    0f,
    listOf(
        BlocksAttacks.DamageReduction(
            90f,
            Optional.empty(),
            -0.5f,
            0.5f
        )
    ),
    BlocksAttacks.ItemDamageFunction(0f, 0f, 0f),
    Optional.empty(),
    Optional.empty(),
    Optional.empty(),
)

class ExploitsCreativeModeTab : CustomCreativeModeTab(
    "Exploits",
    icon = { Items.LINGERING_POTION.defaultInstance },
    items = {
        // Old swords
        val oldSwords = BuiltInRegistries.ITEM.listElements().filter { item ->
            item.`is`(ItemTags.SWORDS)
        }.map { item ->
            ItemStack(
                item,
                1,
                DataComponentPatch.builder()
                    .set(DataComponents.BLOCKS_ATTACKS, OLD_SWORD_BLOCKING_COMPONENT)
                    .set(DataComponents.CUSTOM_NAME, textOf(
                        "1.8.9 ".asPlainText(), item.value().getName(item.value().defaultInstance)))
                    .build(),
            )
        }.toList()

        it.acceptAll(oldSwords)

        // Add unobtainable items
        it.accept(ItemStack(Blocks.COMMAND_BLOCK))
        it.accept(ItemStack(Blocks.CHAIN_COMMAND_BLOCK))
        it.accept(ItemStack(Blocks.REPEATING_COMMAND_BLOCK))
        it.accept(ItemStack(Items.COMMAND_BLOCK_MINECART))
        it.accept(ItemStack(Blocks.END_PORTAL_FRAME))
        it.accept(ItemStack(Blocks.DRAGON_EGG))
        it.accept(ItemStack(Blocks.BARRIER))
        it.accept(ItemStack(Blocks.JIGSAW))
        it.accept(ItemStack(Blocks.STRUCTURE_BLOCK))
        it.accept(ItemStack(Blocks.STRUCTURE_VOID))
        it.accept(ItemStack(Blocks.SPAWNER))
        it.accept(ItemStack(Items.DEBUG_STICK))

        // Add unobtainable potions
        it.accept(createSplashPotion("Troll Potion",
            effects = BuiltInRegistries.MOB_EFFECT.listElements().toTypedArray()
                .mapToArray { statusEffect ->
                    MobEffectInstance(statusEffect, Int.MAX_VALUE, 127)
                })
        )

        it.accept(createSplashPotion("Kill Potion",
            MobEffectInstance(MobEffects.INSTANT_HEALTH, 0, 125),
            MobEffectInstance(MobEffects.INSTANT_DAMAGE, 0, 125)
        ))

    }
)
