import Vector2 from "../../../core/geom/Vector2.js";
import { GridPosition2TransformFlags } from "./GridPosition2TransformFlags.js";


const DEFAULT_FLAGS = GridPosition2TransformFlags.Immediate;

export class GridPosition2Transform {
    /**
     *
     * @type {Vector2}
     */
    offset = new Vector2(0, 0);

    /**
     *
     * @type {GridPosition2TransformFlags|number}
     */
    flags = DEFAULT_FLAGS;

    /**
     *
     * @param {number|GridPosition2TransformFlags} flag
     * @returns {void}
     */
    setFlag(flag) {
        this.flags |= flag;
    }

    /**
     *
     * @param {number|GridPosition2TransformFlags} flag
     * @returns {void}
     */
    clearFlag(flag) {
        this.flags &= ~flag;
    }

    /**
     *
     * @param {number|GridPosition2TransformFlags} flag
     * @param {boolean} value
     */
    writeFlag(flag, value) {
        if (value) {
            this.setFlag(flag);
        } else {
            this.clearFlag(flag);
        }
    }

    /**
     *
     * @param {number|GridPosition2TransformFlags} flag
     * @returns {boolean}
     */
    getFlag(flag) {
        return (this.flags & flag) === flag;
    }

    toJSON() {
        return {
            offset: this.offset.toJSON()
        };
    }

    fromJSON(obj) {

        if (typeof obj.offset === "object") {
            this.offset.fromJSON(obj.offset);
        } else {
            this.offset.set(0, 0);
        }

    }

    /**
     *
     * @param {GridPosition2Transform} other
     */
    copy(other) {
        this.offset.copy(other.offset);

        return this;
    }

    /**
     *
     * @returns {GridPosition2Transform}
     */
    clone() {
        const clone = new GridPosition2Transform();

        clone.copy(this);

        return clone;
    }

    /**
     *
     * @param {GridPosition2Transform} other
     * @returns {boolean}
     */
    equals(other) {
        return this.offset.equals(other.offset);
    }

    /**
     * @returns {number}
     */
    hash() {
        return this.offset.hash();
    }

}

GridPosition2Transform.typeName = "GridPosition2Transform";
