using System;
using System.IO;
using System.Text;
using System.Xml;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;

namespace Aeroflot.Flights.Web.Pages
{
    [ResponseCache(CacheProfileName = "DefaultBoard")]
    [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:File name should match first type name", Justification = "Razor page model")]
    public class SitemapModel : PageModel
    {
        private static readonly XmlWriterSettings XmlWriterSettings = new XmlWriterSettings() { Encoding = Encoding.UTF8, Indent = true };
        private readonly IConfiguration configuration;
        private readonly bool allowIndexing;

        public SitemapModel(IConfiguration configuration)
        {
            this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
            this.allowIndexing = configuration.GetValue<bool>("AllowIndexing", false);
        }

        public HtmlString GetSitemap
        {
            get
            {
                var today = DateTime.Now.Date.ToString("yyyy-MM-dd");
                StringBuilder sb = new StringBuilder();
                using (var sw = new StringWriterWithEncoding(sb, Encoding.UTF8))
                using (XmlWriter writer = XmlWriter.Create(sw, XmlWriterSettings))
                {
                    writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
                    if (this.allowIndexing)
                    {
                        foreach (var lang in Models.Seo.LanguagePairs)
                        {
                            //obrd
                            writer.WriteStartElement("url");
                            writer.WriteElementString("loc", "https://" + HttpContext.Request.Host + $"/{lang}/onlineboard");
                            writer.WriteElementString("lastmod", today);
                            writer.WriteEndElement();

                            //schedule
                            writer.WriteStartElement("url");
                            writer.WriteElementString("loc", "https://" + HttpContext.Request.Host + $"/{lang}/schedule");
                            writer.WriteElementString("lastmod", today);
                            writer.WriteEndElement();
                        }
                    }

                    writer.WriteEndElement();
                    writer.Flush();
                }

                return new HtmlString(sb.ToString());
            }
        }

        public void OnGet()
        {
        }

        public class StringWriterWithEncoding : StringWriter
        {
            private readonly Encoding encoding;

            public StringWriterWithEncoding(StringBuilder sb, Encoding encoding)
                : base(sb)
            {
                this.encoding = encoding;
            }

            public override Encoding Encoding
            {
                get
                {
                    return this.encoding;
                }
            }
        }
    }
}
