// vim: et:ts=4:sw=4:sts=4:ft=javascript
'use strict';

pandora.ui.uploadFileDialog = function(file, callback) {

    var extension = file.name.split('.').pop().toLowerCase(),

        extensions = ['gif', 'jpg', 'jpeg', 'pdf', 'png'],

        filename = file.name.split('.').slice(0, -1).join('.') + '.'
            + (extension == 'jpeg' ? 'jpg' : extension),

        id = pandora.user.username + ':' + filename,

        upload,

        $errorDialog,

        $content = Ox.Element().css({margin: '16px'}),

        $text = $('<div>')
            .html('Uploading ' + file.name)
            .appendTo($content),

        $progress = Ox.Progressbar({
                width: 256,
                showPercent: true,
                showTime: true
            })
            .css({margin: '16px 0 16px 0'})
            .appendTo($content),

        $message = $('<div>')
            .appendTo($content),

        $uploadDialog = Ox.Dialog({
                buttons: [
                    Ox.Button({
                        id: 'close',
                        title: 'Cancel Upload'
                    }).bindEvent({
                        click: function() {
                            var title = this.options('title');
                            $uploadDialog.close();
                            if (title == 'Cancel Upload') {
                                upload.abort();
                            } else if (title == 'Done') {
                                callback({
                                    id: id
                                });
                            }
                        }
                    })
                ],
                content: $content,
                height: 112,
                keys: {escape: 'close'},
                width: 288,
                title: 'Upload File'
            })
            .bindEvent({
                open: function() {
                    upload = pandora.chunkupload({
                            data: {
                                filename: filename
                            },
                            file: file,
                            url: '/api/upload/file/',
                        })
                        .bindEvent({
                            done: function(data) {
                                if (data.progress == 1) {
                                    $uploadDialog.options('buttons')[0].options({title: 'Done'});
                                    Ox.print('SUCCEEDED');
                                } else {
                                    $message.html('Upload failed.')
                                    $uploadDialog.options('buttons')[0].options({title: 'Close'});
                                    Ox.print('FAILED');
                                }
                            },
                            progress: function(data) {
                                $progress.options({progress: data.progress || 0});
                            }
                        });
                }
            });

    if (!Ox.contains(extensions, extension)) {
        return errorDialog('Supported file types are GIF, JPG, PNG and PDF.');
    } else {
        Ox.oshash(file, function(oshash) {
            pandora.api.findFiles({
                keys: ['id'],
                query: {
                    conditions: [{key: 'oshash', value: oshash, operator: '=='}],
                    operator: '&'
                },
                range: [0, 1],
                sort: [{key: 'name', operator: '+'}]
            }, function(result) {
                if (result.data.items.length) {
                    errorDialog(
                        'The file ' + id + ' already exists' + (
                            filename == result.data.items[0].id
                                ? ''
                                : ' as ' + result.data.items[0].id
                        ) + '.'
                    ).open();
                } else {
                    $uploadDialog.open();
                }
            })
        });
        return {open: Ox.noop};
    }

    function errorDialog(text) {
        return $errorDialog = pandora.ui.iconDialog({
            buttons: [
                Ox.Button({
                        id: 'close',
                        title: 'Close'
                    })
                    .bindEvent({
                        click: function() {
                            $errorDialog.close();
                        }
                    })
            ],
            title: 'Upload File',
            text: text
        });
    }

};

