Imports System
Imports System.Configuration
Imports System.Web
Imports System.Web.Mvc

' MVC view master page (the .aspx views set MasterPageFile to this). Inheriting
' ViewMasterPage gives the markup + these helpers access to ViewContext / ViewData.
Public Class Samples
    Inherits 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 ReadOnly Property MarketingBaseUrl As String
        Get
            Dim m As String = ConfigurationManager.AppSettings("MarketingBaseUrl")
            If String.IsNullOrEmpty(m) Then m = "https://selectpdf.com"
            Return m.TrimEnd("/"c)
        End Get
    End Property

    ' Scheme-less host for the footer's visible label.
    Public ReadOnly Property MarketingHost As String
        Get
            Return MarketingBaseUrl.Replace("https://", "").Replace("http://", "").TrimEnd("/"c)
        End Get
    End Property

    ' Converter-target controllers set ViewBag.FlattenForPdf = True; the master then
    ' emits the paged-media flatten <style> so the SelectPdf PDF paginates cleanly.
    Public ReadOnly Property FlattenForPdf As Boolean
        Get
            Dim v As Object = ViewData("FlattenForPdf")
            Return v IsNot Nothing AndAlso Convert.ToBoolean(v)
        End Get
    End Property

    Public ReadOnly Property CurrentController As String
        Get
            Dim c As Object = ViewContext.RouteData.Values("controller")
            If c Is Nothing Then Return String.Empty
            Return c.ToString()
        End Get
    End Property

    Public ReadOnly Property IsHome As Boolean
        Get
            Return String.Equals(CurrentController, "Home", StringComparison.OrdinalIgnoreCase)
        End Get
    End Property

    ' "Sample code · VB.NET" panel — reads the matching Controller source off disk
    ' (controller action source). 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). "" (hidden) on home or any error.
    Public ReadOnly Property CodeHtml As String
        Get
            If IsHome Then Return String.Empty
            Try
                Dim path As String = HttpContext.Current.Server.MapPath(
                    "~/Controllers/" & CurrentController & "Controller.vb")
                If Not System.IO.File.Exists(path) Then Return String.Empty
                Dim code As String = System.IO.File.ReadAllText(path)
                Dim encoded As String = 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; VB.NET</span></div><div class=""spdf-code-pre""><pre style=""margin:0"">" & encoded & "</pre></div></div>"
            Catch
                Return String.Empty
            End Try
        End Get
    End Property

End Class
