/*
 * Copyright (c) 2026 盐灯水母
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.remains_cave.dungeon.tomb;

import com.mojang.serialization.Codec;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.entity.EntityType;
import com.remains_cave.entity.ModEntities;
import org.jetbrains.annotations.NotNull;

public enum ChallengeType implements StringRepresentable {
    NONE("none"),
    WARRIOR("warrior"),
    ARCHER("archer"),
    WARLOCK("warlock");

    private final String name;

    ChallengeType(String name) {
        this.name = name;
    }

    @Override
    public @NotNull String getSerializedName() {
        return name;
    }

    public static final StreamCodec<ByteBuf, ChallengeType> STREAM_CODEC = ByteBufCodecs.STRING_UTF8
            .map(ChallengeType::fromName, ChallengeType::getSerializedName);

    public static final Codec<ChallengeType> CODEC = StringRepresentable.fromEnum(ChallengeType::values);

    public static ChallengeType fromName(String name) {
        for (ChallengeType type : values()) {
            if (type.getSerializedName().equals(name)) {
                return type;
            }
        }
        return NONE; // 或 throw new IllegalArgumentException("No enum constant for " + name);
    }

    public EntityType<?> getEliteEntityType() {
        return switch (this) {
            case WARRIOR -> ModEntities.SKELETON_WARRIOR.get();
            case ARCHER -> ModEntities.SKELETON_ARCHER.get();
            case WARLOCK -> ModEntities.SKELETON_WARLOCK.get();
            default -> null;
        };
    }
}