ActiveAdUnit CreateAdUnit(IEnumerable<ICreative> creativeSet, Ad ad, IAdSource adSource)
        {
            CreativeCompanions companions = null;
            var activeAdUnits = new List<ActiveAdUnit>();
            foreach (var creative in creativeSet)
            {
                if (creative is CreativeLinear)
                {
                    var linear = (CreativeLinear)creative;
                    var eligableMediaFiles = new Queue<MediaFile>(PrioritizeMedia(linear.MediaFiles));
                    ActiveAdUnit chosenAd = null;
                    while (eligableMediaFiles.Any())
                    {
                        var media = eligableMediaFiles.Dequeue();
                        var creativeSource = new LinearSource(linear, media);
                        var vpaid = OnLoadPlayer(creativeSource);
                        if (vpaid != null)
                        {
                            if (VpaidController.Handshake(vpaid))
                            {
                                chosenAd = new ActiveAdUnit(creativeSource, vpaid, ad, adSource);
                                break;
                            }
                            else
                            {
                                OnUnloadPlayer(vpaid);
                            }
                        }
                    }

                    if (chosenAd == null) throw new LoadException(new Exception("Unable to find a player to play the linear ad."));
                    activeAdUnits.Add(chosenAd);
                }
                else if (creative is CreativeNonLinears)
                {
                    bool found = false;
                    var nonLinears = (CreativeNonLinears)creative;
                    foreach (var nonLinear in (nonLinears).NonLinears)
                    {
                        var creativeSource = new NonLinearSource(nonLinear, nonLinears);
                        var vpaid = OnLoadPlayer(creativeSource);
                        if (vpaid != null)
                        {
                            if (VpaidController.Handshake(vpaid))
                            {
                                activeAdUnits.Add(new ActiveAdUnit(creativeSource, vpaid, ad, adSource));
                                found = true;
                                break;
                            }
                            else
                            {
                                OnUnloadPlayer(vpaid);
                            }
                        }
                    }
                    if (!found)
                    {
                        throw new LoadException(new Exception("Unable to find a player to play any nonlinear ads."));
                    }
                }
                else if (creative is CreativeCompanions)
                {
                    companions = (CreativeCompanions)creative;
                }
                else { /* not supported, ignore */ }
            }
            return activeAdUnits.FirstOrDefault(); // there should only be one linear or nonlinear ad in the sequenced group. Others are ignored.
        }
        ActiveAdUnit CreateAdUnit(IEnumerable <ICreative> creativeSet, Ad ad, IAdSource adSource)
        {
            CreativeCompanions companions = null;
            var activeAdUnits             = new List <ActiveAdUnit>();

            foreach (var creative in creativeSet)
            {
                if (creative is CreativeLinear)
                {
                    var          linear             = (CreativeLinear)creative;
                    var          eligableMediaFiles = new Queue <MediaFile>(PrioritizeMedia(linear.MediaFiles));
                    ActiveAdUnit chosenAd           = null;
                    while (eligableMediaFiles.Any())
                    {
                        var media          = eligableMediaFiles.Dequeue();
                        var creativeSource = new LinearSource(linear, media);
                        var vpaid          = OnLoadPlayer(creativeSource);
                        if (vpaid != null)
                        {
                            if (VpaidController.Handshake(vpaid))
                            {
                                chosenAd = new ActiveAdUnit(creativeSource, vpaid, ad, adSource);
                                break;
                            }
                            else
                            {
                                OnUnloadPlayer(vpaid);
                            }
                        }
                    }

                    if (chosenAd == null)
                    {
                        throw new LoadException(new Exception("Unable to find a player to play the linear ad."));
                    }
                    activeAdUnits.Add(chosenAd);
                }
                else if (creative is CreativeNonLinears)
                {
                    bool found      = false;
                    var  nonLinears = (CreativeNonLinears)creative;
                    foreach (var nonLinear in (nonLinears).NonLinears)
                    {
                        var creativeSource = new NonLinearSource(nonLinear, nonLinears);
                        var vpaid          = OnLoadPlayer(creativeSource);
                        if (vpaid != null)
                        {
                            if (VpaidController.Handshake(vpaid))
                            {
                                activeAdUnits.Add(new ActiveAdUnit(creativeSource, vpaid, ad, adSource));
                                found = true;
                                break;
                            }
                            else
                            {
                                OnUnloadPlayer(vpaid);
                            }
                        }
                    }
                    if (!found)
                    {
                        throw new LoadException(new Exception("Unable to find a player to play any nonlinear ads."));
                    }
                }
                else if (creative is CreativeCompanions)
                {
                    companions = (CreativeCompanions)creative;
                }
                else /* not supported, ignore */ } {
        }
        return(activeAdUnits.FirstOrDefault());    // there should only be one linear or nonlinear ad in the sequenced group. Others are ignored.
    }