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

import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.capabilities.Capability;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.content.animation.IEDGAnimation;
import top.r3944realms.eroticdungeongame.content.block.ISeatBlock;
import top.r3944realms.eroticdungeongame.content.block.blockentity.BaseSeatBlockEntity;
import top.r3944realms.eroticdungeongame.content.util.FurnitureHelper;
import top.r3944realms.eroticdungeongame.core.device.ISeatType;
import top.r3944realms.eroticdungeongame.core.network.EDGNetworkHandler;
import top.r3944realms.eroticdungeongame.core.network.toServer.RefreshPlayerAnimationPacket;
import top.r3944realms.eroticdungeongame.util.IEDGEntity;
import top.r3944realms.lib39.core.sync.CachedSyncManager;

import java.util.HashMap;
import java.util.Map;

public class DungeonDataSyncManager extends CachedSyncManager<Capability<AbstractPlayerDungeonData>, AbstractPlayerDungeonData> {
    public Map<Capability<AbstractPlayerDungeonData>, AbstractPlayerDungeonData> playerDungeonData = new HashMap<>();
    @Override
    public Map<Capability<AbstractPlayerDungeonData>, AbstractPlayerDungeonData> getSyncMap() {
        return playerDungeonData;
    }
    static int cooldown = 0;
    public static void tick(@NotNull Player player) {
        if (cooldown > 0) {
            cooldown--;
        }
        player.getCapability(PlayerDungeonDataProvider.PLAYER_DUNGEON_DATA_CAP).ifPresent(
          cap -> {
              if (player.level().isClientSide) {
                  clientTick(player, cap);
              } else if (cap.getDeviceMainBlockPos() != null) {
                  if (cap.getEyeHeight() != -1.0 && cap.getEyeHeight() >= 0.0) {
                      EntityDimensions dimensions = player.getDimensions(player.getPose());
                      player.eyeHeight = cap.getEyeHeight() * dimensions.height;
                  }

              } else IEDGAnimation.stop(player);
          }
        );
    }

    @OnlyIn(Dist.CLIENT)
    public static void clientTick(@NotNull Player player, AbstractPlayerDungeonData dungeonData) {
        if (IEDGAnimation.getPlayingAnimation(player) == null) {
            if (dungeonData.getDeviceMainBlockPos() != null && player.level().getBlockEntity(dungeonData.getDeviceMainBlockPos()) instanceof BaseSeatBlockEntity baseSeatBlockEntity) {
                ISeatType seatType = FurnitureHelper.getSeatType(baseSeatBlockEntity.getBlockState().getBlock());
                if (seatType != null && cooldown == 0) {
                    EDGNetworkHandler.sendToServer(new RefreshPlayerAnimationPacket(player.getUUID()));
                    cooldown = 100; //5s
                }
            }
        }
    }

    public static void login(@NotNull Player player) {
        player.getCapability(PlayerDungeonDataProvider.PLAYER_DUNGEON_DATA_CAP).ifPresent(
                cap -> {
                    if (cap.getDeviceMainBlockPos() != null) {
                        BlockState blockState = player.level().getBlockState(cap.getDeviceMainBlockPos());
                        if (blockState.getBlock() instanceof ISeatBlock iSeatBlock) {
                            if (!iSeatBlock.bindPlayerToSeat(player.level(), cap.getDeviceMainBlockPos(), blockState, player)) {
                                cap.clearDungeonData(player);
                            } else return;
                        }
                    }
                    IEDGAnimation.stop(player);
                }
        );
    }

    public static void logout(@NotNull Player player) {
        player.getCapability(PlayerDungeonDataProvider.PLAYER_DUNGEON_DATA_CAP).ifPresent(
                cap -> {
                    if (cap.getDeviceMainBlockPos() != null && player.level().getBlockEntity(cap.getDeviceMainBlockPos()) instanceof BaseSeatBlockEntity seat) {
                        ((IEDGEntity)player).releaseEntityFromEDGDevice(true);
                        FurnitureHelper.setSeatOccupied(player.level(), cap.getDeviceMainBlockPos(), false);
                    }
                }
        );
    }

}

