/**
 * ravs.js
 *
 * @author    Lucio Marcenaro
 * @copyright (c) 2009, by TechnoAware srl
 * @date      23 September 2009
 * @version   $Id$
 *
 * @license ravs.js is licensed under the terms of the Open Source
 * LGPL 3.0 license. Commercial use is permitted to the extent that the 
 * code/component(s) do NOT become part of another Open Source or Commercially
 * licensed development library or toolkit without explicit permission.
 * 
 * License details: http://www.gnu.org/licenses/lgpl.html
 */
 
/*global Ext, Application */
 
Ext.ns('RAVS');
 
// extensions here
 
Ext.BLANK_IMAGE_URL = './ext/resources/images/default/s.gif';
 
// application main entry point
Ext.onReady(function() {
 
    Ext.QuickTips.init();
    //var win;
	var archivewin = Ext.getCmp('archive-win');
	var win = Ext.getCmp('liveview-win');
	var moviewin = Ext.getCmp('movieview-win');

	var updateClock = function()
	{		
		var liveStatusStore = new Ext.data.JsonStore({
			autoLoad: true,  //autoload the data
			url: 'php/caminfo.php',
			baseParams:{cameraName: 'Live'},
			root: 'props',
			fields: [{name: 'id', type: 'int'}, {name: 'name', type: 'string'}, {name: 'sdp', type: 'string'}, {name: 'dss', type: 'string'}, {name: 'enabled', type: 'int'}],
			listeners: {
				load: loadLiveStatusFinished
			}
		});
		
		function loadLiveStatusFinished()
		{
			var someSize = liveStatusStore.getCount();
			if (!someSize)
				return;
			
			var someRecord = liveStatusStore.getAt(0);
			var enabled = someRecord.get('enabled');
			if (!enabled)
			{
				Ext.fly('live').update('Nessuna seduta in corso');
				//var win = Ext.getCmp('liveview-win');
				if(win){
					Ext.destroy(win);
					win=null;
				}
				return;
			}
			Ext.fly('live').update('Seduta in corso');
		}
	} 
	var task = {
		run: updateClock,
		interval: 5000 //1 second
	}
	var runner = new Ext.util.TaskRunner();
	runner.start(task);

    var button = Ext.get('live');
    button.on('click', function(e)
	{
		var target = e.getTarget();
	 	var cameraInfoStore = new Ext.data.JsonStore({
			autoLoad: true,  //autoload the data
			url: 'php/caminfo.php',
			baseParams:{cameraName: 'Live'},
			root: 'props',
			fields: [{name: 'id', type: 'int'}, {name: 'name', type: 'string'}, {name: 'sdp', type: 'string'}, {name: 'dss', type: 'string'}, {name: 'enabled', type: 'int'}],
			listeners: {
				loadexception: loadCameraViewFailed,
				load: loadCameraViewFinished
			}
		});
		
		function loadCameraViewFinished()
		{
			//alert( 'Data Loaded' );
			var someSize = cameraInfoStore.getCount();
			if (!someSize)
				return;
			var someRecord = cameraInfoStore.getAt(0);
		//	var someTitle = someRecord.get('name');
			
			var camSdp = someRecord.get('dss');
			if (!camSdp)
				return;
			var enabled = someRecord.get('enabled');
			if (!enabled)
			{
				alert( 'Nessuna seduta in corso' );
				return;
			}
			if(win)
			{
				Ext.destroy(win);
				win=null;
			}
			if(!win){
				win = new Ext.ux.Media.Window({
					layout:'fit'
					,title: target.innerHTML
					,height: 350
					,width : 400
					,collapsible:false
					//,resizable:false
					,tools: [{id:'gear',handler:function(e,t,p){p.refreshMedia();}}]
					,mediaCfg:{
						mediaType : 'QT'
						,url       : 'sample_100kbit.mp4'
//						,url       : camSdp
						,loop      : false
						,start     : true
						,autoSize  : true
						,controls  : true
						,volume   : '50%'
						,scale : 'toFit'
						,params: {
							src: Ext.isIE? null:'sample_100kbit.mp4'
							,wmode     :'opaque'
							,stretchToFit : true
							,QTSRC : camSdp
							,QTSRCDontUseBrowser: true
						  }
					}
				});
			}
			win.show(this);			
		}
		function loadCameraViewFailed()
		{
		//	alert( 'There was an exception' );
		}
    });
	
	var buttonArchive = Ext.get('control-archive');
    buttonArchive.on('click', function(e)
	{
//		alert( 'Click' );
		var fileInfoStore = new Ext.data.JsonStore({
			autoLoad: true,  //autoload the data
			url: 'php/recinfo.php',
			sortInfo    : {field: "startTime", direction: "DESC"},
			baseParams:{pubonly: 'yes'},
			root: 'props',
			fields: [{name: 'id', type: 'int'}, {name: 'filename', type: 'string'}, {name: 'startTime', type: 'date', dateFormat: 'Y-m-d H:i:s'}, {name: 'stopTime', type: 'date', dateFormat: 'Y-m-d H:i:s'}, {name: 'pub', type: 'bool'}, {name: 'ipaddress', type: 'string'}],
			listeners: {
				loadexception: loadFileInfoFailed,
				load: loadFileInfoFinished
			}
		});
		function loadFileInfoFailed()
		{
			alert( 'Errore lettura archivio' );
		}
		
		function loadFileInfoFinished()
		{
//			alert( 'lettura archivio ok' );
			if(archivewin)
			{
				Ext.destroy(archivewin);
				archivewin=null;
			}
			if(!archivewin)
			{
				function playMovie(){
					var s = grid4.getSelectionModel().getSelections();
					for(var i = 0, r; r = s[i]; i++)
					{
					//alert('rtsp://'+r.data.ipaddress+'/'+r.data.filename);
						if(moviewin)
						{
							Ext.destroy(moviewin);
							moviewin=null;
						}
						if(!moviewin){
							moviewin = new Ext.ux.Media.Window({
								layout:'fit'
								,title: 'Filmato'
								,height: 350
								,width : 400
								,collapsible:false
								//,resizable:false
								,tools: [{id:'gear',handler:function(e,t,p){p.refreshMedia();}}]
								,mediaCfg:{
									mediaType : 'QT'
//									,url       : 'rtsp://192.168.254:554/2009_10_07_2029.mp4'
									,url       : 'sample_100kbit.mp4'
//									,url       : 'rtsp://'+r.data.ipaddress+'/'+r.data.filename
									,loop      : false
									,start     : true
									,autoSize  : true
									,controls  : true
									,volume   : '50%'
									,scale : 'toFit'
									,params: {
										src: Ext.isIE? null:'sample_100kbit.mp4'
										,wmode     :'opaque'
										,stretchToFit : true
										,QTSRC : 'rtsp://'+r.data.ipaddress+'/'+r.data.filename
										,QTSRCDontUseBrowser: true
									  }
								}
							});
						}
						moviewin.show(this);			
					}
				}
				
				var grid4 = new Ext.grid.GridPanel({
					store: fileInfoStore,
					sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
					columns: [
						new Ext.grid.RowNumberer,
						{
						   header: 'Filmato',
						   dataIndex: 'filename',
						   width: 180
						},{
							header: "Inizio",
							width: 150,
							sortable: true,
							renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'),
							dataIndex: 'startTime'
						},{
							header: "Fine",
							width: 150,
							sortable: true,
							renderer:Ext.util.Format.dateRenderer('Y-m-d H:i:s'),
							dataIndex: 'stopTime'
					}],
					viewConfig: {
						forceFit:true
					},
					columnLines: true,

					// inline toolbars
					tbar:[{
						ref: '../playBtn',
						disabled: true,
						text:'Visualizza',
						tooltip:'Visualizza filmato',
						iconCls:'icon-play',
						handler: playMovie
					}],
					width:600,
					height:300,
					frame:true
				});
				
				grid4.getSelectionModel().on('selectionchange', function(sm){
					grid4.playBtn.setDisabled(sm.getCount() < 1);
				});
				
				grid4.on('rowdblclick', function(sm, index, record){
					playMovie();
				});
				
				archivewin = new Ext.Window({
					layout:'fit',
					width:600,
					height:300,
					plain: true,
					items: grid4,
					title:'Video in archivio'
				});
			}
			archivewin.show(this);			
		}
	});
}); // eo function onReady
 
// eof
