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 static pp.util.FloatMath.PI;

/**
 * Controls the 3D representation of a {@code Shell} in the game, updating its position
 * and rotation based on the shell's current state in the game model. The {@code ShellControl}
 * class ensures that the spatial associated with the shell is positioned and oriented correctly
 * within the world.
 */
public class ShellControl extends AbstractControl {
    private final Shell shell;

    /**
     * Constructs a new {@code ShellControl} to manage the 3D visualization of the given {@code Shell}.
     *
     * @param shell The {@code Shell} being visualized and controlled.
     */
    public ShellControl(Shell shell){
        super();
        this.shell = shell;
    }

    /**
     * Updates the 3D position and rotation of the shell based on its current state.
     * Converts map coordinates to world coordinates and applies the shell's orientation.
     *
     * @param tpf Time per frame, representing the elapsed time since the last update.
     */
    @Override
    protected void controlUpdate(float tpf) {
        Vector3f pos = shell.getPosition();
        Vector3f fixed = new Vector3f(pos.z + 0.5f, pos.y, pos.x + 0.5f);
        fixed.setY(pos.y);
        spatial.setLocalTranslation(fixed);
        spatial.setLocalRotation(shell.getRotation());
        spatial.rotate(PI/2,0,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
    }
}
