private static VASTADWrapperCreative FindMatchingCreative(VASTADInLine ad, VASTADInLineCreative creative, VASTADWrapper wrapperAd)
        {
            Type type = creative.Item.GetType();
            var appropriateCreatives = ad.Creatives.Where(c => c.Item.GetType() == type);
            Type matchingType;
            if (type == typeof(VASTADInLineCreativeLinear))
                matchingType = typeof(VASTADWrapperCreativeLinear);
            else if (type == typeof(VASTADInLineCreativeNonLinearAds))
                matchingType = typeof(VASTADWrapperCreativeNonLinearAds);
            else if (type == typeof(VASTADInLineCreativeCompanionAds))
                matchingType = typeof(VASTADWrapperCreativeCompanionAds);
            else
                return null;

            var appropriateWrapperCreatives = wrapperAd.Creatives.Where(c => c.Item.GetType() == matchingType);

            int index = appropriateCreatives.ToList().IndexOf(creative);
            if (appropriateWrapperCreatives.Count() > index)
                return appropriateWrapperCreatives.ElementAt(index);
            else
                return null;
        }
        private static VASTADWrapperCreative FindMatchingCreative(VASTADInLine ad, VASTADInLineCreative creative, VASTADWrapper wrapperAd)
        {
            Type type = creative.Item.GetType();
            var  appropriateCreatives = ad.Creatives.Where(c => c.Item.GetType() == type);
            Type matchingType;

            if (type == typeof(VASTADInLineCreativeLinear))
            {
                matchingType = typeof(VASTADWrapperCreativeLinear);
            }
            else if (type == typeof(VASTADInLineCreativeNonLinearAds))
            {
                matchingType = typeof(VASTADWrapperCreativeNonLinearAds);
            }
            else if (type == typeof(VASTADInLineCreativeCompanionAds))
            {
                matchingType = typeof(VASTADWrapperCreativeCompanionAds);
            }
            else
            {
                return(null);
            }

            var appropriateWrapperCreatives = wrapperAd.Creatives.Where(c => c.Item.GetType() == matchingType);

            int index = appropriateCreatives.ToList().IndexOf(creative);

            if (appropriateWrapperCreatives.Count() > index)
            {
                return(appropriateWrapperCreatives.ElementAt(index));
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Merges the tracking info for the two ads
        /// </summary>
        private static void MergeWrappedAd(VASTADWrapper wrapperAd, VASTADInLine inlineAd)
        {
            // carry over impressions
            {
                var newItems = inlineAd.Impression.ToList();
                newItems.AddRange(wrapperAd.Impression.Select(i => new Impression_type() { Value = i }));
                inlineAd.Impression = newItems.ToArray();
            }
            // carry over tracking events
            foreach (var creative in inlineAd.Creatives)
            {
                var creativeItem = creative.Item;
                VASTADWrapperCreative creativeWrapper = FindMatchingCreative(inlineAd, creative, wrapperAd);
                if (creativeWrapper != null)
                {
                    if (creativeItem is VASTADInLineCreativeLinear)
                    {
                        var linearWrapper = (VASTADWrapperCreativeLinear)creativeWrapper.Item;
                        if (linearWrapper != null && linearWrapper.TrackingEvents != null)
                        {
                            var linear = (VASTADInLineCreativeLinear)creativeItem;
                            List<TrackingEvents_typeTracking> newItems;
                            if (linear.TrackingEvents == null)
                                newItems = new List<TrackingEvents_typeTracking>();
                            else
                                newItems = linear.TrackingEvents.ToList();
                            newItems.AddRange(linearWrapper.TrackingEvents);

                            linear.TrackingEvents = newItems.ToArray();
                        }
                    }
                    else if (creativeItem is VASTADInLineCreativeNonLinearAds)
                    {
                        var nonlinearWrapper = (VASTADWrapperCreativeNonLinearAds)creativeWrapper.Item;
                        if (nonlinearWrapper != null && nonlinearWrapper.TrackingEvents != null)
                        {
                            var nonLinear = (VASTADInLineCreativeNonLinearAds)creativeItem;
                            List<TrackingEvents_typeTracking> newItems;
                            if (nonLinear.TrackingEvents == null)
                                newItems = new List<TrackingEvents_typeTracking>();
                            else
                                newItems = nonLinear.TrackingEvents.ToList();
                            newItems.AddRange(nonlinearWrapper.TrackingEvents);

                            nonLinear.TrackingEvents = newItems.ToArray();
                        }
                    }
                    else if (creativeItem is VASTADInLineCreativeCompanionAds)
                    {
                        var companions = (VASTADInLineCreativeCompanionAds)creativeItem;
                        foreach (var companion in companions.Companion)
                        {
                            Companion_type companionWrapper = FindMatchingCompanion(companions, companion, (VASTADWrapperCreativeCompanionAds)creativeWrapper.Item);
                            if (companionWrapper != null && companionWrapper.TrackingEvents != null)
                            {
                                List<TrackingEvents_typeTracking> newItems;
                                if (companion.TrackingEvents == null)
                                    newItems = new List<TrackingEvents_typeTracking>();
                                else
                                    newItems = companion.TrackingEvents.ToList();
                                newItems.AddRange(companionWrapper.TrackingEvents);

                                companion.TrackingEvents = newItems.ToArray();
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Merges the tracking info for the two ads
        /// </summary>
        private static void MergeWrappedAd(VASTADWrapper wrapperAd, VASTADInLine inlineAd)
        {
            // carry over impressions
            {
                var newItems = inlineAd.Impression.ToList();
                newItems.AddRange(wrapperAd.Impression.Select(i => new Impression_type()
                {
                    Value = i
                }));
                inlineAd.Impression = newItems.ToArray();
            }
            // carry over tracking events
            foreach (var creative in inlineAd.Creatives)
            {
                var creativeItem = creative.Item;
                VASTADWrapperCreative creativeWrapper = FindMatchingCreative(inlineAd, creative, wrapperAd);
                if (creativeWrapper != null)
                {
                    if (creativeItem is VASTADInLineCreativeLinear)
                    {
                        var linearWrapper = (VASTADWrapperCreativeLinear)creativeWrapper.Item;
                        if (linearWrapper != null && linearWrapper.TrackingEvents != null)
                        {
                            var linear = (VASTADInLineCreativeLinear)creativeItem;
                            List <TrackingEvents_typeTracking> newItems;
                            if (linear.TrackingEvents == null)
                            {
                                newItems = new List <TrackingEvents_typeTracking>();
                            }
                            else
                            {
                                newItems = linear.TrackingEvents.ToList();
                            }
                            newItems.AddRange(linearWrapper.TrackingEvents);

                            linear.TrackingEvents = newItems.ToArray();
                        }
                    }
                    else if (creativeItem is VASTADInLineCreativeNonLinearAds)
                    {
                        var nonlinearWrapper = (VASTADWrapperCreativeNonLinearAds)creativeWrapper.Item;
                        if (nonlinearWrapper != null && nonlinearWrapper.TrackingEvents != null)
                        {
                            var nonLinear = (VASTADInLineCreativeNonLinearAds)creativeItem;
                            List <TrackingEvents_typeTracking> newItems;
                            if (nonLinear.TrackingEvents == null)
                            {
                                newItems = new List <TrackingEvents_typeTracking>();
                            }
                            else
                            {
                                newItems = nonLinear.TrackingEvents.ToList();
                            }
                            newItems.AddRange(nonlinearWrapper.TrackingEvents);

                            nonLinear.TrackingEvents = newItems.ToArray();
                        }
                    }
                    else if (creativeItem is VASTADInLineCreativeCompanionAds)
                    {
                        var companions = (VASTADInLineCreativeCompanionAds)creativeItem;
                        foreach (var companion in companions.Companion)
                        {
                            Companion_type companionWrapper = FindMatchingCompanion(companions, companion, (VASTADWrapperCreativeCompanionAds)creativeWrapper.Item);
                            if (companionWrapper != null && companionWrapper.TrackingEvents != null)
                            {
                                List <TrackingEvents_typeTracking> newItems;
                                if (companion.TrackingEvents == null)
                                {
                                    newItems = new List <TrackingEvents_typeTracking>();
                                }
                                else
                                {
                                    newItems = companion.TrackingEvents.ToList();
                                }
                                newItems.AddRange(companionWrapper.TrackingEvents);

                                companion.TrackingEvents = newItems.ToArray();
                            }
                        }
                    }
                }
            }
        }