/*
 *  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.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 org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.eroticdungeongame.api.EroticDungeonGameApi;
import top.r3944realms.eroticdungeongame.content.animation.EDGAnimation;
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.util.IEDGEntity;
import top.r3944realms.lib39.core.sync.CachedSyncManager;

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

public class DungeonDataSyncManager extends CachedSyncManager<UUID, AbstractPlayerDungeonData> {
    public static Map<UUID, AbstractPlayerDungeonData> playerDungeonData = new HashMap<>();
    @Override
    public Map<UUID, AbstractPlayerDungeonData> getSyncMap() {
        return playerDungeonData;
    }

    @Nullable
    public AbstractPlayerDungeonData getPlayerDungeonData(UUID uuid) {
        return playerDungeonData.get(uuid);
    }

    public static void tick(@NotNull Player player) {
        player.getCapability(EroticDungeonGameApi.PLAYER_DUNGEON_DATA_CAP).ifPresent(
          cap -> {
              if (player.level().isClientSide) {
                  clientTick(player);
              } 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 EDGAnimation.stop(player);
          }
        );
    }

    @OnlyIn(Dist.CLIENT)
    public static void clientTick(@NotNull Player player) {

    }

    public static void login(@NotNull Player player) {
        player.getCapability(EroticDungeonGameApi.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;
                        }
                    }
                    EDGAnimation.stop(player);
                }
        );
    }

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

}

