text: sap.ui.define(["sap/ui/core/mvc/Controller",
  	"sap/m/MessageBox",
  	"./utilities",
  	"sap/ui/core/routing/History"
  ], function(BaseController, MessageBox, Utilities, History) {
  	"use strict";
  
  	return BaseController.extend("com.sap.build.standard.prototipoDeAltaFidelidad.controller.MasterPage2", {
  		handleRouteMatched: function(oEvent) {
  			var sAppId = "App6190512eed0c121ed2b5c976";
  
  			var oParams = {};
  			var oView = this.getView();
  			var bSelectFirstListItem = true;
  			if (oEvent.mParameters.data.context || oEvent.mParameters.data.masterContext) {
  				this.sContext = oEvent.mParameters.data.context;
  
  				this.sMasterContext = oEvent.mParameters.data.masterContext;
  
  			} else {
  				if (this.getOwnerComponent().getComponentData()) {
  					var patternConvert = function(oParam) {
  						if (Object.keys(oParam).length !== 0) {
  							for (var prop in oParam) {
  								if (prop !== "sourcePrototype" && prop.includes("Set")) {
  									return prop + "(" + oParam[prop][0] + ")";
  								}
  							}
  						}
  					};
  
  					this.sMasterContext = patternConvert(this.getOwnerComponent().getComponentData().startupParameters);
  
  				}
  			}
  
  			var oPath;
  
  			if (this.sMasterContext && oEvent.getParameters().config.bypassed.target[0] !== this.sMasterContext) {
  				oPath = {
  					path: "/" + this.sMasterContext,
  					parameters: oParams
  				};
  				this.getView().bindObject(oPath);
  			} else if (this.sContext) {
  				var sCurrentContextPath = "/" + this.sContext;
  
  				bSelectFirstListItem = false;
  			}
  
  			if (bSelectFirstListItem) {
  				oView.addEventDelegate({
  					onBeforeShow: function() {
  						var oContent = this.getView().getContent();
  						if (oContent) {
  							if (!sap.ui.Device.system.phone) {
  								var oList = oContent[0].getContent() ? oContent[0].getContent()[0] : undefined;
  								if (oList) {
  									var sContentName = oList.getMetadata().getName();
  									if (sContentName.indexOf("List") > -1) {
  										oList.attachEventOnce("updateFinished", function() {
  											var oFirstListItem = this.getItems()[0];
  											if (oFirstListItem) {
  												oList.setSelectedItem(oFirstListItem);
  												oList.fireItemPress({
  													listItem: oFirstListItem
  												});
  											}
  										}.bind(oList));
  									}
  								}
  							}
  						}
  					}.bind(this)
  				});
  			}
  
  		},
  		_attachSelectListItemWithContextPath: function(sContextPath) {
  			var oView = this.getView();
  			var oContent = this.getView().getContent();
  			if (oContent) {
  				if (!sap.ui.Device.system.phone) {
  					var oList = oContent[0].getContent() ? oContent[0].getContent()[0] : undefined;
  					if (oList && sContextPath) {
  						var sContentName = oList.getMetadata().getName();
  						var oItemToSelect, oItem, oContext, aItems, i;
  						if (sContentName.indexOf("List") > -1) {
  							if (oList.getItems().length) {
  								oItemToSelect = null;
  								aItems = oList.getItems();
  								for (i = 0; i < aItems.length; i++) {
  									oItem = aItems[i];
  									oContext = oItem.getBindingContext();
  									if (oContext && oContext.getPath() === sContextPath) {
  										oItemToSelect = oItem;
  									}
  								}
  								if (oItemToSelect) {
  									oList.setSelectedItem(oItemToSelect);
  								}
  							} else {
  								oView.addEventDelegate({
  									onBeforeShow: function() {
  										oList.attachEventOnce("updateFinished", function() {
  											oItemToSelect = null;
  											aItems = oList.getItems();
  											for (i = 0; i < aItems.length; i++) {
  												oItem = aItems[i];
  												oContext = oItem.getBindingContext();
  												if (oContext && oContext.getPath() === sContextPath) {
  													oItemToSelect = oItem;
  												}
  											}
  											if (oItemToSelect) {
  												oList.setSelectedItem(oItemToSelect);
  											}
  										});
  									}
  								});
  							}
  						}
  
  					}
  				}
  			}
  
  		},
  		_onPageNavButtonPress: function(oEvent) {
  
  			var oBindingContext = oEvent.getSource().getBindingContext();
  
  			return new Promise(function(fnResolve) {
  
  				this.doNavigate("MasterPage1", oBindingContext, fnResolve, "");
  			}.bind(this)).catch(function(err) {
  				if (err !== undefined) {
  					MessageBox.error(err.message);
  				}
  			});
  
  		},
  		doNavigate: function(sRouteName, oBindingContext, fnPromiseResolve, sViaRelation) {
  			var sPath = (oBindingContext) ? oBindingContext.getPath() : null;
  			var oModel = (oBindingContext) ? oBindingContext.getModel() : null;
  
  			var sEntityNameSet;
  			if (sPath !== null && sPath !== "") {
  				if (sPath.substring(0, 1) === "/") {
  					sPath = sPath.substring(1);
  				}
  				sEntityNameSet = sPath.split("(")[0];
  			}
  			var sNavigationPropertyName;
  			var sMasterContext = this.sMasterContext ? this.sMasterContext : sPath;
  
  			if (sEntityNameSet !== null) {
  				sNavigationPropertyName = sViaRelation || this.getOwnerComponent().getNavigationPropertyForNavigationWithContext(sEntityNameSet, sRouteName);
  			}
  			if (sNavigationPropertyName !== null && sNavigationPropertyName !== undefined) {
  				if (sNavigationPropertyName === "") {
  					this.oRouter.navTo(sRouteName, {
  						context: sPath,
  						masterContext: sMasterContext
  					}, false);
  				} else {
  					oModel.createBindingContext(sNavigationPropertyName, oBindingContext, null, function(bindingContext) {
  						if (bindingContext) {
  							sPath = bindingContext.getPath();
  							if (sPath.substring(0, 1) === "/") {
  								sPath = sPath.substring(1);
  							}
  						} else {
  							sPath = "undefined";
  						}
  
  						// If the navigation is a 1-n, sPath would be "undefined" as this is not supported in Build
  						if (sPath === "undefined") {
  							this.oRouter.navTo(sRouteName);
  						} else {
  							this.oRouter.navTo(sRouteName, {
  								context: sPath,
  								masterContext: sMasterContext
  							}, false);
  						}
  					}.bind(this));
  				}
  			} else {
  				this.oRouter.navTo(sRouteName);
  			}
  
  			if (typeof fnPromiseResolve === "function") {
  				fnPromiseResolve();
  			}
  
  		},
  		_onObjectListItemPress: function(oEvent) {
  
  			var oBindingContext = oEvent.getParameter("listItem").getBindingContext();
  
  			return new Promise(function(fnResolve) {
  				this.doNavigate("DetailPage1", oBindingContext, fnResolve, "");
  			}.bind(this)).catch(function(err) {
  				if (err !== undefined) {
  					MessageBox.error(err.message);
  				}
  			});
  
  		},
  		_onObjectListItemPress1: function(oEvent) {
  
  			var oBindingContext = oEvent.getSource().getBindingContext();
  
  			return new Promise(function(fnResolve) {
  
  				this.doNavigate("DetailPage1", oBindingContext, fnResolve, "");
  			}.bind(this)).catch(function(err) {
  				if (err !== undefined) {
  					MessageBox.error(err.message);
  				}
  			});
  
  		},
  		onInit: function() {
  			this.oRouter = sap.ui.core.UIComponent.getRouterFor(this);
  			this.oRouter.getTarget("MasterPage2").attachDisplay(jQuery.proxy(this.handleRouteMatched, this));
  
  		},
  		onExit: function() {
  
  			// to destroy templates for bound aggregations when templateShareable is true on exit to prevent duplicateId issue
  			var aControls = [{
  				"controlId": "sap_List_Page_0-content-sap_m_ObjectList-1",
  				"groups": ["items"]
  			}];
  			for (var i = 0; i < aControls.length; i++) {
  				var oControl = this.getView().byId(aControls[i].controlId);
  				if (oControl) {
  					for (var j = 0; j < aControls[i].groups.length; j++) {
  						var sAggregationName = aControls[i].groups[j];
  						var oBindingInfo = oControl.getBindingInfo(sAggregationName);
  						if (oBindingInfo) {
  							var oTemplate = oBindingInfo.template;
  							oTemplate.destroy();
  						}
  					}
  				}
  			}
  
  		}
  	});
  }, /* bExport= */ true);
  