/// <summary>
        /// Groups the model controller finished.
        /// </summary>
        /// <param name="sender">The sender.</param>
        public void GroupModelControllerFinished(UPGroupModelController sender)
        {
            UPInsightBoardItemGroupModelController itemController = (UPInsightBoardItemGroupModelController)sender;

            if (itemController.ControllerState == GroupModelControllerState.Finished)
            {
                UPMInsightBoardItem currentItem = (UPMInsightBoardItem)itemController.Group.Children[0];
                if (this.Group.Children.Count == 0)
                {
                    this.Group.AddChild(currentItem);
                }
                else
                {
                    bool inserted = false;
                    for (int i = 0; i < this.Group.Children.Count; i++)
                    {
                        UPMInsightBoardItem item = (UPMInsightBoardItem)this.Group.Children[i];
                        if (item.SortIndex == currentItem.SortIndex)
                        {
                            inserted = true;
                            break;
                        }
                        else if (item.SortIndex > currentItem.SortIndex)
                        {
                            this.Group.InsertChildAtIndex(currentItem, i);
                            inserted = true;
                            break;
                        }
                    }

                    if (!inserted)
                    {
                        this.Group.AddChild(currentItem);
                    }
                }

                this.pending--;
            }

            if (this.pending == 0)
            {
                bool waitTillFinishLoading = false; // ((NSNumber)NSUserDefaults.StandardUserDefaults().ObjectForKey("Dashboard.WaitTillFinishLoad")).BoolValue;
                var  dashboard             = this.Delegate as DashboardPageModelController;

                if (waitTillFinishLoading && dashboard != null)
                {
                    var viewController = dashboard.ModelControllerDelegate;
                    // viewController.PerformSelectorOnMainThreadWithObjectWaitUntilDone(@selector(hideSyncIndicator), null, false);
                }

                this.ControllerState = GroupModelControllerState.Finished;
                this.Delegate.GroupModelControllerFinished(this);
            }
        }
        /// <summary>
        /// Searches the operation did finish with count.
        /// </summary>
        /// <param name="operation">The operation.</param>
        /// <param name="count">The count.</param>
        public void SearchOperationDidFinishWithCount(Operation operation, int count)
        {
            this.ControllerState = GroupModelControllerState.Finished;
            UPMInsightBoardItem item = (UPMInsightBoardItem)this.Group.Children[0];

            item.Countable = true;
            item.Count     = count;
            item.SortIndex = this.SortIndex;
            IConfigurationUnitStore configStore   = ConfigurationUnitStore.DefaultStore;
            SearchAndList           searchAndList = configStore.SearchAndListByName(this.searchAndListConfigurationName);

            this.infoAreaid = searchAndList.InfoAreaId;
            InfoArea infoAreaConfig = configStore.InfoAreaConfigById(this.infoAreaid);

            if (!string.IsNullOrEmpty(infoAreaConfig.ColorKey))
            {
                item.Color = AureaColor.ColorWithString(infoAreaConfig.ColorKey);
            }

            if (!string.IsNullOrEmpty(this.Menu.DisplayName))
            {
                item.Title = this.Menu.DisplayName;
            }
            else
            {
                item.Title = item.Count > 1 ? infoAreaConfig.PluralName : infoAreaConfig.SingularName;
            }

            item.ImageName = this.Menu.ImageName ?? infoAreaConfig.ImageName;
            UPMAction action = new UPMAction(null);

            if (count == 1)
            {
                this.crmQuery.Find(this.requestOption, this);
                action.SetTargetAction(this, this.SwitchToDetailOrganizer);
            }
            else
            {
                action.SetTargetAction(this, this.SwitchToListOrganizer);
            }

            item.Action = action;
            if (count != 1)
            {
                this.Delegate.GroupModelControllerFinished(this);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UPInsightBoardItemGroupModelController"/> class.
        /// </summary>
        /// <param name="menu">The menu.</param>
        /// <param name="theDelegate">The delegate.</param>
        /// <param name="sortIndex">Index of the sort.</param>
        /// <param name="insightBoardItemViewReference">The insight board item view reference.</param>
        /// <param name="recordIdentification">The record identification.</param>
        public UPInsightBoardItemGroupModelController(Menu menu, IGroupModelControllerDelegate theDelegate, int sortIndex,
                                                      ViewReference insightBoardItemViewReference, string recordIdentification)
            : base(theDelegate)
        {
            this.Menu      = menu;
            this.SortIndex = sortIndex;
            this.InsightBoardItemViewReference = insightBoardItemViewReference;
            this.recordIdentification          = recordIdentification;
            bool showImage = false;
            UPMInsightBoardItemType insightBoardType;

            if (menu?.ViewReference?.ViewName == "InsightBoardItem")
            {
                string contextMenuName = this.Menu.ViewReference.ContextValueForKey("ContextMenu");
                this.isAlternativeContextMenu = false;
                if (!string.IsNullOrEmpty(contextMenuName))
                {
                    IConfigurationUnitStore configStore = ConfigurationUnitStore.DefaultStore;
                    Menu configMenu = configStore.MenuByName(contextMenuName);
                    if (configMenu != null)
                    {
                        this.relevantViewReference = configMenu.ViewReference;
                        this.countable             = this.relevantViewReference.ViewName == "RecordListView";
                        if (this.relevantViewReference.ContextValueForKey("Modus") == "GeoSearch")
                        {
                            this.countable = false;
                        }

                        if (menu.ViewReference.ContextValueIsSet("ForceActionStyle"))
                        {
                            this.forcedRecordListView = this.countable;
                            this.countable            = false;
                        }
                        else
                        {
                            this.forcedRecordListView = false;
                        }

                        this.isAlternativeContextMenu = true;
                        if (this.countable)
                        {
                            this.searchAndListConfigurationName = this.relevantViewReference.ContextValueForKey("ConfigName");
                            this.infoAreaid = this.relevantViewReference.ContextValueForKey("InfoArea");
                            if (string.IsNullOrEmpty(this.searchAndListConfigurationName))
                            {
                                this.searchAndListConfigurationName = this.infoAreaid;
                            }
                        }
                    }
                }

                if (!this.isAlternativeContextMenu)
                {
                    this.relevantViewReference = this.Menu.ViewReference;
                    this.countable             = true;
                    if (menu.ViewReference.ContextValueIsSet("ForceActionStyle"))
                    {
                        this.forcedRecordListView = true;
                        this.countable            = false;
                    }

                    this.searchAndListConfigurationName = this.relevantViewReference.ContextValueForKey("ConfigName");
                    if (this.forcedRecordListView)
                    {
                        IConfigurationUnitStore configStore   = ConfigurationUnitStore.DefaultStore;
                        SearchAndList           searchAndList = configStore.SearchAndListByName(this.searchAndListConfigurationName);
                        this.infoAreaid = searchAndList.InfoAreaId;
                    }
                }

                string requestOptionString = this.Menu.ViewReference.ContextValueForKey("RequestOption");
                this.relevantViewReference = new ViewReference(this.relevantViewReference, requestOptionString, "InitialRequestOption", null);
                this.requestOption         = UPCRMDataStore.RequestOptionFromString(requestOptionString, UPRequestOption.Offline);
                insightBoardType           = this.countable ? UPMInsightBoardItemType.Count : UPMInsightBoardItemType.Action;
            }
            else
            {
                this.relevantViewReference = this.Menu.ViewReference;
                showImage        = true;
                insightBoardType = UPMInsightBoardItemType.ExMenu;
            }

            UPMInsightBoardGroup insightBoardGroup = new UPMInsightBoardGroup(StringIdentifier.IdentifierWithStringId("temp"));

            insightBoardGroup.LabelText = "InsightBoard";
            this.Group           = insightBoardGroup;
            this.ControllerState = GroupModelControllerState.Pending;
            IIdentifier identifier;

            if (!string.IsNullOrEmpty(this.infoAreaid))
            {
                identifier = new RecordIdentifier(this.infoAreaid, null);
            }
            else
            {
                identifier = StringIdentifier.IdentifierWithStringId("InsightBoardItem");
            }

            UPMInsightBoardItem item = new UPMInsightBoardItem(identifier)
            {
                ShowImage = showImage,
                Type      = insightBoardType
            };

            this.Group.AddChild(item);
        }
        /// <summary>
        /// Continues the with record identification.
        /// </summary>
        /// <param name="_linkRecordIdentification">The link record identification.</param>
        private void ContinueWithRecordIdentification(string _linkRecordIdentification)
        {
            this.linkRecordIdentification = _linkRecordIdentification;
            if (this.countable)
            {
                IConfigurationUnitStore configStore = ConfigurationUnitStore.DefaultStore;
                this.crmQuery = !string.IsNullOrEmpty(this.searchAndListConfigurationName)
                    ? new UPContainerMetaInfo(this.searchAndListConfigurationName, this.parameters)
                    : new UPContainerMetaInfo(configStore.FieldControlByNameFromGroup("List", this.infoAreaid));

                if (!string.IsNullOrEmpty(this.linkRecordIdentification))
                {
                    this.crmQuery.SetLinkRecordIdentification(this.linkRecordIdentification);
                }

                if (!string.IsNullOrEmpty(this.filterName))
                {
                    if (this.crmQuery != null)
                    {
                        UPConfigFilter filter = configStore.FilterByName(this.filterName);
                        if (filter != null)
                        {
                            if (this.parameters?.Count > 0)
                            {
                                filter = filter.FilterByApplyingValueDictionaryDefaults(this.parameters, true);
                            }

                            this.crmQuery.ApplyFilter(filter);
                        }
                    }
                }

                Operation operation = this.crmQuery.CountTheDelegate(this.requestOption, this);
                if (operation == null)
                {
                    this.SearchOperationDidFinishWithCount(null, 0);
                }
            }
            else
            {
                UPMInsightBoardItem item = (UPMInsightBoardItem)this.Group.Children[0];
                item.Countable = false;
                item.Count     = 1;
                item.Title     = !string.IsNullOrEmpty(this.Menu.DisplayName) ? this.Menu.DisplayName : string.Empty;
                item.ImageName = this.Menu.ImageName;
                item.SortIndex = this.SortIndex;

                this.detailActionViewReference    = this.relevantViewReference;
                this.detailActionRecordIdentifier = this.linkRecordIdentification;

                UPMAction action = new UPMAction(null);
                if (this.forcedRecordListView)
                {
                    action.SetTargetAction(this, this.SwitchToListOrganizer);
                }
                else
                {
                    action.SetTargetAction(this, this.SwitchToDetailOrganizer);
                }

                item.Action          = action;
                this.ControllerState = GroupModelControllerState.Finished;
                this.Delegate.GroupModelControllerFinished(this);
            }
        }