using System;
using System.Configuration;
using System.Web;
using System.Web.Mvc;

namespace SelectPdf.Samples.Views.Shared
{
    // MVC view master page (the .aspx views set MasterPageFile to this). Inheriting
    // ViewMasterPage gives the markup + these helpers access to ViewContext / ViewData.
    public partial class Samples : System.Web.Mvc.ViewMasterPage
    {
        // Public marketing site the chrome (nav/logo/footer) links at — env-driven
        // via the Web.config "MarketingBaseUrl" appSetting (staging deploy sets it
        // to selectpdf.net; default selectpdf.com). No trailing slash.
        public string MarketingBaseUrl
        {
            get
            {
                string m = ConfigurationManager.AppSettings["MarketingBaseUrl"];
                if (string.IsNullOrEmpty(m)) m = "https://selectpdf.com";
                return m.TrimEnd('/');
            }
        }

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

        // Converter-target controllers set ViewBag.FlattenForPdf = true; the master
        // then emits the paged-media flatten <style> so the SelectPdf PDF paginates
        // cleanly (flattens the screen-only sticky nav + scrollable rail).
        public bool FlattenForPdf
        {
            get
            {
                object v = ViewData["FlattenForPdf"];
                return v != null && Convert.ToBoolean(v);
            }
        }

        public string CurrentController
        {
            get
            {
                object c = ViewContext.RouteData.Values["controller"];
                return c == null ? string.Empty : c.ToString();
            }
        }

        public bool IsHome
        {
            get { return string.Equals(CurrentController, "Home", StringComparison.OrdinalIgnoreCase); }
        }

        // "Sample code · C#" panel — reads the matching Controller source off disk
        // (controller action source, where the SelectPdf conversion code lives).
        // This minimal MVC4 sample does not reference ColorCode, so the source is
        // HTML-encoded plain text in the styled panel (chrome + dark-mode invert
        // still apply). Returns "" (panel hidden) on the home page or any error.
        public string CodeHtml
        {
            get
            {
                if (IsHome) return string.Empty;
                try
                {
                    string path = HttpContext.Current.Server.MapPath(
                        "~/Controllers/" + CurrentController + "Controller.cs");
                    if (!System.IO.File.Exists(path)) return string.Empty;
                    string code = System.IO.File.ReadAllText(path);
                    string encoded = System.Web.HttpUtility.HtmlEncode(code);
                    return
                        "<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\"><pre style=\"margin:0\">" + encoded + "</pre></div>" +
                        "</div>";
                }
                catch
                {
                    return string.Empty;
                }
            }
        }
    }
}
