'use strict';

oml.ui.list = function() {

    var ui = oml.user.ui,

        that = oml.ui[ui.listView + 'View']()
            .bindEvent({
                add: function(data) {
                    var keys = data.keys || '';
                    if (!index && (
                        keys != 'shift' || ui.listSelection
                    )) {
                        oml.addList(
                            Ox.contains(keys, 'alt'),
                            Ox.contains(keys, 'shift')
                        );
                    }
                },
                closepreview: function() {
                    oml.$ui.mainMenu.setItemTitle('preview', Ox._('Show Preview'));
                    oml.$ui.previewButton.options({value: false});
                    oml.$ui.previewDialog.close();
                    delete oml.$ui.previewDialog;
                },
                copy: function(data) {
                    oml.clipboard.copy(data.ids, 'book');
                },
                copyadd: function(data) {
                    oml.clipboard.copy(data.ids, 'book');
                },
                cut: function(data) {
                    var listData = oml.getListData();
                    if (listData.editable && listData.type == 'static') {
                        oml.clipboard.copy(data.ids, 'book');
                        oml.doHistory('cut', data.ids, ui._list, function() {
                            oml.UI.set({listSelection: []});
                            oml.reloadLists();
                        });
                    }
                },
                cutadd: function(data) {
                    var listData = oml.getListData();
                    if (listData.editable && listData.type == 'static') {
                        oml.clipboard.add(data.ids, 'book');
                        oml.doHistory('cut', data.ids, ui._list, function() {
                            oml.UI.set({listSelection: []});
                            oml.reloadLists();
                        });
                    }
                },
                'delete': function(data) {
                    var listData = oml.getListData();
                    if (listData.editable && listData.type == 'static') {
                        oml.doHistory('delete', data.ids, ui._list, function() {
                            oml.UI.set({listSelection: []});
                            oml.reloadLists();
                        });
                    }
                },
                init: function(data) {
                    if (ui.find.conditions.length == 0 || (
                        ui.find.conditions.length == 1
                        && ui.find.conditions[0].key == 'list'
                        && ui.find.conditions[0].operator == '=='
                    )) {
                        oml.$ui.folders.updateItems(data.items);
                    }
                    oml.$ui.statusbar.set('total', data);
                },
                key_control_d: function(event) {
                    var items = ui.listSelection.filter(function(id) {
                        return oml.$ui.list.value(id, 'mediastate') == 'unavailable';
                    });
                    if (items.length) {
                        oml.api.addListItems({
                            items: items,
                            list: ':'
                        }, function(result) {
                            Ox.Request.clearCache();
                            // FIXME: reload?
                        });
                    }
                    event.preventDefault();
                },
                key_control_delete: function() {
                    if (that.options('selected').filter(function(id) {
                        return that.value(id, 'mediastate') == 'available';
                    }).length) {
                        oml.ui.deleteItemsDialog().open();
                    }
                },
                key_shift_enter: function() {
                    var selected = that.options('selected');
                    if (selected.length) {
                        oml.UI.set({
                            item: selected[0],
                            itemView: 'book'
                        });
                    }
                },
                load: function() {
                    if (oml._updateEditMenu) {
                        oml.$ui.mainMenu.updateElement('editMenu');
                        oml._udpateEditMenu = false;
                    }
                },
                open: function(data) {
                    oml.UI.set({
                        item: data.ids[0],
                        itemView: 'info'
                    });
                },
                openpreview: function() {
                    if (!oml.$ui.previewDialog) {
                        oml.$ui.mainMenu.setItemTitle('preview', Ox._('Hide Preview'));
                        oml.$ui.previewButton.options({value: true});
                        oml.$ui.previewDialog = oml.ui.previewDialog()
                            .open()
                            .bindEvent({
                                close: function() {
                                    that.closePreview();
                                    delete oml.$ui.previewDialog;
                                }
                            });
                    } else {
                        oml.$ui.previewDialog.updateElement();
                    }
                },
                paste: function(data) {
                    var items = oml.clipboard.paste();
                    if (
                        items.length
                        && oml.clipboard.type() == 'book'
                        && oml.getListData().editable
                    ) {
                        oml.doHistory('paste', items, ui._list, function() {
                            oml.UI.set({listSelection: items});
                            oml.reloadLists();
                        });
                    }
                },
                select: function(data) {
                    oml.UI.set({listSelection: data.ids});
                },
                oml_find: function() {
                    that.reloadList();
                },
                oml_item: function() {
                    if (!ui.item) {
                        that.gainFocus();
                    } else {
                        that.options({selected: [ui.item]});
                    }
                },
                oml_listselection: function(data) {
                    that.options({selected: data.value});
                },
                oml_listsort: function(data) {
                    that.options({sort: data.value});
                },
                oml_sidebarsize: function(data) {
                    that.size();
                }
            });

    oml.enableDragAndDrop(that);

    that.updateElement = function() {
        Ox.Request.clearCache('find');
        that.reloadList(true);
    };

    return that;

};