//  @import com.hedgehoglab.application.view.BaseView;
//  @import com.hedgehoglab.ui.SelectPane;

function ContactImportView(scope) {
    BaseView.call(this, scope);
    var _this = this;
    var _lab_box = new LabBox({width:400,height:72,buttons:['continue','rollback','upload csv','cancel']});
	var _old_title=scope.action.attr('title');
	var _ui_disabled=false;
	
    $.blockUI.defaults.applyPlatformOpacityRules = false;
    $.blockUI.defaults.fadeIn = 0;
    $.blockUI.defaults.fadeOut = 0;
    $.blockUI.defaults.timeout = 0;
        
    scope.action.bind('click', function() {
		// if(!_ui_disabled){
	       
	        _this.raise_event('import_clicked', {});			
		// }

        return false;
    });

	var _show=this.show;
	this.show=function(){
		 _show({});	
	};

    _lab_box.subscribe_to('lb_rollback_clicked', function(e) {});
    _lab_box.subscribe_to('lb_continue_clicked', function(e) {});

	this.disable_actions=function(state){
		if(state){
			// scope.action.attr('tit',scope.action.attr('title'))
			_ui_disabled=true;
			scope.action.attr('title','Your licence does not permit you to import any more contacts');
			scope.action.addClass('disabled');	
		}else{
			_ui_disabled=false;
			scope.action.attr('title',_old_title);
			scope.action.removeClass('disabled');
		}
	}



    this.show = function(model) {
        _this.render_template_with_view('contacts/CSVImportView', {},{}, function(rendered_template) {
                    _lab_box.show(rendered_template, function(el,f) {
                        var upload = new AjaxUpload($('.lb-upload-csv',f), {
                           action: '/api/backend/import',
                           name: 'importFile',
                           autoSubmit: true,
                           onChange: function(file, ext) {
                               // after file is selected
                               if (! (ext && /^(csv)$/.test(ext))){
                                  alert('Error: only upload csv files');
                                  return false;
                               }
                           },
                           onSubmit: function(file, ext) {
                               // before the file is uploaded

								//	Do a fake pause to show loading!
								
                               _lab_box.enable_button('cancel', false);
							  _lab_box.enable_button('upload csv', false);
								//alert("Block UI");
                               // $.blockUI({
                               //                                  message:'Please wait while solomon imports your data. This can take upto 5 minutes depending on how many contacts you are importing.',
                               //                                  css:{padding:'5em'}});
                                 //TODO Centre blockui dialog!
								_lab_box.show_loading("Importing contacts...");
								//	show loading in footer
                           },
                           onComplete: function(file, response) {
								//	Cancel loading in footer
								// alert("import complete");
								// console.info(file,response);
								// 
								// console.warn(response.text);
								var message=$("success success",response)[0];
								var result={};
																
								if(message!=null){
									result=$.extend(result,{message:$(message).text()});
								}else{
									// var success=$("success success",response)[0];
									var message=$($("error error",response)[0]).text();
									result=$.extend(result,{"error":true,"message":message});									
								}
								
								setTimeout(function(e){
									_lab_box.hide(function(e){
										_lab_box.enable_button('upload csv', true);
										_lab_box.enable_button('cancel', true);
										_lab_box.hide_loading();
									});

								_this.raise_event('import_complete', result);
								},1);
								
								//	Raise a message saying contacts imported successfully
								//	possibly pass fail status etc.
							}
							
                       });
                    });
                    _lab_box.enable_button('rollback', false);
                    _lab_box.enable_button('continue', false);
                });
    };
};