﻿using System.Collections.Generic;
using System.Xml.Serialization;

namespace SimpleMvcSitemap.Serialization
{
    class XmlNamespaceBuilder : IXmlNamespaceBuilder
    {
        private readonly IDictionary<string, string> prefixList;

        public XmlNamespaceBuilder()
        {
            prefixList = new Dictionary<string, string>
            {
                { XmlNamespaces.Sitemap, XmlNamespaces.SitemapPrefix },
                { XmlNamespaces.Image, XmlNamespaces.ImagePrefix },
                { XmlNamespaces.News, XmlNamespaces.NewsPrefix},
                { XmlNamespaces.Video, XmlNamespaces.VideoPrefix},
                { XmlNamespaces.Xhtml, XmlNamespaces.XhtmlPrefix}
            };
        }

        public XmlSerializerNamespaces Create(IEnumerable<string> namespaces)
        {
            XmlSerializerNamespaces result = new XmlSerializerNamespaces();
            result.Add(XmlNamespaces.SitemapPrefix, XmlNamespaces.Sitemap);

            foreach (var ns in namespaces)
            {
                string prefix;
                if (prefixList.TryGetValue(ns, out prefix))
                {
                    result.Add(prefix, ns);
                }
            }

            return result;
        }
    }
}