using System.Text;
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 RobotsModel : PageModel
    {
        private readonly IConfiguration configuration;
        private readonly bool allowIndexing;

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

        public void OnGet()
        {
        }

        public HtmlString RobotsTxtRules
        {
            get
            {
                StringBuilder sb = new StringBuilder();

                if (!this.allowIndexing)
                {
                    // disallow all
                    sb.AppendLine("User-agent: *");
                    sb.AppendLine("Disallow: /");
                    sb.AppendLine();
                }
                else
                {
                    foreach (var agent in Models.Seo.UserAgents)
                    {
                        sb.AppendLine($"User-agent: {agent}");
                        sb.AppendLine("#obrd");
                        foreach (var lang in Models.Seo.LanguagePairs)
                        {
                            sb.AppendLine($"Allow: /{lang}/onlineboard");
                        }

                        sb.AppendLine("#schd");
                        foreach (var lang in Models.Seo.LanguagePairs)
                        {
                            sb.AppendLine($"Allow: /{lang}/schedule");
                        }

                        sb.AppendLine("#errors");
                        foreach (var lang in Models.Seo.LanguagePairs)
                        {
                            sb.AppendLine($"Disallow: /{lang}/error/*");
                        }

                        sb.AppendLine();
                    }

                    sb.AppendLine();
                }

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

        public string SitemapLink => "https://" + HttpContext.Request.Host + "/sitemap.xml";
    }
}
