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

import net.minecraft.world.entity.player.Player;
import top.r3944realms.eroticdungeongame.api.capability.IPlayerDungeonData;
import top.r3944realms.eroticdungeongame.api.workspace.Services;
import top.r3944realms.eroticdungeongame.config.EDGConfig;
import top.r3944realms.eroticdungeongame.core.capability.PlayerDungeonDataProvider;
import top.r3944realms.eroticdungeongame.util.IEDGEntity;

public class NorpService {
    public static boolean checkCanNorp(Player player) {
        return Services.WORK_SPACE.isInDevice(player);
    }
    public static boolean norp(Player player) {
        if (checkCanNorp(player))
            return player.getCapability(PlayerDungeonDataProvider.PLAYER_DUNGEON_DATA_CAP)
                .map(data -> {
                    boolean flag = false;
                    switch (data.getNorpState()) {
                        case IPlayerDungeonData.IDLE -> {
                            flag = System.currentTimeMillis() - data.getNorpTime() >= EDGConfig.COMMON.NORP_SUCCESSFUL_COOLDOWN.get() * 1000;
                            if (flag) {
                                data.setNorpState(data.REQUEST);
                                //todo 提醒玩家下次生效
                            } else {
                                // todo 还在冷却中
                            }
                        }
                        case IPlayerDungeonData.REQUEST -> {
                            flag = System.currentTimeMillis() - data.getNorpTime() >= EDGConfig.COMMON.NORP_INTERVAL_TIME.get() * 1000;
                            if (flag) {
                                data.setNorpState(data.IDLE);
                                ((IEDGEntity)player).releaseEntityFromEDGDevice(true);
                                //todo 提醒玩家成功
                            } else {
                                // todo 提醒玩家等待间隔
                            }
                        }
                        default -> {}
                    }
                    return flag;
                }).orElse(false);
        else return false;
    }
}
