// a simple example of adding custom nodes

requirejs('customNodesDefinitions');

// add custom node under 'root' by enhancing its getChildren() method
nodeHandlers.root.override({
    getChildren: function ($super, node) {
        return new Promise(function (resolve, reject) {
            $super(node).then(function () {
                node.addChild(null, 'myCustomNodesHandler');
                nodeHandlers.root.sortNodesAndVisibility(node);
                resolve();
            });
        });
    }
});

nodeHandlers.myCustomNodesBase = inheritNodeHandler('MyCustomNodesBase', 'Base', {    
    menu: function (node) {        
        let res = [];        
        res.push({
            action: {
                title: _('Configure custom nodes'),
                icon: 'options',
                execute: function () {                    
                    let list = app.getAddonList('all');
                    if (list) {
                        list.whenLoaded().then(() => {                            
                            list.forEach((item) => {
                                if (item.ext_id == 'customNodes') {                                    
                                    uitools.openDialog('dlgAddonConfig', {
                                        modal: true,
                                        addon: item
                                    }, (dlg) => {                                        
                                        nodeUtils.refreshNodeChildren(node);                                               
                                    });                                    
                                }
                            });                            
                        });
                    }
                }
            },
            order: 10,
            grouporder: 10
        });             
        return res;
    },
    hideDefaultMenu: true, // don't append the default popup-menu
});

nodeHandlers.myCustomNodesHandler = inheritNodeHandler('MyCustomNodesHandler', 'MyCustomNodesBase', {
    title: function (node) {
        return 'Custom Nodes';
    },
    icon: 'myCustomIcon',
    hasChildren: true,
    getChildren: function (node) {
        return new Promise(function (resolve, reject) {
            node.addChild(null, 'myTracksNodeHandler');
            node.addChild(null, 'myAlbumsNodeHandler');

            resolve();
        });
    },
    viewAs: ['nodeList'],
    formatStatus: function (data) {
        return statusbarFormatters.formatDefaultSimpleStatus(data, function (cnt) {
            return cnt + ' custom nodes';
        });
    }   
});

nodeHandlers.myTracksNodeHandler = inheritNodeHandler('MyTracksNodeHandler', 'MyCustomNodesBase', {
    title: function (node) {
        return 'Tracks';
    },
    icon: 'listview',
    hasChildren: true,
    getChildren: function (node) {
        return new Promise(function (resolve, reject) {
            let defs = getCustomNodeDefinitions();
            forEach(defs, (item) => {
                if (item.type != 'tracks') 
                    return; // only tracks

                let dataSource = {
                    id: item.name,
                    title: item.name,
                    getTracklist: function () {
                        return app.db.getTracklist( item.sql, -1);
                    }
                };
                node.addChild(dataSource, 'myTracklistSubNodeHandler');
            });
            resolve();
        });
    },
    viewAs: ['nodeList']
});

nodeHandlers.myTracklistSubNodeHandler = inheritNodeHandler('MyTracklistSubNodeHandler', 'MyCustomNodesBase', {
    title: function (node) {
        return node.dataSource.title;
    },
    icon: 'listview',
    hasChildren: false,
    viewAs: ['tracklist'],
    getViewDataSource: function (view) {
        if (view.viewNode.dataSource.getTracklist)
            return view.viewNode.dataSource.getTracklist();
        else
            return app.getAllTracksList(); // just to not crash when node definition id changed (or getting list not defined)
    }
});

nodeHandlers.myAlbumsNodeHandler = inheritNodeHandler('MyAlbumsNodeHandler', 'MyCustomNodesBase', {
    title: function (node) {
        return 'Albums';
    },
    icon: 'album',
    getChildren: function (node) {
        return new Promise(function (resolve, reject) {
            let defs = getCustomNodeDefinitions();
            forEach(defs, (item) => {
                if (item.type != 'albums') 
                    return; // only albums

                let dataSource = {
                    id: item.name,
                    title: item.name,                    
                    getAlbumList: function () {
                        return app.db.getAlbumList( item.sql, -1);
                    }
                };
                node.addChild(dataSource, 'myAlbumlistSubNodeHandler');
            });
            resolve();
        });
    },
    viewAs: ['nodeList']
});

nodeHandlers.myAlbumlistSubNodeHandler = inheritNodeHandler('MyAlbumlistSubNodeHandler', 'MyCustomNodesBase', {
    title: function (node) {
        return node.dataSource.title;
    },
    icon: 'album',
    hasChildren: false,
    viewAs: ['albumlist'],
    getViewDataSource: function (view) {
        if (view.viewNode.dataSource.getAlbumList)
            return view.viewNode.dataSource.getAlbumList();
        else
            return app.getAllAlbumsList(); // just to not crash when node definition id changed (or getting list not defined)
    }
});
