using System;
using System.Configuration;
using System.IO;
using ColorCode;

namespace SelectPdf.Samples
{
    public partial class Samples : System.Web.UI.MasterPage
    {
        // True on the home page (Default.aspx) — drives the card-grid (is-home)
        // layout and hides the side rail + the code panel.
        protected bool IsHome { get; private set; }

        // Set by converter-target pages (the convert-current-page capture and the
        // view-http-* echo pages) BEFORE render. When true the master emits an
        // inline PDF-reset <style> that flattens the screen-only sticky nav and
        // the viewport-tall scrollable rail to natural top-to-bottom flow, so the
        // SelectPdf engine paginates the page cleanly (otherwise position:sticky +
        // 100vh sizing scatter header/content/footer onto separate pages). NOT
        // wrapped in @media print — the Chromium engine renders with screen media.
        public bool FlattenForPdf { get; set; }

        // Public marketing site the chrome (nav menu, logo, footer) links at.
        // Env-specific via the "MarketingBaseUrl" appSetting in Web.config —
        // classic .NET Framework has no IWebHostEnvironment.IsStaging(), so the
        // staging deploy sets this appSetting to https://selectpdf.net; the
        // default (dev + production) is https://selectpdf.com. No trailing slash.
        protected string MarketingBaseUrl
        {
            get
            {
                string v = ConfigurationManager.AppSettings["MarketingBaseUrl"];
                if (string.IsNullOrEmpty(v)) v = "https://selectpdf.com";
                return v.TrimEnd('/');
            }
        }

        // Scheme-less host form for the footer's visible label
        // (selectpdf.com / selectpdf.net).
        protected string MarketingHost
        {
            get
            {
                return MarketingBaseUrl
                    .Replace("https://", string.Empty)
                    .Replace("http://", string.Empty)
                    .TrimEnd('/');
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // Robust home detection: the content page's app-relative path
            // (e.g. "~/Default.aspx" / "~/convert-url-to-pdf.aspx"), NOT the
            // request URL — so it works at the site root and under an IIS
            // sub-application alike.
            IsHome = (Page.AppRelativeVirtualPath ?? string.Empty)
                .ToLowerInvariant().EndsWith("default.aspx");

            // Home = card grid: no rail, no code panel.
            RailArea.Visible = !IsHome;
            if (IsHome)
            {
                LitCode.Visible = false;
                return;
            }

            // Inner page: the "Sample code · C#" panel with this page's own
            // .aspx.cs source, ColorCode-highlighted. NOTE: a published Web
            // Application Project compiles code-behind into the assembly and
            // does NOT deploy the .cs, so this is populated in the shipped
            // source layout / dev; it hides cleanly (catch) when absent.
            try
            {
                string srcPath = Server.MapPath(Page.AppRelativeVirtualPath) + ".cs";
                string code = File.ReadAllText(srcPath);
                string colorized = new CodeColorizer().Colorize(code, Languages.CSharp);
                LitCode.Text =
                    "<div class=\"spdf-code spdf-code-block\">" +
                    "<div class=\"spdf-code-head\">" +
                    "<span class=\"spdf-code-dots\"><span class=\"spdf-code-dot-red\"></span>" +
                    "<span class=\"spdf-code-dot-yellow\"></span><span class=\"spdf-code-dot-green\"></span></span>" +
                    "<span>Sample code &#183; C#</span>" +
                    "</div>" +
                    "<div class=\"spdf-code-pre\">" + colorized + "</div>" +
                    "</div>";
            }
            catch
            {
                LitCode.Visible = false;
            }
        }
    }
}
