
/**
 * @class Voyeur.Tool.DocumentInputAdd
 * @description A panel that provides widgets for creating and updating a corpus.
 * @extends Ext.GridPanel
 * @extends Voyeur.Tool
 * @author Stéfan Sinclair
 * @since 0.1.1
 */
Voyeur.Tool.DocumentInputAdd = Ext.extend(Ext.Panel, {
	constructor : function(config) {
	
		Ext.apply(this, new Voyeur.Tool(config, this));

		var xtype = config.xtype;
		Ext.applyIf(config, {
			bodyStyle: 'text-align: center;',
			items: [{
				xtype: 'textarea',
				id: 'input',
				name: 'input',
				cls: 'input_box',
				hideLabel: true,
				width:'100%'
				,emptyText: this.localize('input_label')
			}, {
				xtype: 'button',
				id: 'reveal',
				text: this.localize('reveal'),
				handler: function(btn){
					var input_value = Ext.getCmp('input').getValue().replace(/^\s+/, '');
					if (this.getCorpus().isEmpty() && input_value.length == 0) {
						return this.alertError({
							msg: this.localize('empty_input_error')
							,animEl: btn.id
							,fn: function(){
								var el = Ext.getCmp('input').getEl();
								el.frame("ff0000");
							}
						});
					}
					this.update({
						tool : 'CorpusSummary'
						,params: {
							input: input_value
							,corpusCreateIfNotExists : true
						}
					})
				},
				scope: this
			}]
			,bbar: [{
				xtype: 'tbbutton'
				,text: this.localize('upload')
				,tooltip : this.localize('uploadTip')
				,iconCls: 'tbupload'
				,handler : function() {
					if (Ext.getCmp('input').getValue()) {
						Ext.Msg.show({
							title : this.localize('uploadIgnoresInputTitle')
							,msg : this.localize('uploadIgnoresInputMsg')
							,buttons : Ext.Msg.YESNO
							,icon: Ext.MessageBox.QUESTION
							,fn: function(btn) {
								if (btn=='yes') {this.showFileUpload();}
							}
							,scope: this
						})
					}
					else {this.showFileUpload();}
				}
				,scope : this
			},
			{
				xtype: 'tbbutton'
				,text: this.localize('openCorpus')
				,tooltip : this.localize('openCorpusTip')
				,iconCls: 'icon-folder'
				,handler : function() {
					if (Ext.getCmp('input').getValue()) {
						Ext.Msg.show({
							title : this.localize('openCorpusIgnoresInputTitle')
							,msg : this.localize('openCorpusIgnoresInputMsg')
							,buttons : Ext.Msg.YESNO
							,icon: Ext.MessageBox.QUESTION
							,fn: function(btn) {
								if (btn=='yes') {this.showOpenCorpus();}
							}
							,scope: this
						})
					}
					else {this.showOpenCorpus();}
				}
				,scope : this
			}]
		});
		Voyeur.Tool.DocumentInputAdd.superclass.constructor.apply(this, arguments);
		
	}

	,showOpenCorpus : function() {

		new Ext.Window({
			modal: true
			,title: this.localize('openCorpus')
			,width: 400
			,items: [{
    				xtype : 'form',
    				labelWidth : 30,
    				labelAlign : 'right',
    				border : false,
    				items : [{
    					xtype : 'combo',
    					id : 'comparisonCorpus',
//    					value : this.getStore().baseParams.comparisonCorpus,
    					fieldLabel : '<span ext:qtip="'
    							+ this.localize('openCorpusTip') + '">'
    							+ this.localize('openCorpus') + '</span>',
    					loadingText : this.localize('loading', 'tool'),
    					width : 300,
    					store : this.getApplication().getCorporaStore()
    				    ,mode:'local'
    				    ,selectOnFocus : true
    				    ,displayField: 'label'
    				    ,triggerAction: 'all'
    				    ,valueField: 'id'
    				    ,emptyText: this.localize('none','tool')
    				}],
    				buttons : [{
    					text : this.localize('ok', 'tool'),
    					iconCls : 'icon-accept',
    					listeners : {
    						click : {
    							fn : function(btn) {
									var formPanel = btn.findParentByType('form');
									var form = formPanel.getForm();
									var comparisonCorpus = form.findField('comparisonCorpus');

									// make sure we don't have any queries
									if (comparisonCorpus.getValue() && !comparisonCorpus.getRawValue()) {comparisonCorpus.setValue('');}
									else if (comparisonCorpus.lastQuery && comparisonCorpus.lastQuery!=comparisonCorpus.getValue()) {
										comparisonCorpus.getStore().loadData({corpora: {corpora: [{id: comparisonCorpus.lastQuery, label: comparisonCorpus.lastQuery, description: ''}]}}, true);
										comparisonCorpus.setValue(comparisonCorpus.lastQuery)
									}
			
									if (form.isDirty()) {
										this.update({params: {corpus: comparisonCorpus.getValue()}, tool: 'CorpusSummary'})
									}
									formPanel.findParentByType('window').destroy();
						
    							},
    							scope : this
    						}
    					}
    				}, {
    					text : this.localize('cancel', 'tool'),
    					handler : function(btn) {
    						btn.findParentByType('window').destroy();
    					}
    				}]
     			}
			]
		}).show();
		
	}
	
	/**
	 * Show the file upload panel.
	 */
	,showFileUpload : function() {
		 var win = new Ext.Window({
		 width:180
		 ,minWidth:165
		 ,id:'winid'
		 ,height:220
		 ,minHeight:200
		 ,stateful:true
		 ,layout:'fit'
		 ,border:false
		 ,closable:true
		 ,title:this.localize('upload')
		 ,items:[{
			xtype:'uploadpanel'
			,addText:this.localize('fileAdd')
			,clickRemoveText : this.localize('clickRemoveText')
			,clickStopText : this.localize('clickStopText')
			,emptyText : this.localize('emptyText')
			,errorText : this.localize('errorText')
			,fileQueuedText : this.localize('fileQueuedText')
			,fileDoneText : this.localize('fileDoneText')
			,fileFailedText : this.localize('fileFailedText')
			,fileStoppedText : this.localize('fileStoppedText')
			,fileUploadingText : this.localize('fileUploadingText')
			,removeAllText : this.localize('removeAllText')
			,removeText : this.localize('removeText')
			,stopAllText : this.localize('stopAllText')
			,uploadText : this.localize('upload')
			,buttonsAt:'tbar'
			,id:'uppanel'
			,url:this.getTromboneUrl()
			,path:'root'
			,maxFileSize:1048576
			,enableProgress:false
			,singleUpload:true
			,forTool: this
			,listeners: {
				'render': {
					fn: function(panel) {
						
						// we need to set the corpus before uploading so that all files are added to the same corpus
						panel.uploader.baseParams.corpus = this.getCorpus().getId();
						panel.uploader.baseParams.corpusCreateIfNotExists = true;

						// we'll handle the results ourselves because we want different parsing of results and behaviour
						panel.uploader.uploadCallback = function(options, success, response) {
							var uploader = panel.uploader;
							uploader.upCount--;
							uploader.form = false;
							var r = response.responseText;
							// for some reason FF wraps the response in <pre> tag
							r = r.replace(/<\/?pre.*?>/g,'');
							r = Ext.decode(r);
							if (r.error) {
								uploader.processFailure(options, response, r.error);
							}
							else {
								uploader.processSuccess(options, response, r);
								if (uploader.upCount==0) { // all finished
									panel.forTool.getApplication().dispatchEvent('CorpusSummaryResultLoaded', panel.forTool, r);
									panel.ownerCt.destroy();
								}
							}
							uploader.fireFinishEvents(options);
						}
					}
					,scope: this
				}
			}
		 }]
		 });
		 win.show();
	}

	// private localization variables
	,i18n : {
		title : {en: "Add Texts"}
		,input_label : {en: "Type in one ore more URLs on separate lines or paste in a full text."}
		,reveal : {en: "reveal"}
		,upload : {en: "Upload"}
		,uploadTip : {en : "Click this to upload one or more files from your local computer."}
		,uploadIgnoresInputTitle : {en: "Confirmation Required"}
		,uploadIgnoresInputMsg : {en: "You cannot upload a file <b>and</b> specify other text(s) to be added. Do you wish to continue with uploading one or more files (the contents of the <i>Add Texts</i> box will be ignored)?"}
		,openCorpus : {en: "Open"}
		,openCorpusTip : {en : "Click this to open a pre-defined corpus."}
		,openCorpusIgnoresInputTitle : {en: "Confirmation Required"}
		,openCorpusIgnoresInputMsg : {en: "You cannot open an existing corpus <b>and</b> specify other text(s) to be added. Do you wish to continue with opening a corpus (the contents of the <i>Add Texts</i> box will be ignored)?"}
		,fileAdd : {en: "Add"}
		,clickRemoveText : {en: "Click to remove"}
		,clickStopText : {en: "Click to stop"}
		,emptyText : {en: "No files"}
		,errorText : {en: "Error"}
		,fileQueuedText : {en: "File <b>{0}</b> is queued for upload"}
		,fileDoneText : {en: "File <b>{0}</b> has been successfully uploaded"}
		,fileFailedText : {en: "File <b>{0}</b> failed to upload"}
		,fileStoppedText : {en: "File <b>{0}</b> stopped by user"}
		,fileUploadingText : {en: "Uploading file <b>{0}</b>"}
		,removeAllText : {en: "Remove All"}
		,removeText : {en: "Remove"}
		,stopAllText : {en: "Stop All"}
		,empty_input_error : {en: "Please provide input before continuing."}
		,help : {en: "<p>This tool allows you to specify the origin of the texts to be analyzed.</p><p>You can provide one or more URLs (on separate lines). Each available URL will be fetched and added as a document. Supported formats include plain text (.txt), HTML (.html), XML (.xml), MS Word (.doc, .docx), RTF (.rtf), and PDF (.pdf).</p><p>You can also copy and paste a single text into the box (this may be preferable if you're experiencing problems with character encoding). You will be able to add additional texts later.</p>"}
	}
});

Ext.reg('voyeurDocumentInputAdd', Voyeur.Tool.DocumentInputAdd);
