Imports System.IO
Imports System.Threading

Public Class Application2
    Inherits System.Web.UI.MasterPage

#Region "Properties"

    Property PercentComplete As String = "0% Complete"

    Property app As Logic.DriverApplicationSummary

    Property CurrentDriver As Logic.DriverAccount
    Property CurrentPage As String
    ReadOnly Property CurrentTitle As String
        Get
            Return nav.GetCurrentTitle()
        End Get
    End Property

#End Region

#Region "Methods"

    Sub EnableNextPage()
        Dim nextItem = nav.GetNext()
        If nextItem.status = SideNavItem.ItemStatus.Disabled Then
            app.currentFile = nextItem.target
            app.Update(modifiedBy:="[Driver]")
        End If
    End Sub

    Function GetFirstPage() As SideNav.MenuItem
        Return nav.GetFirst()
    End Function

    Sub GotoNextPage()
        Response.Redirect(nav.GetNext().target & "?appId=" & app.id)
    End Sub

#End Region

#Region "Event Handlers"

    ' in here, we'll load app and verify we can have it loaded
    ' also, we've gotta set a few master vars
    Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        Try

            ' set data from master
            CurrentDriver = Master.CurrentDriver
            CurrentPage = Master.CurrentPage

            ' load app
            Dim appId = Logic.Conversion.ToInteger(Request.Params("appId"))
            If appId = 0 Then
                Response.Redirect("../Dashboard2.aspx")
            End If
            app = New Logic.DriverApplicationSummary(appId)

            ' analytics
            analytics.LoadByAppId(appId)

            ' make sure we can have it open
            If app.driverAccountId <> SessionVars.DriverId(Session) Then
                Response.Redirect("../Login2.aspx")
                Throw New ArgumentOutOfRangeException("appId", "Cannot find application.")
            End If
            If Not app.canDriverEdit Then
                Response.Redirect("../")
            End If
            If app.transferId > 0 Then
                ' 20190514 - can't get here per other code, but just in case:
                Response.Redirect("../transfer/TransferApplication.aspx?xferId=" & app.transferId)
            End If

            ' Load our navigation
            If Not Page.IsPostBack Then

                Dim links = {
                    {"Instructions", "Instructions.aspx", True},
                    {"Personal Information", "PersonalInformation.aspx", True},
                    {"Address Info", "AddressInfo.aspx", True},
                    {"Education Info", "EducationInfo.aspx", True},
                    {"General Questions", "GeneralQuestions.aspx", True},
                    {"Licenses", "Licenses.aspx", True},
                    {"Driving Experience", "DrivingExperience.aspx", True},
                    {"Driver Training", "DriverTraining.aspx", True},
                    {"Unemployment", "Unemployment.aspx", True},
                    {"Employment History", "EmploymentHistory.aspx", True},
                    {"FMCSR", "FMCSR.aspx", True},
                    {"Traffic Violations", "TrafficViolations.aspx", True},
                    {"Accidents", "Accidents.aspx", True},
                    {"Conviction Record", "ConvictionRecord.aspx", True},
                    {"Request for Check of Driving Record", "AgreementDrivingRecord.aspx", True},
                    {"Previous Pre-Employment Controlled Substances or Alcohol Test Disclosure", "AgreementControlledSubstance.aspx", True},
                    {"Controlled Substances & Alcohol Testing Consent Form and Application Certification", "AgreementControlledSubstanceTest.aspx", True},
                    {"Drug & Alcohol Clearinghouse Request Consent", "AgreementDACH.aspx", app.includeDach},
                    {"Important Disclosure Regarding Background Reports from the PSP Online Service", "AgreementPSP.aspx", app.includePSP},
                    {"Criminal Background Check Release", "AgreementCBC.aspx", app.includeCBC},
                    {"Application Certification", "ApplicationCertification.aspx", True}
                }

                '                    {"Certificate of Compliance with Driver License Requirements", "ComplianceCertificate.aspx", True},

                '                                    {"Current Driver's License", "CurrentLicense.aspx", True},
                '                    {"Current Medical Certificate", "MedicalRegistry.aspx", True},

                ''                                    {"Safety Performance History Investigation", "SafetyHistory.aspx", True},
                '                {"Certificate of Violations", "ViolationsCertificate.aspx", True},
                '                    {"Driver Statement of On-Duty Hours", "OnDutyHours.aspx", True},

                ' If currentFile is not defined, then it is the first link
                If String.IsNullOrEmpty(app.currentFile) Then
                    app.currentFile = links(0, 1)
                End If

                ' fill in our links
                Dim status = SideNavItem.ItemStatus.Complete
                For i = 0 To UBound(links)
                    Dim title As String = links(i, 0)
                    Dim url As String = links(i, 1)
                    Dim include As Boolean = links(i, 2)

                    If Not include Then
                        Continue For
                    End If

                    ' complete to current to disabled
                    If url = app.currentFile Then
                        status = SideNavItem.ItemStatus.Current
                    ElseIf status = SideNavItem.ItemStatus.Current Then
                        status = SideNavItem.ItemStatus.Disabled
                    End If

                    ' add in the menu item
                    nav.Add(title, url, status)

                    ' make sure we have gotten to this page -- no skipping!
                    If url.ToLower() = CurrentPage And status = SideNavItem.ItemStatus.Disabled Then
                        Response.Redirect("Default.aspx")

                    End If

                Next

                ' change buttons up depending on state
                If CurrentPage = links(0, 1).ToLower() Then
                    btnBack.Visible = False
                ElseIf CurrentPage = links(UBound(links), 1).ToLower() Then
                    btnNext.Text = If(app.status = "incomplete", "Submit", "Save")
                    btnNext2.Text = If(app.status = "incomplete", "Submit", "Save")
                ElseIf CurrentPage = "submit.aspx" Then
                    btnBack.Visible = False
                    btnNext.Visible = False
                End If

            End If

            PercentComplete = nav.GetPercent

        Catch ex As ThreadAbortException
            ' nop
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try

    End Sub

    Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        Response.Redirect(nav.GetPrevious().target & "?appId=" & app.id)
    End Sub

    Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click, btnNext2.Click
        If Not Page.IsValid Then
            Exit Sub
        End If
        GotoNextPage()
    End Sub

    Private Sub nav_LinkClicked(item As SideNav.MenuItem) Handles nav.LinkClicked
        Response.Redirect(item.target & "?appId=" & app.id)
    End Sub

#End Region

End Class