Ext.ns('app');

app.playlist = Ext.extend(Ext.Panel,{
	region: 'east',
	layout:'accordion',
	//html : 'playlist',
	width : 200,
	border : true,
	curPL : null,
	autoPlay : true,
	autoLoop : false,
	initComponent : function(){
		this.tbar= ['','->',{
	        text : 'Авто',
	        enableToggle: true,
	        pressed :this.autoPlay,
	        tooltip: 'Auto play',
	        handler: function(t){
    			this.autoPlay = t.pressed;
	    	},
	        scope : this
	    },' ',{
	        text : 'Давтах',
	        enableToggle: true,
	        tooltip: 'Loop',
	        handler: function(t){
	    		this.autoLoop = t.pressed;
	    	},
	        scope : this 
	        //handler: this.toolHandle.createDelegate(this,['ganre'])
	    },' ',{	    	
	    	text : 'Шинэ',
	        tooltip: 'New List',
	        handler: this.newPlayList,
	        scope : this 
	    }];
		this.hideCollapseTool = true;
		app.playlist.superclass.initComponent.call(this);
		this.on('add',function(){
			//this.doLayout();
		},this);
		this.on('render',this.initPlayList,this);
		this.addClass('play-list');
		
	},
	addSound : function(sound_id){
		if(user.login == false){
			alert('user not login-d');
			return;
		}
		this.curPL.addItem_(sound_id);
	},
	pushActive : function(sound_id,playL_id){
		var pl = this.get('local-pl-'+playL_id);
		if(pl){
			pl.activeSound(sound_id);
		}
	},
	playFinish : function(sound_id,playL_id){
		if(this.autoPlay == false) return;
		//console.log('finish='+sound_id);
		var pl = this.get('local-pl-'+playL_id);
		if(pl){
			var r=pl.nextSound(sound_id);
			if(r == null){
				if(this.autoLoop == true){
					r=pl.firstSound();
					if(r == null) return;
					this.fireEvent('pl_play',r.soundID,pl.pl_id);
				}
				return;
			}
			this.fireEvent('pl_play',r.soundID,pl.pl_id);
		}
	},
	mDisable : function(label){
		this.disable();
		this.body.mask(label | 'Түр хүлээнэ үү.');
	},
	mEnable : function(label){
		this.enable();
		this.body.unmask();
	},
	initPlayList : function(){
		this.mDisable();
		this.body.addClass('p-bg');
		Ext.Ajax.request({
			url: www.url+'/playlist/get',
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				if(obj.er == false){
					var ii =0;
					var pp = null;
					Ext.each(obj.plist,function(it){
						var p = this.newPList(it.label,it.playlist_id);
						if(ii==0) pp = p;
						ii ++;
					},this);
					this.doLayout();
					if(pp != null){
						pp.expand();
					}
				}else{
					alert('Error :'+obj.msg);
				}
				this.mEnable();
			},
			scope :this
		});
	},
	newPlayList : function(){
		if(user.login == false){
			alert('user not login-d');
			return;
		}
		var plName = prompt('Play list name','');
		if(plName == null) return;
		if(plName.trim() ==""){
			alert('empty');
			return;
		}
		this.mDisable();
		
		Ext.Ajax.request({
			url: www.url+'/playlist/add',
			params : {
				name : plName
			}, 
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				if(obj.er == false){
					this.newPList(obj.pl_name,obj.pl_id);
					this.doLayout();
				}else{
					alert('Error :'+obj.msg);
				}
				this.mEnable();
			},
			scope :this
		});
		
	},
	newPList:function(label,id){
		var pl = new app.playlistOne({
			id : 'local-pl-'+id,
			title :label,
			pl_id : id
		});
		pl.on('expand',function(p){
			this.curPL = p;
		},this);
		
		pl.on('pl_delete',function(p){
			this.remove(p);
		},this);
		
		pl.on('pl_play',function(sound_id,playL_id){
			this.fireEvent('pl_play',sound_id,playL_id);
		},this);
		this.add(pl);
		return pl;
	}
});
app.playlistOne = Ext.extend(Ext.Panel,{
	border : false,
	collapsed :true,
	hideCollapseTool :true,
	pl_data : null,
	pl_id : 0,
	
	initComponent : function(){
		this.tools = [{
			id:'refresh',
			qtip :'Дахин ачаалах',
			handler: this.reLoad,scope : this
		},{
			id : 'gear',
			qtip :'Нэрийг солих',
			handler: this.changeName,scope : this
		},{
			id : 'close',
			qtip :'Устгах',
			handler: this.pl_delete,scope : this
		}];
		
		this.on('expand',function(p){
			var t = p.getTool('refresh');
			t.show();
			var t = p.getTool('close');
			t.show();
			var t = p.getTool('gear');
			t.show();
			this.initData();
		},this);
		this.on('collapse',function(p){
			var t = p.getTool('refresh');
			t.hide();
			var t = p.getTool('close');
			t.hide();
			t.hide();
			var t = p.getTool('gear');
			t.hide();
		});
		app.playlistOne.superclass.initComponent.call(this);
	},
	activeSound : function(sound_id){
		var r = null;
		Ext.each(this.pl_data,function(it){
			if(it.soundID == sound_id){
				r = it;
			}
			it.removeClass('list-item-active');
		},this);
		r.addClass('list-item-active');
	},
	okPlay:function(){
		
	},
	firstSound : function(){
		var ar = this.pl_data;
		var r = ar[0];
		return Ext.isObject(r)?r : null;
	},
	nextSound : function(sound_id){
		var ar = this.pl_data;
		for(var i = 0, len = ar.length; i < len; i++){
			var it = ar[i];
			if(it.soundID == sound_id){
				var r = ar[i+1];
				return Ext.isObject(r)?r : null;
			}
		}
		return null;
	},
	initLoad : function(){
		this.body.addClass('panel-loading');
		//this.body.update('Load ...');
	},
	finishLoad : function(){
		this.body.removeClass('panel-loading');
		//this.body.update('');
	},
	initData : function(){
		if(this.pl_data == null){
			this.pl_data=[];
			this.initLoad();
			Ext.Ajax.request({
				url: www.url+'/playlist/list',
				params : {
					pl_id : this.pl_id
				}, 
				success: function(response, opts) {
					var obj = Ext.decode(response.responseText);
					if(obj.er == false){
						Ext.each(obj.list,function(it){
							this.addItem_pl(it.label,it.sound_id);
						},this);
						
					}else{
						alert('Error :'+obj.msg);
					}
					this.finishLoad();
				},
				scope :this
			});
		}
	},
	changeName : function(){
		var plName = prompt('Play list name',this.title);
		if(plName == null) return;
		if(plName.trim() ==""){
			alert('empty');
			return;
		}
		this.setTitle(plName + ' ...');
		Ext.Ajax.request({
			url: www.url+'/playlist/change.name',
			params : {
				pl_id : this.pl_id,
				label : plName
			}, 
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				if(obj.er == false){
					this.setTitle(obj.label);
				}else{
					alert('Error :'+obj.msg);
				}
			},
			scope :this
		});
	},
	reLoad : function(){
		Ext.each(this.pl_data,function(it){
			it.remove();
		},this);
		this.pl_data = null;
		this.initData();
	},
	pl_delete : function(e,t,p){
		if(confirm("Delete List? \n"+this.title) == false){
			return;
		}
		var oldTitle = this.title;
		this.setTitle('Устгаж байна ...');
		p.disable();
		t.hide();
		Ext.Ajax.request({
			url: www.url+'/playlist/delete',
			params : {
				pl_id : this.pl_id
			}, 
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				if(obj.er == false){
					this.fireEvent('pl_delete',this);
				}else{
					alert('Error :'+obj.msg);
				}
				this.setTitle(oldTitle);
				p.enable();
			},
			scope :this
		});
	},
	addItem_:function(sound_id){
		var item= this.addItem_pl('load ...',sound_id);
		item.isDisable = true;
		Ext.Ajax.request({
			url: www.url+'/playlist/add-item',
			params : {
				pl_id : this.pl_id,
				s_id : sound_id
			}, 
			success: function(response, opts) {
				var obj = Ext.decode(response.responseText);
				var sp = item.child('span');
				if(obj.er == false){
					sp.update(obj.label);
					item.isDisable = false;
				}else{
					alert(obj.msg);
					item.remove();
				}
			},
			scope :this
		});
	},
	addItem_pl : function(label,sid){
		//this.body
		var dh =Ext.DomHelper;
		var item = dh.append(this.body,{
			cls : 'list-item',
			cn : {
				tag : 'span',
				html : label
			}
		},true);
		this.pl_data.push(item);
		item.addClassOnOver('list-item-over');
		item.addClassOnClick('list-item-click');
		item.isDeleteing = false;
		item.isDisable = false;
		item.soundID = sid;
		item.hover(function(e){
			if(item.isDeleteing == true){
				return;
			}
			if(item.mtool){
				item.mtool.show();
			}else{
				item.mtool = item.insertFirst({
					cls : 'x-tool',
					html : ' '
				});
				item.mtool.on('click',function(e){					
					e.stopEvent();
					if(confirm("Delete? \n"+label) == false){
						return;
					}
					var sp = item.child('span');
					sp.update('Устгаж байна ...');
					item.isDeleteing = true;
					item.mtool.hide();
					
					Ext.Ajax.request({
						url: www.url+'/playlist/delete-item',
						params : {
							pl_id : this.pl_id,
							s_id : sid
						}, 
						success: function(response, opts) {
							var obj = Ext.decode(response.responseText);
							if(obj.er == false){
								this.pl_data.remove(item);
								item.remove();
							}else{
								alert('Error :'+obj.msg);
								sp.update(label);
							}
						},
						scope :this
					});
				},this);
			}
		},function(e){
			if(item.mtool){
				item.mtool.hide();
			}
		},this);
		item.on('click',function(e){
			e.stopEvent();
			if(item.isDeleteing == true){
				alert('Устгаж байна.');
				return;
			}
			this.fireEvent('pl_play',sid,this.pl_id);
		},this);
		return item;
	}	
});
