package optics.view;

import basics.math.algebra.Matrix;
import basics.math.algebra.Vector;
import renderer.Renderer;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

public class PlayerController {

    private Vector translation;
    private Matrix rotation;

    private int cursor;

    private boolean changed;

    public PlayerController() {
        this.translation = new Vector(0,0,0);

        this.rotation = new Matrix();
        this.rotation.toIdentity();
    }

    public void motionKeyControl(KeyEvent e) {
        this.rotation.toIdentity();
        this.translation = new Vector(0,0,0);

        switch (e.getKeyChar()) {
            case 'w':
                this.translation = new Vector(0,0,0.2);
                this.changed = true;
                break;
            case 's':
                this.translation = new Vector(0,0,-0.2);
                this.changed = true;
                break;
            case 'a':
                this.translation = new Vector(0.2,0, 0);
                this.changed = true;
                break;
            case 'd':
                this.translation = new Vector(-0.2, 0,0);
                this.changed = true;
                break;
            case 'e':
                this.translation = new Vector(0, -0.2,0);
                this.changed = true;
                break;
            case 'q':
                this.translation = new Vector(0, 0.2,0);
                this.changed = true;
                break;
        }
    }

    public boolean changed() {
        boolean tmp = changed;
        this.changed = false;
        return tmp;
    }

    public void apply(Renderer renderer) {
        renderer.getCamera().transform(this.translation, this.rotation);
    }

    public void motionMouseControl(MouseEvent e) {
        this.translation = new Vector(0,0,0);

        double delta = (this.cursor - e.getX()) * 1e-2;

        this.cursor = e.getX();
        this.rotation = Matrix.rotationY(delta);
        this.changed = true;
    }

    public void mouseReleased() {
        this.cursor = 0;
    }
}
