text: /* global showError, logoff */
  sap.ui.core.mvc.Controller.extend("epmwebsockets.view.Master", {
  
  	onInit : function() {
  		this.oInitialLoadFinishedDeferred = jQuery.Deferred();
  
  		var oEventBus = this.getEventBus();
  		oEventBus.subscribe("Detail", "TabChanged", this.onDetailTabChanged, this);
  
  		var oList = this.getView().byId("list");
  	    oList.attachEvent("updateFinished", function() {
  			this.oInitialLoadFinishedDeferred.resolve();
  			oEventBus.publish("Master", "InitialLoadFinished");
  		}, this);
  		
  		//On phone devices, there is nothing to select from the list. There is no need to attach events.
  		if (sap.ui.Device.system.phone) {
  			return;
  		}
  
  		this.getRouter().attachRoutePatternMatched(this.onRouteMatched, this);
  
  		oEventBus.subscribe("Detail", "Changed", this.onDetailChanged, this);
  		oEventBus.subscribe("Detail", "NotFound", this.onNotFound, this);
  	},
  
  	onRouteMatched : function(oEvent) {
  		var sName = oEvent.getParameter("name");
  
  		if (sName !== "main") {
  			return;
  		}
  
  		//Load the detail view in desktop
  		this.loadDetailView();
  
  		//Wait for the list to be loaded once
  		this.waitForInitialListLoading(function () {
  
  			//On the empty hash select the first item
  			this.selectFirstItem();
  
  		});
  
  	},
  
  	onDetailChanged : function (sChanel, sEvent, oData) {
  		var sEntityPath = oData.sEntityPath;
  		//Wait for the list to be loaded once
  		this.waitForInitialListLoading(function () {
  			var oList = this.getView().byId("list");
  
  			var oSelectedItem = oList.getSelectedItem();
  			// The correct item is already selected
  			if(oSelectedItem && oSelectedItem.getBindingContext().getPath() === sEntityPath) {
  				return;
  			}
  
  			var aItems = oList.getItems();
  
  			for (var i = 0; i < aItems.length; i++) {
  				if (aItems[i].getBindingContext().getPath() === sEntityPath) {
  					oList.setSelectedItem(aItems[i], true);
  					break;
  				}
  			}
  		});
  	},
  
  	onDetailTabChanged : function (sChanel, sEvent, oData) {
  		this.sTab = oData.sTabKey;
  	},
  	
  	loadDetailView : function(){
  		this.getRouter().myNavToWithoutHash({ 
  			currentView : this.getView(),
  			targetViewName : "epmwebsockets.view.Detail",
  			targetViewType : "XML"
  		});
  	},
  
  	waitForInitialListLoading : function (fnToExecute) {
  		jQuery.when(this.oInitialLoadFinishedDeferred).then(jQuery.proxy(fnToExecute, this));
  	},
  
  	onNotFound : function () {
  		this.getView().byId("list").removeSelections();
  	},
  
  	selectFirstItem : function() {
  		var oList = this.getView().byId("list");
  		var aItems = oList.getItems();
  		if (aItems.length) {
  			oList.setSelectedItem(aItems[0], true);
      		//Load the detail view in desktop
              this.loadDetailView();
      		oList.fireSelect({"listItem" : aItems[0]});
  		}
  		else {
      		this.getRouter().myNavToWithoutHash({ 
      			currentView : this.getView(),
      			targetViewName : "epmwebsockets.view.NotFound",
      			targetViewType : "XML"
          	});
  		}
  	},
  
  	onSearch : function() {
  		this.oInitialLoadFinishedDeferred = jQuery.Deferred();
  		// Add search filter
  		var filters = [];
  		var searchString = this.getView().byId("searchField").getValue();
  		if (searchString && searchString.length > 0) {
  			filters = [ new sap.ui.model.Filter("Name", sap.ui.model.FilterOperator.Contains, searchString) ];
  		}
  		// Update list binding
  		this.getView().byId("list").getBinding("items").filter(filters);
  		
  		//On phone devices, there is nothing to select from the list
  		if (sap.ui.Device.system.phone) {
  			return;
  		}
  
  		//Wait for the list to be reloaded
  		this.waitForInitialListLoading(function () {
  			//On the empty hash select the first item
  			this.selectFirstItem();
  		});
  	},
  
  	onSelect : function(oEvent) {
  		// Get the list item either from the listItem parameter or from the event's
  		// source itself (will depend on the device-dependent mode)
  		this.showDetail(oEvent.getParameter("listItem") || oEvent.getSource());
  	},
  
  	showDetail : function(oItem) {
  		// If we're on a phone device, include nav in history
  		var bReplace = jQuery.device.is.phone ? false : true;
  		this.getRouter().navTo("detail", {
  			from: "master",
  			entity: oItem.getBindingContext().getPath().substr(1),
  			tab: this.sTab
  		}, bReplace);
  	},
  	
  	getEventBus : function () {
  		return sap.ui.getCore().getEventBus();
  	},
  
  	getRouter : function () {
  		return sap.ui.core.UIComponent.getRouterFor(this);
  	},
  	
  	onExit : function(oEvent){
  		var oEventBus = this.getEventBus();
  		oEventBus.unsubscribe("Detail", "TabChanged", this.onDetailTabChanged, this);
  		oEventBus.unsubscribe("Detail", "Changed", this.onDetailChanged, this);
  		oEventBus.unsubscribe("Detail", "NotFound", this.onNotFound, this);
  	},
  	
      createEntity: function() {
          if (!this.dialog) {
              this.dialog = sap.ui.xmlfragment(this.getView().getId(), "epmwebsockets.view.CreateDialog", this);
              this.getView().addDependent(this.dialog);
  	        var i18n = this.getOwnerComponent().getModel("i18n").getResourceBundle();
              this.dialog.setTitle(i18n.getText("createDialogTitle"));
          }
  
          jQuery.sap.syncStyleClass("sapUiSizeCompact", this.getView(), this.dialog);
          this.dialog.open(); 
      },
  
      onDialogAcceptButton: function() {
          this.getOwnerComponent().getModel().create("/Products/", {
              Name: this.byId("dialogInput1").getValue(),
              Description: this.byId("dialogInput2").getValue(),
              Price: this.byId("dialogInput3").getValue()
          }, {
              success: jQuery.proxy(function() {
          		this.getRouter().navTo("detail", {entity: "Products('"
          		    + this.byId("dialogInput1").getValue() + "')"});
                  this.dialog.close();
          	}, this),
              error: showError
          });
      },
  
      onDialogCloseButton: function() {
          this.dialog.close();
      }
      
  });