/// <summary>
        ///
        /// </summary>
        /// <param name="viewModel">Target grid</param>
        /// <param name="maxAmount">Hard max of how many to add</param>
        /// <param name="targetCount"></param>
        /// <param name="index"></param>
        public void AddEmptyCells(GovernorGridViewModel grid, int targetCount, int?maxAmount = null, int?index = null)
        {
            var addedCount = 0;
            var lastIndex  = grid.HeaderCells.Count - 1;

            if (index.HasValue) // validate the index
            {
                if (index > lastIndex)
                {
                    index = null;                    // change to an 'Add' operation
                }
                else if (index < 0)
                {
                    index = 0;
                }
            }

            while (grid.HeaderCells.Count < targetCount && (!maxAmount.HasValue || addedCount < maxAmount.Value))
            {
                if (grid.HeaderCells.Count < targetCount)
                {
                    var sortKey = "col_" + (grid.HeaderCells.Count + 1);
                    if (index == null)
                    {
                        grid.HeaderCells.Add(new GridCellViewModel(string.Empty)
                        {
                            SortKey = sortKey, SortType = "sortText"
                        });
                        foreach (var row in grid.Rows)
                        {
                            row.Cells.Add(new GridCellViewModel(string.Empty)
                            {
                                SortKey = sortKey, SortType = "sortText"
                            });
                        }
                    }
                    else
                    {
                        grid.HeaderCells.Insert(index.Value, new GridCellViewModel(string.Empty)
                        {
                            SortKey = sortKey, SortType = "sortText"
                        });
                        foreach (var row in grid.Rows)
                        {
                            row.Cells.Insert(index.Value, new GridCellViewModel(string.Empty)
                            {
                                SortKey = sortKey, SortType = "sortText"
                            });
                        }
                    }
                    addedCount++;
                }
            }
        }
        private void CreateGrids(GovernorsDetailsDto dto, IEnumerable <GovernorModel> governors, bool isHistoric, int?groupUid, int?establishmentUrn)
        {
            var roles = dto.ApplicableRoles.Where(role => !EnumSets.eSharedGovernorRoles.Contains(role)
                                                  ||
                                                  (RoleEquivalence.GetLocalEquivalentToSharedRole(role) != null &&
                                                   !dto.ApplicableRoles.Contains(RoleEquivalence.GetLocalEquivalentToSharedRole(role).Value)));

            foreach (var role in roles)
            {
                var equivalantRoles = RoleEquivalence.GetEquivalentToLocalRole(role).Cast <int>().ToList();
                var pluralise       = !EnumSets.eSingularGovernorRoles.Contains(role);


                var grid = new GovernorGridViewModel($"{_nomenclatureService.GetGovernorRoleName(role, eTextCase.SentenceCase, pluralise)}{(isHistoric ? " (in past 12 months)" : string.Empty)}")
                {
                    Tag              = isHistoric ? "historic" : "current",
                    Role             = role,
                    IsSharedRole     = EnumSets.eSharedGovernorRoles.Contains(role),
                    GroupUid         = groupUid,
                    EstablishmentUrn = establishmentUrn,
                    IsHistoricRole   = isHistoric,
                    RoleName         = _nomenclatureService.GetGovernorRoleName(role)
                };

                var displayPolicy = dto.RoleDisplayPolicies.Get(role);
                Guard.IsNotNull(displayPolicy, () => new Exception($"The display policy should not be null for the role '{role}'"));
                bool includeEndDate = ((isHistoric && role == GR.Member || role != GR.Member) && displayPolicy.AppointmentEndDate) ||
                                      (role.OneOfThese(GR.ChiefFinancialOfficer, GR.AccountingOfficer) && isHistoric);

                SetupHeader(role, grid, displayPolicy, includeEndDate);

                var list = governors.Where(x => x.RoleId.HasValue && equivalantRoles.Contains(x.RoleId.Value));
                foreach (var governor in list)
                {
                    var isShared       = governor.RoleId.HasValue && EnumSets.SharedGovernorRoles.Contains(governor.RoleId.Value);
                    var establishments = string.Join(", ",
                                                     governor.Appointments?.Select(a => $"{a.EstablishmentName}, URN: {a.EstablishmentUrn}") ??
                                                     new string[] { });
                    var appointment = governor.Appointments?.SingleOrDefault(a => a.EstablishmentUrn == EstablishmentUrn);
                    var startDate   = (isShared && appointment != null) ? appointment.AppointmentStartDate : governor.AppointmentStartDate;
                    var endDate     = (isShared && appointment != null) ? appointment.AppointmentEndDate : governor.AppointmentEndDate;

                    var row = grid.AddRow(governor, endDate)
                              .AddCell(governor.GetFullName(), displayPolicy.FullName)
                              .AddCell(string.IsNullOrWhiteSpace(establishments) ? null : establishments, role.OneOfThese(GR.LocalGovernor, GR.ChairOfLocalGoverningBody))
                              .AddCell(governor.Id, displayPolicy.Id)
                              .AddCell(AppointingBodies.FirstOrDefault(x => x.Id == governor.AppointingBodyId)?.Name, displayPolicy.AppointingBodyId)
                              .AddCell(startDate?.ToString("dd/MM/yyyy"), displayPolicy.AppointmentStartDate)
                              .AddCell(endDate?.ToString("dd/MM/yyyy"), includeEndDate)
                              .AddCell(governor.PostCode, displayPolicy.PostCode)
                              .AddCell(governor.DOB?.ToString("dd/MM/yyyy"), displayPolicy.DOB)
                              .AddCell(governor.GetPreviousFullName(), displayPolicy.PreviousFullName)
                              .AddCell(governor.EmailAddress, displayPolicy.EmailAddress)
                              .AddCell(governor.TelephoneNumber, displayPolicy.TelephoneNumber);

                    if (isHistoric)
                    {
                        var gov = new HistoricGovernorViewModel
                        {
                            AppointingBodyId     = governor.AppointingBodyId,
                            AppointingBody       = AppointingBodies.FirstOrDefault(x => x.Id == governor.AppointingBodyId)?.Name,
                            AppointmentEndDate   = new DateTimeViewModel(governor.AppointmentEndDate),
                            AppointmentStartDate = new DateTimeViewModel(governor.AppointmentStartDate),
                            FullName             = governor.GetFullName(),
                            RoleName             = _nomenclatureService.GetGovernorRoleName(role)
                        };

                        HistoricGovernors.Add(gov);
                    }
                }

                grid.Rows         = grid.Rows.OrderByDescending(x => x.SortValue).ThenBy(x => x.Model.GetFullName()).ToList();
                HistoricGovernors = HistoricGovernors.OrderByDescending(x => x.AppointmentEndDate.ToDateTime()).ThenBy(x => x.FullName).ToList();

                if (isHistoric)
                {
                    HistoricGrids.Add(grid);
                }
                else
                {
                    Grids.Add(grid);
                }
            }
        }