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

import com.github.tartaricacid.touhoulittlemaid.api.task.IMaidTask;
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.github.tartaricacid.touhoulittlemaid.util.ItemsUtil;
import com.google.common.collect.Lists;
import com.mojang.datafixers.util.Pair;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.ai.behavior.BehaviorControl;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.items.wrapper.CombinedInvWrapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.eroticdungeongame.core.compat.maid.barin.FindPlayerNearWorkPos;
import top.r3944realms.eroticdungeongame.core.compat.maid.barin.LeadTargetPlayerToDevice;
import top.r3944realms.eroticdungeongame.core.compat.maid.bauble.DungeonFilterBauble;
import top.r3944realms.eroticdungeongame.core.register.EDGItems;
import top.r3944realms.superleadrope.core.register.SLPItems;

import java.util.List;
import java.util.function.Predicate;

public class BindPlayerToDeviceTask implements IMaidTask {

    private static final ResourceLocation UID = EroticDungeon.rl("bind_player_to_device");

    private static final ItemStack ICON = EDGItems.DEVICE_KEY.get().getDefaultInstance();

    @Override
    public @NotNull ResourceLocation getUid() {
        return UID;
    }

    @Override
    public @NotNull ItemStack getIcon() {
        return ICON;
    }

    @Override
    public boolean isEnable(@NotNull EntityMaid maid) {
        DungeonFilterBauble firstFilterBauble = DungeonFilterBauble.getFirstFilterBauble(maid);
        return firstFilterBauble != null && firstFilterBauble.workPos != null;
    }

    @Nullable
    @Override
    public SoundEvent getAmbientSound(@NotNull EntityMaid maid) {
        return null;
    }

    @Override
    public @NotNull List<Pair<Integer, BehaviorControl<? super EntityMaid>>> createBrainTasks(@NotNull EntityMaid maid) {
        FindPlayerNearWorkPos findPlayerNearWorkPos = new FindPlayerNearWorkPos();
        LeadTargetPlayerToDevice leadTargetPlayerToDevice = new LeadTargetPlayerToDevice();
        return Lists.newArrayList(Pair.of(0, findPlayerNearWorkPos), Pair.of(1, leadTargetPlayerToDevice));
    }

    @Override
    public boolean workPointTask(@NotNull EntityMaid maid) {
        return true;
    }

    @Override
    public boolean enableLookAndRandomWalk(@NotNull EntityMaid maid) {
        return false;
    }

    @Override
    public boolean enablePanic(@NotNull EntityMaid maid) {
        return false;
    }

    @Override
    public @NotNull List<Pair<String, Predicate<EntityMaid>>> getConditionDescription(@NotNull EntityMaid maid) {
        return Lists.newArrayList(
                Pair.of("no_vaild_filter", DungeonFilterBauble::hasVaildFilterBauble)
        );
    }

    @Override
    public @NotNull List<Pair<String, Predicate<EntityMaid>>> getEnableConditionDesc(@NotNull EntityMaid maid) {
        return Lists.newArrayList(
                Pair.of("need_a_super_lead", this::checkHasSuperLead)
        );
    }

    private boolean checkHasSuperLead(EntityMaid maid) {
        return !getSuperLeadItem(maid).isEmpty();
    }

    public static ItemStack getSuperLeadItem(EntityMaid entityMaid) {
        CombinedInvWrapper itemHandler = entityMaid.getAvailableInv(false);
        return ItemsUtil.getStack(itemHandler, stack -> stack.getItem() == SLPItems.SUPER_LEAD_ROPE.get());
    }



}
