jQuery(document).ready(function($){
    
    // CSV IMPORT
    (function(){
        if (!$('#csv-import').length) {
            return;
        }
    
        var rowCount = 0,
            timeout = 5, 
            haltExecution = false, 
            importSuccessCount = 0, 
            importErrorCount = 0, 
            importCount = 0, 
            recordsCount = -1,
            recordsIteration = 0,
            percentage = 0;
        
        var $csvImport = $('#csv-import'),
            $importSuccessCount = $csvImport.find('.counters .success'), 
            $importErrorCount = $csvImport.find('.counters .error'), 
            $importTotalProcessed = $csvImport.find('.counters .total-processed'),
            $importTotal = $csvImport.find('.counters .total'),
            $importPercentage = $csvImport.find('.counters .percentage'),
            $logInfo = $csvImport.find('.log-info'),
            $logErrors = $csvImport.find('.log-errors'),
            $progress = $csvImport.find('.progress').eq(0), 
            $progressBar = $progress.find('.progress-bar'),
            $progressBarSr = $progressBar.find('.sr-only'),
            pause = $csvImport.data('pause') * 1000;
        
        function doQueueMessage(messageObject, counter, doHaltExecution) {
            setTimeout(function(){
                if (haltExecution) {
                    return;
                }
                
                if (messageObject.type == 'error') {
                    messageObject.type = 'danger';
                }
                
                $logInfo.text(messageObject.message);
                if (messageObject.type == 'danger') {
                    $logErrors.prepend('<div class="alert alert-'+messageObject.type+'">'+messageObject.message+'</div>');
                }
                rowCount--;
                
                if (messageObject.counter) {
                    
                    if (messageObject.type == 'success' || messageObject.type == 'info') {
                        importSuccessCount++;
                    } else if (messageObject.counter && messageObject.type == 'danger') {
                        importErrorCount++;
                    }
                    
                    importCount = importSuccessCount + importErrorCount;
                    $importTotalProcessed.text(importCount);
                    $importSuccessCount.text(importSuccessCount);
                    $importErrorCount.text(importErrorCount);
                    
                    recordsIteration++;
                    percentage = Math.floor((recordsIteration / recordsCount) * 100);
                    $progressBar.width(percentage + '%');
                    $progressBarSr.text(percentage + '%');
                    $importPercentage.text(percentage + '%');
                    $importTotal.text(recordsCount);    
                }

                haltExecution = (doHaltExecution === true ? true : false);
            }, counter * timeout);    
        }
        
        function sendRequest(attributes) {
            if (haltExecution) {
                return;
            }
            attributes = attributes || $('#csv-import').data('attributes');
            
            var sendData = {'ListCsvImport': {}};
            for (i in attributes) {
                sendData['ListCsvImport'][i] = attributes[i];
            }
            
            if ($('meta[name=csrf-token-name]').length && $('meta[name=csrf-token-value]').length){
                var csrfTokenName = $('meta[name=csrf-token-name]').attr('content'),
                    csrfTokenValue = $('meta[name=csrf-token-value]').attr('content');
                sendData[csrfTokenName] = csrfTokenValue;
            }
            
            $.ajax({
                url: '',
                data: sendData,
                type: 'POST',
                dataType: 'json'
            }).done(function(json){
                if (json.result == 'error') {
                    doQueueMessage({type:'error', message: json.message, counter: false}, 1, true);
                } else if (json.result == 'success'){
                    if (json.attributes) {
                        setTimeout(function(){
                            sendRequest(json.attributes);  
                        }, pause);
                    }
                    
                    if (json.recordsCount && recordsCount == -1) {
                        recordsCount = json.recordsCount;
                    }
                    
                    if (json.import_log) {
                        for (i in json.import_log) {
                            rowCount++;
                            doQueueMessage(json.import_log[i], rowCount);
                        }
                    }

                    rowCount++;
                    doQueueMessage({type:'success', message: json.message, counter: false}, rowCount);
                }
            }).fail(function(jqXHR){
                if (jqXHR.statusText == 'error') {
                    jqXHR.statusText = 'Error, aborting the import process!'
                }
                doQueueMessage({type:'error', message: jqXHR.statusText, counter: false}, 1, true);
            });
        }
        sendRequest();
        
        // fake iframe to avoid cookie expiration.
        setInterval(function() {
            var iframe = $('<iframe/>', {
                src: $('#list-import-log-container').data('iframe'), 
                width: 1, 
                height: 1
            }).css({display:'none'});
            $('body').append(iframe);
            setTimeout(function(){
                iframe.remove();
            }, 1000 * 60 * 2);
        }, 1000 * 60 * 20);
    })();
    
    // ping page from within iframe
    (function(){
        if (!$('#ping').length || !window.opener) {
            return;
        }
        if ($('meta[name=csrf-token-name]').length && $('meta[name=csrf-token-value]').length){
            var csrfTokenName = $('meta[name=csrf-token-name]').attr('content'),
            csrfTokenValue = $('meta[name=csrf-token-value]').attr('content');
            
            window.opener.$('meta[name=csrf-token-name]').attr('content', csrfTokenName);
            window.opener.$('meta[name=csrf-token-value]').attr('content', csrfTokenValue);    
        }
    })();
});