MagazinePage AssociatsAdsWithMagazinePageStruct(List <Ad> adListParam, int totalAdsCount, int adsIndex, MagazinePage magPage, int adCount)
        {
            List <Ad> adsForThisLayout = new List <Ad>();

            bool adsAvailableForPage = (adsIndex + adCount) <= totalAdsCount;

            if (adsAvailableForPage)
            {
                adsForThisLayout = adListParam.GetRange(adsIndex, adCount);
            }
            else
            {
                int adsNeeded = adsIndex + adCount;

                int adsNeededFromAdListStart = adsNeeded - totalAdsCount;
                for (int i = 0; i < adsNeededFromAdListStart; i++)
                {
                    adListParam.Add(adListParam[i]);
                }
                adsForThisLayout = adListParam.GetRange(adsIndex, adCount);
            }
            magPage.Ads = adsForThisLayout;
            return(magPage);
        }
        public void LoadModalController(List <Ad> adListParam, string selectedClassification)
        {
            magPageList = new List <MagazinePage>();



            adList = adListParam;

            int totalAdsCount = adList.Count;

            var device = UIDevice.CurrentDevice;



            bool adsAvailable      = true;
            int  adsIndex          = 0;
            int  magazinePageIndex = 0;

            int banManProCounter = 1;


            Random rnd = new Random();

            do
            {
                int layout = 1;

                if (banManProCounter == 5)
                {
                    layout           = 4;
                    banManProCounter = 1;
                }
                else
                {
                    banManProCounter++;
                    var randomDouble = rnd.NextDouble();

                    if (device.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
                    {
                        //condition will be met 35% of the time
                        if (randomDouble > 0 && randomDouble <= .33)
                        {
                            layout = 1;
                        }

                        //condition will be met 35% of the time
                        if (randomDouble > .33 && randomDouble <= .66)
                        {
                            layout = 2;
                        }

                        //condition will be met 15% of the time
                        if (randomDouble > .66)
                        {
                            layout = 3;
                        }
                    }
                    else
                    {
                        layout = 2;
                    }
                }


                MagazinePage magPage = new MagazinePage();
                magPage.SelectedClassification = selectedClassification;

                var currentAd = adList[adsIndex];
                if (currentAd.IsFeatured && layout != 4)
                {
                    magPage.Layout = 2;
                    List <Ad> adsForThisLayout = new List <Ad>();
                    adsForThisLayout.Add(currentAd);
                    magPage.Ads = adsForThisLayout;
                    adsIndex++;
                }
                else
                {
                    switch (layout)
                    {
                    case 1:
                    {
                        //int adCount = 1;
                        int adCount = 2;
                        magPage        = AssociatsAdsWithMagazinePageStruct(adList, totalAdsCount, adsIndex, magPage, adCount);
                        magPage.Layout = 1;

                        adsIndex += adCount;
                        break;
                    }

                    case 2:
                    {
                        int adCount = 1;
                        magPage        = AssociatsAdsWithMagazinePageStruct(adList, totalAdsCount, adsIndex, magPage, adCount);
                        magPage.Layout = 2;

                        adsIndex += adCount;
                        break;
                    }

                    case 3:
                    {
                        //int adCount = 1;
                        int adCount = 3;
                        magPage        = AssociatsAdsWithMagazinePageStruct(adList, totalAdsCount, adsIndex, magPage, adCount);
                        magPage.Layout = 3;

                        adsIndex += adCount;
                        break;
                    }

                    case 4:
                    {
                        int adCount = 0;
                        magPage        = AssociatsAdsWithMagazinePageStruct(adList, totalAdsCount, adsIndex, magPage, adCount);
                        magPage.Layout = 4;

                        adsIndex += adCount;
                        break;
                    }

                    default:
                    {
                        int adCount = 0;
                        magPage        = AssociatsAdsWithMagazinePageStruct(adList, totalAdsCount, adsIndex, magPage, adCount);
                        magPage.Layout = 1;

                        adsIndex += adCount;
                        break;
                    }
                    }
                }

                magPage.MagazinePageIndex = magazinePageIndex++;

                magPageList.Add(magPage);

                if (adsIndex < adList.Count)
                {
                    adsAvailable = true;
                }
                else
                {
                    adsAvailable = false;
                }
            } while (adsAvailable);

            //Assign total pages to each MagazinePageStruct
            for (int i = 0; i < magPageList.Count; i++)
            {
                magPageList[i].TotalPages = magPageList.Count;
                var ads = magPageList[i].Ads;
                Console.WriteLine("Layout: " + magPageList[i].Layout);
                ads.ForEach(r => Console.WriteLine(r.Name + " id: " + r.ID));
            }

            Console.WriteLine("Total Ads: " + adList.Count);
        }