
const TEST_DIR = 'c:/userdata/recipe';
const NAME_FILTER = '*.txt';

class CheckToolsName {
    static init() {
        window.sigApiReadyPromise.then(() => {
            const codemodule = new CheckToolsName();
        });
    }

    constructor() {
        this._registerEvents();
    }

    /**
     * @private
     * register to user defined events
     */
    _registerEvents() {

        sigApi.eventMediator.subscribe(
            sigApi.events.getUserDefinedInternalEvent('CheckToolsName'),
            () => {
                sigApi.datapointManager.readDataPoint('10:ToolsName1.Tools_Name_Unsaved', 0)
                    .then((resolve) => {
                        if (resolve.value.replace(/ /g, "") === "")
                        {
                            sigApi.datapointManager.writeDataPoint('10:ToolsName1.NameEmpty', 1);

                            window.sigApi.windowManager.openWindowByWindowIndex(44);
                        }
                        else {
                            sigApi.datapointManager.writeDataPoint('10:ToolsName1.NameEmpty', 0);
                            this._onReadDir(resolve.value);
                        }
                    });
            }
        );
    }

    /**
     * @private
     * log the result of sigApi.fileManager.readDir
     */
    _onReadDir(newname) {
        newname = newname.toLowerCase();
        console.log('[DemoApiFileManager] _onReadDir()');
        const attributeIncludeFilter = 0;
        const attributeExcludeFilter = 0;
        let receps = [];
        sigApi.fileManager.readDir(TEST_DIR, NAME_FILTER, attributeIncludeFilter, attributeExcludeFilter).then((res) => {
            console.log(`[DemoApiFileManager] sigApi.fileManager.readDir('${TEST_DIR}', '${NAME_FILTER}', ${attributeIncludeFilter}, ${attributeExcludeFilter}) uid: ${res.uid}`);
            console.log(`[DemoApiFileManager]     parent: ${res.parent.dpne}`);
            for (let ii = 0, len = res.parent.nodes.length; ii < len; ii += 1) {
                const node = res.parent.nodes[ii];
                console.log(`[DemoApiFileManager]         nodes[${ii}]:`);
                console.log(`[DemoApiFileManager]             dpne: ${node.dpne}`);
                receps.push(node.dpne);
                console.log(`[DemoApiFileManager]             size: ${node.size}`);
                console.log(`[DemoApiFileManager]             attribute: ${node.attribute}`);
            }
            console.log(`[DemoApiFileManager]             NameExist: ${receps.includes(newname)}`);
            if (receps.includes(newname) === true)
            {
                sigApi.datapointManager.writeDataPoint('10:ToolsName1.NameAlreadyExists', 1);

                window.sigApi.windowManager.openWindowByWindowIndex(38);

            }
            else
            {
                sigApi.datapointManager.writeDataPoint('10:ToolsName1.NameAlreadyExists', 0);
                sigApi.datapointManager.writeDataPoint('10:SelectedAssistant1.Assistant15', 1);
                window.sigApi.appRouter.navigate('assistant15');
            }
        }).catch((error) => {
            console.log(`[DemoApiFileManager] error in sigApi.fileManager.readDir('${TEST_DIR}', '${NAME_FILTER}', ${attributeIncludeFilter}, ${attributeExcludeFilter}) uid: ${error.uid} status: ${error.status} errortxt: ${error.errortxt}`);
        });
    }

}

/**
 * Create the codemodule instance
 */
CheckToolsName.init();
