Imports System.Data
Imports System.Configuration.ConfigurationSettings

' written by David Carroll

Public Class ReadingPlan
    Inherits System.Web.UI.Page
    Protected WithEvents Tbl As System.Web.UI.WebControls.PlaceHolder
    Dim Start As Date

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Start = AppSettings("BibleStartDate")
        Dim dt As Date = Now
        If Request("date") <> "" Then dt = Request("date")
        If Request("Redirect") = 1 Then
            Response.Redirect(BibleGatewayUrl(dt.Subtract(Start).Days + 1))
            Exit Sub
        End If
        Tbl.Controls.Add(New ReadPlan(Start))
    End Sub

    Public Class ReadPlan
        Inherits Web.UI.Control
        Dim Start As Date

        Public Sub New(ByVal sd As Date)
            Start = sd
        End Sub

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            writer.WriteLine("<table border=1>")
            writer.WriteLine(vbTab & "<th>Date</th><th>Old Testament</th><th>New Testament</th><th>Psalms</th><th>Proverbs</th></tr>")
            For d As Integer = 1 To 365
                writer.Write(vbTab & "<td>")
                If Start.AddDays(d - 1) = Now.Date Then
                    writer.Write("<a name=""today""/>")
                End If
                writer.WriteLine("<a href=""" & BibleGatewayUrl(d).Replace(" ", "+") & """>" & Start.AddDays(d - 1) & "</a></td>")
                Dim dr As DataRow = Readings.Rows.Find(d)
                For Each pre As String In New String() {"ot", "nt", "ps", "pr"}
                    writer.WriteLine(vbTab & "<td>" & Verses(d, pre) & "</td>")
        End Sub
    End Class
End Class

Module Utility
    Public Bible As New DataSet
    Public Books As DataTable
    Public Readings As DataTable

    Sub InitBible(ByVal File As String)
        Books = Bible.Tables("Book")
        Books.PrimaryKey = New DataColumn() {Books.Columns("Ord")}

        Readings = Bible.Tables("Reading")
        Readings.PrimaryKey = New DataColumn() {Readings.Columns("Day")}
    End Sub

    Public Function BibleGatewayUrl(ByVal day As Integer) As String
        Dim dr As DataRow = Readings.Rows.Find(day)
        Return "" & _
                String.Format("{0};{1};{2};{3}", Verses(dr, "ot"), Verses(dr, "nt"), Verses(dr, "ps"), Verses(dr, "pr")) & _
    End Function

    Public Function Verses(ByVal dr As DataRow, ByVal pre As String) As String
        Dim b1 As String = Books.Rows.Find(dr(pre & "sb")).Item("Abbr")
        Dim b2 As String = Books.Rows.Find(dr(pre & "eb")).Item("Abbr")
        Dim ref As String
        If b1 = b2 Then ' starting verse and ending verse are in same book
            ref = String.Format("{0} {1}:{2}-{3}:{4}", _
                    b1, dr(pre & "sc"), dr(pre & "sv"), dr(pre & "ec"), dr(pre & "ev"))
        Else ' have to form two sets of verse ranges, since more than one book is involved
            ref = String.Format("{0} {1}:{2}-", b1, dr(pre & "sc"), dr(pre & "sv"))
            ref &= String.Format(";{0} 1:1-{1}:{2}", b2, dr(pre & "ec"), dr(pre & "ev"))
        End If
        Return ref
    End Function

    Public Function Verses(ByVal d As Integer, ByVal pre As String) As String
        Return Verses(Readings.Rows.Find(d), pre)
    End Function

End Module