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

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import top.r3944realms.eroticdungeongame.api.workspace.Services;
import top.r3944realms.eroticdungeongame.client.util.RendererUtil;

import java.util.Objects;

@Mixin(Camera.class)
public class MixinCamera {
        @Shadow
        private BlockGetter level;

        @Shadow
        private net.minecraft.world.phys.Vec3 position;

        @WrapOperation(
                method = "getMaxZoom",
                at = @At(
                        value = "INVOKE",
                        target = "Lnet/minecraft/world/level/BlockGetter;clip(Lnet/minecraft/world/level/ClipContext;)Lnet/minecraft/world/phys/BlockHitResult;"
                )
        )
        private BlockHitResult getMaxZoomFix(BlockGetter instance, ClipContext context, Operation<BlockHitResult> original) {
           return Services.WORK_SPACE.tryToDoIfInDeviceAndRet(
                   Minecraft.getInstance().player,
                   data -> {
                       if (Minecraft.getInstance().cameraEntity != null && Objects.equals(Minecraft.getInstance().cameraEntity, Minecraft.getInstance().player) && RendererUtil.enabled) {
                           // 先执行原始的clip方法获取碰撞结果
                           BlockHitResult hitResult = original.call(instance, context);

                           // 如果碰撞到了方块
                           if (hitResult.getType() == HitResult.Type.BLOCK) {
                               BlockPos blockPos = hitResult.getBlockPos();
                               BlockState blockState = this.level.getBlockState(blockPos);

                               // 检查是否是想要忽略的方块
                               if (RendererUtil.shouldIgnoreBlock(blockState)) {
                                   // 返回一个MISS类型的BlockHitResult，表示没有碰撞
                                   // 这样相机就不会因为这个方块而缩放
                                   return BlockHitResult.miss(
                                           this.position,
                                           hitResult.getDirection(),
                                           blockPos
                                   );
                               }
                           }

                           // 如果不是忽略的方块，返回原始的碰撞结果
                           return hitResult;
                       } else return original.call(instance, context);
                   },
                   () -> original.call(instance, context)
           );
        }


}
