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

import com.github.tartaricacid.touhoulittlemaid.api.bauble.IMaidBauble;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.eroticdungeongame.content.item.FilterItem;

import java.util.Optional;
import java.util.Set;

public class DungeonFilterBauble implements IMaidBauble {
    public FilterItem.FilterMode filterMode = FilterItem.FilterMode.WHITELIST;
    public Set<FilterItem.GameMode> gameModes = Set.of();
    public Set<String> tags = Set.of();
    public BlockPos workPos = null;
    public Set<String> teams = Set.of();
    public Set<String> whitelist = Set.of(), blacklist = Set.of();
    @Override
    public void onTick(EntityMaid maid, ItemStack baubleItem) {
       if (maid.tickCount % 150 == 0) {
           if(baubleItem.getItem() instanceof FilterItem) {
              filterMode = FilterItem.getFilterMode(baubleItem);
              gameModes = FilterItem.getGameModes(baubleItem);
              tags = FilterItem.getTags(baubleItem);
              workPos = FilterItem.getWorkPosition(baubleItem);
              teams = FilterItem.getTeams(baubleItem);
              whitelist = FilterItem.getWhitelistPlayers(baubleItem);
              blacklist = FilterItem.getBlacklistPlayers(baubleItem);
           }
       }
    }

    @Nullable
    public static DungeonFilterBauble getFirstFilterBauble(@NotNull EntityMaid maid) {
        int slots = maid.getMaidBauble().getSlots();
        for (int slot = 0; slot < slots; slot++) {
            IMaidBauble baubleInSlot = maid.getMaidBauble().getBaubleInSlot(slot);
            if (baubleInSlot instanceof DungeonFilterBauble bauble) {
                return bauble;
            }
        }
        return null;
    }

    public static boolean hasVaildFilterBauble(@NotNull EntityMaid maid) {
        DungeonFilterBauble firstFilterBauble = getFirstFilterBauble(maid);
        return (firstFilterBauble != null ? firstFilterBauble.workPos : null) != null;
    }

    public static Optional<DungeonFilterBauble> getFirstFilterBaubleOpt(@NotNull EntityMaid maid) {
        int slots = maid.getMaidBauble().getSlots();
        for (int slot = 0; slot < slots; slot++) {
            IMaidBauble baubleInSlot = maid.getMaidBauble().getBaubleInSlot(slot);
            if (baubleInSlot instanceof DungeonFilterBauble bauble) {
                return Optional.of(bauble);
            }
        }
        return Optional.empty();
    }

    /**
     * 获取工作点的距离
     */
    public double getDistanceToWorkPos(EntityMaid maid) {
        if (workPos == null) {
            return -1;
        }
        return Math.sqrt(maid.distanceToSqr(workPos.getX(), workPos.getY(), workPos.getZ()));
    }


    /**
     * 当找到玩家时调用
     */
    public void onPlayerFound(EntityMaid maid, Player player) {
        // 饰品发现玩家时的逻辑

    }

    /**
     * 当玩家在附近时周期性调用
     */
    public void onPlayerNearby(EntityMaid maid, Player player) {
        // 玩家在附近的持续效果

    }

    /**
     * 当失去玩家目标时调用
     */
    public void onPlayerLost(EntityMaid maid) {
        // 玩家离开时的清理逻辑
    }
}
