import { SimpleStateMachineDescription } from "../../../core/fsm/simple/SimpleStateMachineDescription.js";
import { SimpleStateMachine } from "../../../core/fsm/simple/SimpleStateMachine.js";
import Vector3 from "../../../core/geom/Vector3.js";
import { SurfacePoint3 } from "../../../core/geom/3d/SurfacePoint3.js";

const smd = new SimpleStateMachineDescription();

export const STATE_INITIAL = smd.createState();
export const STATE_HOVER = smd.createState();
export const STATE_EDIT = smd.createState();

smd.createEdge(STATE_INITIAL, STATE_HOVER);
smd.createEdge(STATE_HOVER, STATE_EDIT);
smd.createEdge(STATE_HOVER, STATE_INITIAL);
smd.createEdge(STATE_EDIT, STATE_HOVER);


export class FaceEditor {
    constructor() {
        /**
         *
         * @type {ShadedGeometry|null}
         * @private
         */
        this.__mesh = null;

        /**
         *
         * @type {EntityComponentDataset|null}
         * @private
         */
        this.__ecd = null;

        /**
         *
         * @type {number}
         * @private
         */
        this.__entity = -1;

        /**
         *
         * @type {Vector3}
         * @private
         */
        this.__edit_anchor_point = new Vector3();
        /**
         *
         * @type {Vector3}
         * @private
         */
        this.__edit_target_point = new Vector3();

        /**
         *
         * @type {SurfacePoint3}
         * @private
         */
        this.__hover_contact = new SurfacePoint3();

        this.__sm = new SimpleStateMachine(smd);
        this.__sm.setState(STATE_INITIAL);

        this.__sm.addEventHandlerStateEntry(STATE_HOVER, () => {
            console.log('+hovering');
        });

        this.__sm.addEventHandlerStateExit(STATE_HOVER, () => {
            console.log('-hovering');
        });
    }

    get state() {
        return this.__sm.getState();
    }

    set state(v) {
        this.__sm.navigateTo(v);
    }

    /**
     * @param {ShadedGeometry} v
     */
    set mesh(v) {
        this.__mesh = v;
    }

    get mesh() {
        return this.__mesh;
    }

    /**
     * @param {EntityComponentDataset} v
     */
    set ecd(v) {
        this.__ecd = v;
    }

    /**
     * @param {number} v
     */
    set entity(v) {
        this.__entity = v;
    }

    /**
     *
     * @param {Vector3} v
     */
    set edit_anchor_point(v) {
        this.__edit_anchor_point.copy(v);
    }

    /**
     *
     * @param {Vector3} v
     */
    set edit_target_point(v) {
        this.__edit_target_point.copy(v);
    }

    set contact(v) {
        this.__hover_contact.copy(v);
    }

    get contact() {
        return this.__hover_contact;
    }

    attach() {
        this.__sm.navigateTo(STATE_INITIAL);
    }

    detach() {
        this.__sm.navigateTo(STATE_INITIAL);
    }
}
