cs.pro.JobTabPanel = Ext.extend(Ext.TabPanel, {

	empty: true,
	task: null,
	toolkeys: {
		'prosurface': 'Resurface',
		'translate': 'Translate',
		'prodecimate': 'Decimate',
		'provolume' : 'Volume'
	},

	initComponent: function()
	{
		Ext.apply(this, {
			// 20090221 bwd changed from autoheight to fixed height
			region: 'south',
			frame: false,
			hidden: true,
			resizeTabs: true,
			minTabWidth: 165,
			maxTabWidth: 235
		});
		cs.pro.JobTabPanel.superclass.initComponent.apply(this, arguments);
	},
	
	showPanel: function() {
		this.show();
		this.setHeight(89);
		this.ownerCt.doLayout();		
	},
	
	hidePanel: function() {
		this.hide();
		this.ownerCt.doLayout();
	},
	
	addTab: function(filename, title, job_id)
	{
		this.showPanel();
		var newTab = this.add(
			new cs.pro.JobTab({
				id: job_id,
				title: this.toolkeys[title]+": "+filename,
				description: filename
		}));
		newTab.show();
		newTab.doLayout();
		if(this.empty)
		{
			this.empty = false;
		}
	},

	removeAllTabs: function()
	{
		this.items.each(this.removeTab, this);
		this.hidePanel();
	},
	
	removeTab: function(item)
	{
		this.remove(item);
		if (this.items.length <= 0)
		{
			this.empty = true;
			this.hidePanel();
		}
		return true;
	},

	getJoblist: function()
	{
		Ext.Ajax.request({
			url: 'json/joblist',
			callback: this.gotJoblist,
			scope: this
		});
	},
	
	gotJoblist: function(options, success, response)
	{
		if(response = cs.common.lib.errorFilter(success, response, this.getResurface))
		{
			var rows = response.rows;
			for(var i = 0; i < rows.length; i++)
			{
				var row = rows[i];
				this.addTab(row.filename, row.tool, row.id);
			}
			this.addListener('tabchange', this.getProgress);
			this.getProgress();
		}
	},

	logout: function()
	{
		Ext.TaskMgr.stop(this.task);
		this.removeListener('tabchange');
		this.removeAllTabs();
	},
	
	login: function()
	{
		this.task = Ext.TaskMgr.start({
			run: this.getProgress,
			scope: this, 
			interval: 5000
		});
		this.getJoblist();
	},
	
	getProgress: function()
	{
		var activeTab = this.getActiveTab();
		if(activeTab != null && activeTab.id != 0)
		{
			switch(activeTab.status)
			{
				case null:
				case 'waiting':
				case 'assigned':
				case 'finished':
				case 'complete':
				case 'crashed':

					Ext.Ajax.request({
						url: 'json/pro_status',
						params: {
							job: activeTab.id
						},
						callback: this.gotProgress,
						scope: this
					});
					break;
					
				case 'processing':
					var url = 'http://' + activeTab.url + '/job_' + activeTab.id + '.js';
					Ext.Ajax.request({
						url: url,
						scriptTag: true
					});
					break;
			}
		}
	},

	gotProgress: function(options, success, response)
	{
		if(response = cs.common.lib.errorFilter(success, response, this.getResurface))
		{
			var activeTab = this.getActiveTab();
			if(response.id == activeTab.id)
			{
				switch(response.status) {
	
					case 'waiting':
						activeTab.progressBar.updateText('Accepted');
						activeTab.status = 'waiting';
						break;

					case 'assigned':
						activeTab.progressBar.updateText('Assigned');
						activeTab.url = response.url;
						activeTab.status = 'assigned';
						break;

					case 'processing':
						if(activeTab.status != 'finished')
						{
							activeTab.progressBar.updateText('Processing');
							activeTab.url = response.url;
							activeTab.status = 'processing';
						}
						break;

					case 'complete':
					case 'crashed':
						this.remove(activeTab);
						cs.pro.viewport.refreshGrid();
						break;

					default:
						alert('unknown');
				}
			}
		}
	},

	updateProgress: function(id, progress)
	{
		var activeTab = this.getActiveTab();
		if(activeTab !== null && activeTab.id == id && activeTab.progressBar.isVisible())
		{
			activeTab.progressBar.updateProgress(progress / 100);
			if(progress > 99)
			{
				activeTab.status = 'complete';
			}
		}
	},

	jobCrash: function(id, message)
	{
	    switch(message) {
	    	case 'translate':
	    		text = 'The translation of your file failed. It is possible that we cannot read your file. Please try a different file format, see the documentation, or contact tech support.';
	    		break;
	    	case 'prosurface':
	    		text = 'Resurfacing your file failed. This could be due to a translation error with the input file. If you are confident that your input file is robust, you can try to resurface it again. Otherwise, check the quality and format of your input file.';
	    		break;
	    	case 'prodecimate':
	    		text = 'Decimation of your file failed. This could be due to a translation error with the file. If you are confident that your file is robust, you can try to decimate it again.';
	    		break;
	    }
	    
		window.alert(text);
		var activeTab = this.getActiveTab();
		if(activeTab !== null && activeTab.id == id && activeTab.progressBar.isVisible())
		{
			activeTab.status = 'finished';
		}
	}
});

cs.pro.JobTab = Ext.extend(Ext.form.FormPanel, {
	
	title: 'Unknown',
	description: 'Unknown',
	id: 0,
	url: null,
	status: 'waiting',

	initComponent: function()
	{
		this.progressBar = new Ext.ProgressBar({
			autoHeight: true,
			autoWidth: true,
			text: 'Idle'
		});
		
		this.fieldSet = new Ext.form.FieldSet({
			autoHeight: true,
			title: this.description,
			items: [ this.progressBar ]
		});
		
		Ext.apply(this, {
			frame: true,
			autoHeight: true,
			items: [
				this.fieldSet
			]
		});

		cs.pro.JobTab.superclass.initComponent.apply(this, arguments);
	}
});
