/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.content;

import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.BootstapContext;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.damagesource.DamageScaling;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.EroticDungeon;

@SuppressWarnings("OptionalGetWithoutIsPresent")
public class EDGDamageTypes {
    public static final ResourceKey<DamageType> FUCKED = ResourceKey.create(Registries.DAMAGE_TYPE, EroticDungeon.rl("fucked"));
    public static final ResourceKey<DamageType> WHIPPED = ResourceKey.create(Registries.DAMAGE_TYPE, EroticDungeon.rl("whipped"));

    @Contract("_ -> new")
    public static @NotNull DamageSource causeFuckedDamage(@NotNull RegistryAccess registryAccess) {
        return new DamageSourceRandomMessages(registryAccess.registry(Registries.DAMAGE_TYPE).get().getHolderOrThrow(FUCKED), 2);
    }
    @Contract("_, _ -> new")
    public static @NotNull DamageSource causeFuckedDamage(@NotNull RegistryAccess registryAccess, Entity source) {
        return new DamageSourceRandomMessages(registryAccess.registry(Registries.DAMAGE_TYPE).get().getHolderOrThrow(FUCKED), source, 2);
    }
    @Contract("_ -> new")
    public static @NotNull DamageSource causeWhipDamage(@NotNull RegistryAccess registryAccess) {
        return new DamageSourceRandomMessages(
                registryAccess.registry(Registries.DAMAGE_TYPE)
                        .get()
                        .getHolderOrThrow(WHIPPED),
                3
        );
    }
    @Contract("_, _ -> new")
    public static @NotNull DamageSource causeWhipDamage(@NotNull RegistryAccess registryAccess, Entity source) {
        return new DamageSourceRandomMessages(
                registryAccess.registry(Registries.DAMAGE_TYPE)
                        .get()
                        .getHolderOrThrow(WHIPPED),
                source,
                3 // 死亡信息数量
        );
    }
    private static class DamageSourceRandomMessages extends DamageSource {

        private final int messageCount;

        public DamageSourceRandomMessages(Holder.Reference<DamageType> message, int messageCount) {
            super(message);
            this.messageCount = messageCount;
        }

        public DamageSourceRandomMessages(Holder.Reference<DamageType> message, Entity source, int messageCount) {
            super(message, source);
            this.messageCount = messageCount;
        }

        @Override
        public @NotNull Component getLocalizedDeathMessage(@NotNull LivingEntity attacked) {
            int type = attacked.getRandom().nextInt(this.messageCount);
            String s = "death.attack." + this.getMsgId() + "_" + type;
            Entity entity = this.getDirectEntity() == null ? this.getEntity() : this.getDirectEntity();
            if (entity != null) {
                return Component.translatable(s + ".entity", attacked.getDisplayName(), entity.getDisplayName());
            }else{
                return Component.translatable(s, attacked.getDisplayName());
            }
        }
    }
    public static void bootstrap(@NotNull BootstapContext<DamageType> context) {
        context.register(FUCKED, new DamageType(
                "fucked",
                DamageScaling.ALWAYS,
                0.2F
        ));
        context.register(WHIPPED, new DamageType(
                "whipped",
                DamageScaling.ALWAYS,
                0.2F
        ));


    }
}
