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

import net.minecraft.world.entity.Entity;
import net.minecraftforge.eventbus.api.IEventBus;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.eroticdungeongame.api.EroticDungeonGameApi;
import top.r3944realms.eroticdungeongame.api.capability.IPlayerDungeonData;
import top.r3944realms.eroticdungeongame.api.workspace.IWorkSpaceHelper;
import top.r3944realms.eroticdungeongame.content.entity.SeatEntity;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class WorkSpaceHelper implements IWorkSpaceHelper {

    @Override
    public boolean isInDevice(@NotNull Entity player) {
        if (checkIsPlayer(player) && player.isPassenger() && player.getVehicle() instanceof SeatEntity) {
            AtomicBoolean hasSeat = new AtomicBoolean(false);
            player.getCapability(EroticDungeonGameApi.PLAYER_DUNGEON_DATA_CAP)
                    .ifPresent(i->{
                        if (i.getDeviceMainBlockPos() != null) {
                            hasSeat.set(true);
                        }
                    });
            return hasSeat.get();
        }
        return false;
    }

    @Override
    public void tryToDoIfInDevice(@NotNull Entity player, Consumer<IPlayerDungeonData> action, Runnable fallback) {
        if (checkIsPlayer(player) && player.isPassenger() && player.getVehicle() instanceof SeatEntity) {
            player.getCapability(EroticDungeonGameApi.PLAYER_DUNGEON_DATA_CAP)
                    .ifPresent(i->{
                        if (i.getDeviceMainBlockPos() != null) {
                            action.accept(i);
                        } else fallback.run();
                    });
            return;
        }
        fallback.run();
    }

    @Override
    public <T> T tryToDoIfInDeviceAndRet(@NotNull Entity player, Function<IPlayerDungeonData, T> action, Supplier<T> fallback) {
        if (checkIsPlayer(player) && player.isPassenger() && player.getVehicle() instanceof SeatEntity) {
            AtomicReference<T> ret = new AtomicReference<>();
            player.getCapability(EroticDungeonGameApi.PLAYER_DUNGEON_DATA_CAP)
                    .ifPresent(i->{
                        if (i.getDeviceMainBlockPos() != null) {
                            ret.set(action.apply(i));
                        } else ret.set(fallback.get());
                    });
            return ret.get();
        }
        return fallback.get();
    }

    @Override
    public IEventBus getModEventBus() {
        return EroticDungeon.EVENT_BUS;
    }

}
