/*
 *  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.animation;


import io.zershyan.sccore.animation.AnimationApi;
import io.zershyan.sccore.animation.helper.AnimationHelper;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.eroticdungeongame.content.block.ISeatBlock;
import top.r3944realms.eroticdungeongame.content.block.type.*;

import java.util.Objects;

public enum EDGAnimation {
    CUFF_BED("cuff_bed", CuffBedBlock.class),
    PRONE_BENCH("prone_bench", ProneBenchBlock.class),
    IRON_CAGE("iron_cage", IronCageBlock.class),
    SPANISH_DONKEY("spanish_donkey", SpanishDonkeyBlock.class),
    HANGING_POST("hanging_post", HangingPostBlock.class),
    CUFF_POLE("cuff_pole", CuffPoleBlock.class),
    CRUX("crux", CruxBlock.class),
    PETRINE_CROSS("petrine_cross", PetrineCrossBlock.class),
    PILLORY("pillory", PilloryBlock.class),
    RACK("rack", RackBlock.class),
    WALL_RACK("wall_rack", WallRackBlock.class),
    X_CROSS("x_cross", XCrossBlock.class),;
    private final ResourceLocation baseId;
    private final Class<?> blockClass;
    public static final ResourceLocation LAYER = EroticDungeon.rl("top_height_layer");
    EDGAnimation(String baseId, Class<?> blockClass) {
        this.baseId = EroticDungeon.rl(baseId + "_pose_");
        this.blockClass = blockClass;
    }
    public @NotNull ResourceLocation getDefaultRL() {
        return this.baseId.withSuffix("01");
    }

    public @NotNull ResourceLocation getVarietyRL(int varNumber) {
        return this.baseId.withSuffix(String.format("%02d", Math.max(1, Math.min(99, varNumber))));
    }

    public static @NotNull ResourceLocation getDefaultRLStatic(@NotNull EDGAnimation animation) {
        return animation.getDefaultRL();
    }

    public static @NotNull ResourceLocation getVarietyRLStatic(@NotNull EDGAnimation animation, int varNumber) {
        return animation.getVarietyRL(varNumber);
    }

    @Nullable
    public static EDGAnimation getAnimation(@NotNull Class<?> blockClass) {
        if (ISeatBlock.class.isAssignableFrom(blockClass)) {
            for (EDGAnimation value : values()) {
                if (value.blockClass.equals(blockClass)) {
                    return value;
                }
            }
        }
        return null;
    }
    public void play(Player player) {
        if (player != null && !player.level().isClientSide) {
            AnimationHelper helper = AnimationApi.getHelper(player);
            helper.playAnimation(EDGAnimation.LAYER, baseId.withSuffix("01"));

        }
    }
    public void play(Player player, int varNumber) {
        if (player != null && !player.level().isClientSide) {
            AnimationHelper helper = AnimationApi.getHelper(player);
            helper.playAnimation(EDGAnimation.LAYER, baseId.withSuffix(String.format("%02d", Math.max(1, Math.min(99, varNumber)))));
        }
    }

    @Nullable
    public static ResourceLocation getPlayingAnimation(Player player) {
        if (player != null && !player.level().isClientSide) {
            AnimationHelper helper = AnimationApi.getHelper(player);
            return helper.getAnimationPlaying(EDGAnimation.LAYER);
        }
        return null;
    }

    public static boolean isPlayerAnimation(Player player, @NotNull EDGAnimation animation) {
        return Objects.equals(getPlayingAnimation(player), animation.getDefaultRL());
    }

    public static boolean isPlayerAnimation(Player player, @NotNull EDGAnimation animation, int varNumber) {
        return Objects.equals(getPlayingAnimation(player), animation.getVarietyRL(varNumber));
    }

    public static void stop(Player player) {
        if (player != null && !player.level().isClientSide) {
            AnimationHelper helper = AnimationApi.getHelper(player);
            helper.removeAnimation(EDGAnimation.LAYER);
        }
    }
}
