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

import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.eroticdungeongame.content.block.ISeatBlock;
import top.r3944realms.eroticdungeongame.content.block.blockentity.BaseSeatBlockEntity;
import top.r3944realms.eroticdungeongame.content.block.multiply.IHasPrimaryPart;
import top.r3944realms.eroticdungeongame.core.register.EDGSoundEvents;

import java.util.List;

public class MasterDeviceKeyItem extends DeviceKeyItem {
    public MasterDeviceKeyItem(Properties properties) {
        super(properties);
    }
    @Override
    protected @Nullable InteractionResult keyBindLogic(@NotNull BlockState state, BlockPos pos, Level level, ItemStack itemInHand, Player player) {
        if (state.getBlock() instanceof ISeatBlock seatBlock) {
            if (seatBlock instanceof IHasPrimaryPart<?> part) {
                pos = part.getPrimaryPartPos(state, pos);
            }
            if (level.getBlockEntity(pos) instanceof BaseSeatBlockEntity seat) {
                CompoundTag tag = itemInHand.getOrCreateTag();
                if (seat.hasLockCode()) {
                    seat.setMasterLocked(false);
                    seat.setLockCode(-1);
                    seat.setChanged();
                    player.displayClientMessage(Component.translatable("eroticdungeongame.message.key.success.unlock").withStyle(ChatFormatting.GREEN) ,true);
                    level.playSound( null, pos, EDGSoundEvents.UNLOCKING.get(), SoundSource.BLOCKS);
                } else {
                    seat.setMasterLocked(true);
                    seat.setChanged();
                    player.displayClientMessage(Component.translatable("eroticdungeongame.message.key.success.locked").withStyle(ChatFormatting.GREEN) ,true);
                    level.playSound(null, pos, EDGSoundEvents.LOCKING.get(), SoundSource.BLOCKS);
                }
                return InteractionResult.SUCCESS;
            }
        }
        return null;
    }
    @Override
    public void appendHoverText(
            @NotNull ItemStack pStack,
            @Nullable Level pLevel,
            @NotNull List<Component> pTooltipComponents,
            @NotNull TooltipFlag pIsAdvanced
    ) {
        pTooltipComponents.add(
                Component.translatable("message.eroticdungeongame.master_dungeon_key_01").withStyle(ChatFormatting.RED)
        );
        pTooltipComponents.add(
                Component.translatable("message.eroticdungeongame.master_dungeon_key_02").withStyle(ChatFormatting.GREEN)
        );
    }
}
