/*
 *  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.compat.maid.extra;

import com.github.tartaricacid.touhoulittlemaid.entity.chatbubble.IChatBubbleData;
import com.github.tartaricacid.touhoulittlemaid.entity.chatbubble.implement.TextChatBubbleData;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

public class ChatBabbles {
    public static final Map<String, List<ChatBabble>> babbles = Maps.newHashMap(
            Map.of(
                    "idle", Lists.newArrayList(),
                    "find", Lists.newArrayList(),
                    "toward", Lists.newArrayList(),
                    "capture", Lists.newArrayList(),
                    "no_vaild_device", Lists.newArrayList(),
                    "bind", Lists.newArrayList()
            )
    );
    public record ChatBabble(String key, String type) {
        public static ChatBabble EMPTY = new ChatBabble("message.eroticdungeongame.empty", "common");
    }
    static {
        chatCommonBabble("capture","message.eroticdungeongame.capture.1");
        chatCommonBabble("capture","message.eroticdungeongame.capture.2");
        chatCommonBabble("capture","message.eroticdungeongame.capture.3");
        chatMasterBabble("capture","message.eroticdungeongame.capture.master.1");
        chatCommonBabble("find","message.eroticdungeongame.find.1");

        chatCommonBabble("toward","message.eroticdungeongame.toward.1");
        chatCommonBabble("no_vaild_device","message.eroticdungeongame.device_not_found.1");

        chatCommonBabble("bind","message.eroticdungeongame.bind.1");
        chatCommonBabble("idle", "message.eroticdungeongame.idle.1");
        chatMasterBabble("idle", "message.eroticdungeongame.idle.master.1");
        chatMasterBabble("idle", "message.eroticdungeongame.idle.master.2");
    }
    public static void chatMasterBabble(String type, String message) {
        babbles.computeIfPresent(type, (s, chatBabbles) -> {
            ChatBabble master = new ChatBabble(message, "master");
            chatBabbles.add(master);
            return chatBabbles;
        });
    }

    public static void chatCommonBabble(String type, String message) {
        babbles.computeIfPresent(type, (s, chatBabbles) -> {
            ChatBabble master = new ChatBabble(message, "common");
            chatBabbles.add(master);
            return chatBabbles;
        });
    }

    public static ChatBabble getRandomBabble(String type, @NotNull EntityMaid maid) {
        List<ChatBabble> chatBabbles = babbles.get(type);
        return chatBabbles == null || chatBabbles.isEmpty() ? ChatBabble.EMPTY : chatBabbles.get(maid.getRandom().nextInt(chatBabbles.size())) ;
    }

    public static @NotNull Component getRandomBabbleComponent(String type, @NotNull EntityMaid maid) {
        Component component;
        Component ownerName = (maid.getOwner() != null)
                ? maid.getOwner().getName()
                : Component.literal("Master");
        ChatBabble randomBabble = getRandomBabble(type, maid);
        if (randomBabble.type.equals("master")) {
            component = Component.translatable(randomBabble.key, ownerName);
        } else {
            component = Component.translatable(randomBabble.key);
        }
        return component;
    }
    public static TextChatBubbleData createTextChatBubbleData(int existTick, Component text) {
        return TextChatBubbleData.create(existTick, text, TextChatBubbleData.TYPE_2, IChatBubbleData.DEFAULT_PRIORITY);
    }
}
