/**
 * @author <a href="mailto:uwolfer@tocco.ch">Urs Wolfer</a>
 * @since Aug 7, 2009
 * @class PackageLoader
 * @pattern singleton
 */
var PackageLoader = (function() {

    var packageModules = [];

    // asynchronous approach, with ext deps -uw
    /*var pkgs = null;
    var importQueue = [];
    var options = {
        url: '/js/nice.jsb2',
        method: 'GET',
        scope: this,
        callback: function(options, success, response) {
            if (success) {
                var jsonData = Ext.util.JSON.decode(response.responseText);
                if (jsonData) {
                    pkgs = new Ext.util.MixedCollection();
                    pkgs.addAll(jsonData.pkgs);
//                    log(jsonData);
                    Ext.each(importQueue, function(item) {
                        import(item);
                    });
                } else {
                    log('Failed parsing JS index.', 'error');
                }
            } else {
                log('Failed loading JS index.', 'error');
            }
        }
    };
    Ext.Ajax.request(options);*/

    return  {

        /**
         * Registers a new package (module).
         *
         * @param {Object} packageObject Package in JSBuilder 2 format.
         */
        register : function(packageObject, srcPath, deployPath) {
            packageObject.srcPath = srcPath;
            packageObject.deployPath = deployPath;
            packageModules[packageModules.length] = packageObject;
        },

        /**
         *
         * @param pkgName
         * @param level
         */
        importPackage : function(pkgName, level) {
            level = level || 'always';
            if (level.toLowerCase() == 'debug' && !DEBUG) return;
            if (level.toLowerCase() == 'release' && DEBUG) return;
            /*if (pkgs == null) {
                importQueue[importQueue.length] = pkgName;
                return;
            }
            var pkg = pkgs.find(function(item) {
                return item.name === pkgName;
            });*/
            var deployDir = null;
            var srcPath = null;
            var pkg = null;
            for (var j = 0; j < packageModules.length; j++) {
                var currentModule = packageModules[j];

                for (var i = 0; i < currentModule.pkgs.length; i++) {
                    if (currentModule.pkgs[i].name === pkgName) {
                        pkg = currentModule.pkgs[i];
                        deployDir = currentModule.deployPath;
                        srcPath = currentModule.srcPath;
                        break;
                    }
                }
            }
            if (pkg === null) {
                log('Unresolved import: ' + pkgName, 'error');
                return;
            }
            if (COMPRESSEDJS) {
//                loadJs('/js/release/' + pkg.file.replace(/package.js/g, "package-debug.js"));
                loadJs('/js/' + deployDir + '/' + pkg.file);
            } else {
                var includes = pkg.fileIncludes;
                for (i = 0; i < includes.length; i++) {
                    var include = includes[i];
                    loadJs('/js/' + srcPath + include.path + include.text);
                }
                /*Ext.each(pkg.fileIncludes, function(include) {
                    loadJs('/' + include.path + include.text);
                });*/
            }
        },

        /**
         * Loads a whole package list (JSB2)
         *
         * @param {Object} pkgList List object in JSB2 format.
         */
        load : function(pkgList) {
            for (var i = 0; i < pkgList.pkgs.length; i++) {
                var currentPkg = pkgList.pkgs[i];
                importPackage(currentPkg.name);
            }
        }

    }; //end return

})();
var importPackage = PackageLoader.importPackage;


/*
Code to generate a hiveapp file list of a jsb2 file. -uw

var jsb2output = "";
Ext.each(extjsb2.pkgs, function(pkg) {
    var pkgname = pkg.name;
    Ext.each(pkg.fileIncludes, function(file) {
        jsb2output += "<javascript source=\"[#self]/js/" + file.path + file.text + "\" package=\""+ pkgname +"\"/>\n";
    });
    jsb2output += "\n";
});
console.log(jsb2output);
*/
