import jQuery from "jquery";

export default class ToolMenu {
    constructor(tool) {
        this.tool = tool;
        this.menuId = this.tool.getKey() + "-menu";
        this.warnings = false;
        this.values = {};
    }

    show() {
        this.getMenu().removeClass("hidden");
    }

    hide() {
        this.getMenu().addClass("hidden");
    }

    beforeGet(key) {
        if (this.warnings) {
            console.warn('Method "beforeGet" not implemented.');
        }
    }

    afterSet(key, value) {
        if (this.warnings) {
            console.warn('Method "afterSet" not implemented.');
        }
    }

    value(key, newValue) {
        // If key not defined
        // (be it by giving no arguments, or giving it an undefined value)
        // Return all values as dict.
        if (key === undefined) {
            return this.values;
        }

        // Is key valid?
        // If not, create undefined entry
        if (key in this.values == false) {
            this.values[key] = undefined;
        }

        // If value not defined, returned specified value.
        if (newValue === undefined) {
            newValue = this.beforeGet(key);

            if (newValue !== undefined) {
                this.values[key] = newValue;
            }

            return this.values[key];
        }
        // If bot defined, store specified value.
        else {
            this.values[key] = newValue;
            newValue = this.afterSet(key, newValue);
        }
    }

    getChildren(selector) {
        return this.getMenu().children(selector);
    }

    getMenu() {
        return jQuery("#tool-menu #" + this.menuId);
    }
}