var fs = require('fs');
var mapper = require('../mapper');

function attrObjectToHTML(attr) {
    var l = [];
    // Maybe in the future it will be helpful.
    function escape() { return arguments[0];};
    
    for (var a in attr) {
        if (attr.hasOwnProperty(a)) {
            l.push(a + '="' + escape(attr[a]) + '"');
        }
    }
    return l.join(' ');
}


// TODO is it only one element?
function generateHTML(list, templates) {
    var html = '';
    function traverse(o) {
        function tpl(name, attr) {
            if (!o.name) return;
            var t = templates[name] || '';
            html += t.call? t(attr || o) : t;
        }
        
        tpl('start');
        tpl('node');
        tpl('startChildren');        
        
        o.elements && o.elements.forEach(function (element) {
            if (!element) return;         
            
            // TODO generalize this. Not always we're gonna have `html` on beginning.
            // and remove (i.e. refactor) this really strange line below from the code!
            if (element.element && element.element.name && element.element.name.indexOf('html') === 0)
                return;
            
            
            // TODO refactor
            if (element.element.html) {
                traverse(element.element.html);
            } else if (element.element) {
                traverse(element.element);
            }
            tpl('onChild', element);
        });
        
        
        tpl('endChildren');        
        tpl('end');
    }
    list.forEach(function (o) {
    
        traverse(o);
    });
    return html;
}

module.exports = function(fname, o) {
    var html = generateHTML(o, {
        start: function (node) {
            var cls = '';            
            
            if (typeof node.filesize !== 'undefined') {                            
                if (node.filesize > 5 * 1024) 
                    cls = 'file-very-large';
                else if (node.filesize > 2 * 1024) 
                    cls = 'file-large';                    
                else if (node.filesize > 512) 
                    cls = 'file-medium';
                else cls = 'file-small';                    
            }
            var result = '';
            result += '\n<li class="' + cls + '"';
            result += '><section>';            
            return result;
        },
        node: function (node) {
            var filename = node.filename || '';
            var result = '<h1>' + (node.nameInHTML || node.name  || '') + '</h1>';            
            var title = node.filename;
            // Maybe this code below is too PHPish, but it works ;)
            if (typeof node.filesize !== 'undefined')            
                result += '<span class="file-size" title="' + title + '"> (' + node.filesize + 'B)</span>';
            return result;
        },
        end: '</section></li>\n',
        onChild: function (el) {
            return '<code>&lt;' + el.element.name + ' ' +  attrObjectToHTML(el.attrs) + '&gt;</code>';
        },
        startChildren: '<ul>',
        endChildren: '</ul>',
    });
    var htmlTree = '<ul>' + html + '</ul>';
    var htmlHeader = fs.readFileSync('templates/header.html', {encoding:'utf8'});
    var htmlFooter = fs.readFileSync('templates/footer.html', {encoding:'utf8'});
    fs.writeFileSync(fname, htmlHeader + htmlTree + htmlFooter);
}
