package pp.battleship.client.gui;

import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.control.AbstractControl;
import pp.battleship.model.Shell;
import pp.util.Position;

/**
 * Controls the 2D representation of a {@code Shell} in the game, updating its position
 * based on the shell's current state in the game model. The {@code Shell2DControl} class
 * is responsible for translating the shell's 3D position to a 2D view position within
 * the game's map view.
 */
public class Shell2DControl extends AbstractControl {
    private final Shell shell;
    private final MapView view;

    /**
     * Constructs a new {@code Shell2DControl} to manage the 2D visualization of the given {@code Shell}.
     *
     * @param view  The {@code MapView} used to get information about the map to display.
     * @param shell The {@code Shell} being visualized.
     */
    public Shell2DControl(MapView view, Shell shell){
        this.shell = shell;
        this.view = view;
    }

    /**
     * Updates the position of the shell's 2D representation based on the shell's current
     * 3D position in the game model. The position is mapped from model space to view space
     * coordinates and translated to the appropriate location within the {@code MapView}.
     *
     * @param tpf Time per frame, representing the time elapsed since the last frame.
     */
    @Override
    protected void controlUpdate(float tpf) {
        Vector3f shellPos = shell.getPosition();
        Position viewPos = view.modelToView(shellPos.x, shellPos.z);
        spatial.setLocalTranslation(viewPos.getX() + MapView.FIELD_SIZE / 2, viewPos.getY() + MapView.FIELD_SIZE / 2, 0);
    }

    /**
     * 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-specific behavior required for this control
    }
}
