/*================================*/
/*========= assets loader ========*/
/*================================*/
import "./preloadjs-0.6.2.min.js";
import "./soundjs-0.6.2.min";
export default (argument)=> {
	let _default = {
		imgList          : [],
		audioList        : [],
		loadStart        : function(){},
		progressCallBack : function(){},
		completeCallBack : function(){},
		errorCallBack    : function(){},
	}

	let opts = Object.assign({}, _default, argument );
        
        //拼合图片 音频列表
        let AllList = opts.imgList.concat( opts.audioList );

		let packagingListObject = (array)=>{
        	let file = [];
			for(let i = 0 ; i < array.length; i++){
	        	let n;
	            if(array[i].id){
	            	n  = {id:array[i].id,src:array[i].src}
	            }else{
	            	let name = array[i].split('/');
	        		let id   = name[name.length-1].split('.');
	            	n  = {id:id[0],src:array[i]}
	            }
	            file.push(n)
	        }  
	        return file;      	
        }        

        //生成createjs 规范的加载对象数组
        let files  = packagingListObject(AllList);
        
        let queue = new createjs.LoadQueue(true,'','*');

        //设置最大连接数100
        queue.setMaxConnections(10);  
        queue.maintainScriptOrder = true;  
        // if(!appPlugins.isAndriod()){
        queue.installPlugin(createjs.Sound);
        // }else{
        // console.log(opts.audioList.length)
		// if(opts.audioList.length > 0) createjs.Sound.registerSounds(opts.audioList);
        // }
        
        // createjs.Sound.initializeDefaultPlugins()
        queue.loadManifest(files);

        opts.loadStart && opts.loadStart();

        queue.on("progress", function() {
        	opts.progressCallBack && opts.progressCallBack(queue.progress);
        });
        
        queue.on("complete", function(e) {
            opts.completeCallBack && opts.completeCallBack(e);
        })	

        queue.on("error", function(e) {
            opts.errorCallBack && opts.errorCallBack(e);
        })

        // queue.on("fileload", (e)=>{
        // 	console.log(e.item)
        // }, this);

        // queue.on("error", (e)=>{
        // 	console.log(e)
        // }, this);

	/*

	let loader = preloader({
	  	xhrImages: false
	});
	//进度
	loader.on('progress',(progress)=> {
	  	opts.progressCallBack && opts.progressCallBack(progress);
	});
	loader.on('complete',()=> {
	  	opts.completeCallBack && opts.completeCallBack();
	});
	for(var i in opts.imgList){
		loader.addImage(opts.imgList[i]);
	}
	if(opts.audioList.length > 0){
		for(var i in opts.audioList){
			loader.addAudio(opts.audioList[i]);
		}			
	}
		
	//加载 加载器
	loader.load();
	*/	

	return queue;	
};