/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2026 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.command.commands.module.teleport

import net.ccbluex.liquidbounce.features.command.Command
import net.ccbluex.liquidbounce.features.command.builder.CommandBuilder
import net.ccbluex.liquidbounce.features.command.builder.ParameterBuilder
import net.ccbluex.liquidbounce.features.module.modules.movement.ModuleTeleport
import net.ccbluex.liquidbounce.lang.translation
import net.ccbluex.liquidbounce.utils.block.canStandOn
import net.ccbluex.liquidbounce.utils.block.collisionShape
import net.ccbluex.liquidbounce.utils.client.chat
import net.ccbluex.liquidbounce.utils.client.markAsError
import net.ccbluex.liquidbounce.utils.client.player
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.world.phys.AABB
import net.minecraft.world.phys.shapes.BooleanOp
import net.minecraft.world.phys.shapes.Shapes
import net.minecraft.world.phys.shapes.VoxelShape
import kotlin.math.abs

/**
 * VClip Command
 *
 * Allows you to clip through blocks.
 *
 * Module: [ModuleTeleport]
 */
object CommandVClip : Command.Factory {

    override fun createCommand(): Command {
        return CommandBuilder
            .begin("vclip")
            .requiresIngame()
            .hub()
            .subcommand(
                CommandBuilder.begin("by")
                    .parameter(
                        ParameterBuilder
                            .begin<Float>("distance")
                            .required()
                            .verifiedBy(ParameterBuilder.FLOAT_VALIDATOR)
                            .build()
                    )
                    .handler {
                        val dy = args[0] as Float

                        ModuleTeleport.indicateTeleport(getX(), getY() + dy, getZ())
                    }
                    .build()
            )
            .subcommand(
                CommandBuilder.begin("smart")
                    .hub()
                    .subcommand(buildAutomaticCommand(Direction.UP, "up"))
                    .subcommand(buildAutomaticCommand(Direction.DOWN, "down"))
                    .build()
            )
            .build()
    }

    private fun buildAutomaticCommand(direction: Direction, name: String): Command {
        return CommandBuilder.begin(name)
            .parameter(
                ParameterBuilder
                    .begin<Int>("max")
                    .optional()
                    .verifiedBy(ParameterBuilder.INTEGER_VALIDATOR)
                    .build()
            )
            .handler {
                performAutomaticClip(args, command, direction)
            }
            .build()
    }

    private fun performAutomaticClip(args: Array<out Any>, command: Command, direction: Direction) {
        val max = if (args.isNotEmpty()) {
            abs(args[0] as Int)
        } else {
            10
        }

        val blockPos = player.vehicle?.blockPosition() ?: player.blockPosition()
        val pos = player.vehicle?.position() ?: player.position()

        var newPos = blockPos

        // avoid clipping on the block we're already on
        if (direction == Direction.DOWN) {
            newPos = newPos.below()
        }

        for (x in 0 until max) {
            // go to the next position in the direction
            newPos = newPos.relative(direction)

            val shape = newPos.collisionShape

            // we have to be able to stand on the position
            if (canTpOn(newPos, shape)) {

                // allows clipping on fences, etc.
                val vOffset = shape.max(Direction.Axis.Y)

                val dy = (newPos.y + vOffset) - pos.y

                // check if the found position is too far away
                if (abs(dy) > max) {
                    break
                }

                // teleport
                ModuleTeleport.indicateTeleport(getX(), getY() + dy, getZ())
                return
            }
        }

        chat(markAsError(translation("liquidbounce.command.vclip.result.noPositionFound")), command)
    }

    private fun canTpOn(pos: BlockPos, posCollisionShape: VoxelShape): Boolean {
        // check if there is enough space at the new position
        val boundingBox = player.vehicle?.boundingBox ?: player.boundingBox

        if (isNotEnoughSpaceAboveBlock(pos, boundingBox, posCollisionShape)) {
            return false
        }

        player.vehicle?.let {
            if (isNotEnoughSpaceAboveBlock(pos, player.boundingBox, posCollisionShape)) {
                return false
            }
        }

        // a simple case, we can stand on the position
        if (pos.canStandOn()) {
            return true
        }

        // even tho canStandOn returns false the block might not be full on the upper side, but we can stand on it tho
        val shape = posCollisionShape.move(pos.x.toDouble(), pos.y.toDouble(), pos.z.toDouble())
        val dy = shape.min(Direction.Axis.Y) - boundingBox.min(Direction.Axis.Y)
        return Shapes.joinIsNotEmpty(
            shape,
            Shapes.create(boundingBox.move(0.0, dy, 0.0)),
            BooleanOp.AND
        )
    }

    private fun isNotEnoughSpaceAboveBlock(pos: BlockPos, boundingBox: AABB, posCollisionShape: VoxelShape): Boolean {
        val requiredHeight = boundingBox.maxY - boundingBox.minY - (1.0 - posCollisionShape.max(Direction.Axis.Y))
        var accumulatedHeight = 0.0
        var newPos = pos

        while (accumulatedHeight < requiredHeight) {
            newPos = newPos.above()
            val collisionShape = newPos.collisionShape

            if (!collisionShape.isEmpty) {
                val maxAvailableHeight = collisionShape.min(Direction.Axis.Y)
                if (maxAvailableHeight < requiredHeight - accumulatedHeight) {
                    return true
                }
            }

            accumulatedHeight += 1.0
        }

        return false
    }

    private fun getX() = player.vehicle?.x ?: player.x

    private fun getY() = player.vehicle?.y ?: player.y

    private fun getZ() = player.vehicle?.z ?: player.z

}
