/*
 *  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.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
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;
import top.r3944realms.eroticdungeongame.util.NorpLogger;

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;
                        long currentTime = System.currentTimeMillis();

                        switch (data.getNorpState()) {
                            case IPlayerDungeonData.IDLE -> {
                                long cooldownSeconds = EDGConfig.COMMON.NORP_SUCCESSFUL_COOLDOWN.get();
                                flag = currentTime - data.getNorpTime() >= cooldownSeconds * 1000;

                                if (flag) {
                                    data.setNorpState(IPlayerDungeonData.REQUEST);
                                    data.setNorpTime(currentTime);
                                    player.displayClientMessage(
                                            Component.translatable("eroticdungeongame.message.norp.request",
                                                    EDGConfig.COMMON.NORP_INTERVAL_TIME.get()),
                                            true
                                    );
                                    NorpLogger.logNorpEvent(player.getName().getString(), "wanna to stop RP", "waiting");
                                    player.level().getEntitiesOfClass(Player.class, AABB.ofSize(player.getOnPos().getCenter(), 10, 10, 10))
                                            .forEach(item -> item.displayClientMessage(Component.translatable("eroticdungeongame.message.norp.warning", player.getDisplayName()).withStyle(ChatFormatting.YELLOW), false));
                                    // 提醒玩家下次生效
                                    player.displayClientMessage(
                                            Component.translatable("eroticdungeongame.message.norp.next",
                                                    EDGConfig.COMMON.NORP_INTERVAL_TIME.get()),
                                            false
                                    );
                                } else {
                                    // 还在冷却中
                                    long remainingSeconds = (cooldownSeconds * 1000 - (currentTime - data.getNorpTime())) / 1000;
                                    player.displayClientMessage(
                                            Component.translatable("eroticdungeongame.message.norp.cooldown", remainingSeconds),
                                            true
                                    );
                                }
                            }
                            case IPlayerDungeonData.REQUEST -> {
                                long intervalSeconds = EDGConfig.COMMON.NORP_INTERVAL_TIME.get();
                                flag = currentTime - data.getNorpTime() >= intervalSeconds * 1000;

                                if (flag) {
                                    data.setNorpState(IPlayerDungeonData.IDLE);
                                    data.setNorpTime(currentTime);
                                    NorpLogger.logNorpSuccess(player);
                                    ((IEDGEntity) player).releaseEntityFromEDGDevice(true);
                                    // 提醒玩家成功
                                    player.displayClientMessage(
                                            Component.translatable("eroticdungeongame.message.norp.success"),
                                            true
                                    );
                                } else {
                                    // 提醒玩家等待间隔
                                    long remainingSeconds = (intervalSeconds * 1000 - (currentTime - data.getNorpTime())) / 1000;
                                    player.displayClientMessage(
                                            Component.translatable("eroticdungeongame.message.norp.wait", remainingSeconds),
                                            true
                                    );
                                }
                            }
                            default -> {}
                        }
                        return flag;
                    }).orElse(false);
        else return false;
    }
}
