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

import io.github.kosmx.emotes.api.events.client.ClientEmoteAPI;
import io.github.kosmx.emotes.api.events.server.ServerEmoteAPI;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModList;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.eroticdungeongame.api.workspace.Services;
import top.r3944realms.lib39.core.compat.ICompat;

public class EmoteCraftCompat implements ICompat {
    public static EmoteCraftCompat INSTANCE = new EmoteCraftCompat();
    public static ResourceLocation ID = EroticDungeon.rl("emotecraft");

    public boolean initialize = false;
    @Override
    public void setInitialize(boolean initialize) {
        this.initialize = initialize;
    }

    @Override
    public boolean isInitialized() {
        return initialize;
    }

    @Override
    public ResourceLocation id() {
        return ID;
    }

    @Override
    public boolean isModLoaded() {
        return ModList.get().isLoaded("emotecraft");
    }

    @Override
    public void initialize() {

    }

    @Override
    public void addCommonGameListener(IEventBus gameBus) {
        gameBus.addListener(this::onServerTick);
    }

    @Override
    public void addClientGameListener(IEventBus gameBus) {
        gameBus.addListener(this::onClientTick);
    }

    private void onServerTick (TickEvent.ServerTickEvent event) {
        for (ServerPlayer player : event.getServer().getPlayerList().getPlayers()) {
            if (Services.WORK_SPACE.isInDevice(player)) {
                ServerEmoteAPI.forcePlayEmote(player.getUUID(), null);
            }
        }
    }
    @OnlyIn(Dist.CLIENT)
    private void onClientTick (TickEvent.RenderTickEvent event) {
        LocalPlayer player = Minecraft.getInstance().player;
        if (player != null && Services.WORK_SPACE.isInDevice(player)) {
            ClientEmoteAPI.playEmote(null);
        }
    }
}
