package pp.battleship.client.gui;

import com.jme3.scene.control.AbstractControl;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;

/**
 * Control that handles the sinking effect for destroyed ships.
 * It will gradually move the ship downwards and then remove it from the scene.
 */
class SinkingControl extends AbstractControl {
    private static final float SINK_DURATION = 5f;  // Duration of the sinking animation
    private static final float SINK_SPEED = 0.1f;   // Speed at which the ship sinks
    private float elapsedTime = 0;

    private final Node shipNode;

    /**
     * Constructs a {@code SinkingControl} object with the shipNode to be to be sunk
     * @param shipNode the node to handeld
     */
    public SinkingControl(Node shipNode) {
        this.shipNode = shipNode;
    }

    /**
     * Updated the Map to sink the ship
     * 
     * @param tpf time per frame
     */
    @Override
    protected void controlUpdate(float tpf) {
        // Update the sinking effect
        elapsedTime += tpf;

        // Move the ship down over time
        Vector3f currentPos = shipNode.getLocalTranslation();
        shipNode.setLocalTranslation(currentPos.x, currentPos.y - SINK_SPEED * tpf, currentPos.z);

        // Check if sinking duration has passed
        if (elapsedTime >= SINK_DURATION) {
            // Remove the ship from the scene
            shipNode.removeFromParent();
        }
    }

    /**
     * This method is called during the rendering phase, but it does not perform any
     * operations in this implementation as the control only influences the spatial's
     * transformation, not its rendering process.
     *
     * @param rm the RenderManager rendering the controlled Spatial (not null)
     * @param vp the ViewPort being rendered (not null)
     */
    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
        // No rendering-related code needed
    }
}
