Imports System.Threading

Public Class Transfer
    Inherits System.Web.UI.MasterPage

    Property transfer As Logic.DriverMoveApplicationSummary
    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


    Sub EnableNextPage()
        Dim nextItem = nav.GetNext()
        If nextItem.status = SideNavItem.ItemStatus.Disabled Then
            transfer.currentPage = nextItem.target
            transfer.Update()
        End If
    End Sub

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

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


    Private Sub Page_Init() Handles Me.Init
        Try
            CurrentDriver = Master.CurrentDriver
            CurrentPage = Master.CurrentPage

            Dim xferId = Logic.Conversion.ToInteger(Request.Params("xferId"))
            If xferId = 0 Then Response.Redirect("../")
            transfer = New Logic.DriverMoveApplicationSummary(xferId)

            ' analytics
            analytics.LoadByContractId(transfer.targetId)

            ' Make sure we can have it open
            If transfer.isComplete Then
                Response.Redirect("../")
            End If
            If transfer.driverAccountId <> SessionVars.DriverId(Session) Then
                Throw New ArgumentOutOfRangeException("xferId", "Cannot find transfer application.")
            End If

            Dim appId = Logic.Conversion.ToInteger(transfer.appId)
            If appId = 0 Then Response.Redirect("../")
            app = New Logic.DriverApplicationSummary(appId)

            ' make sure we can have it open
            If app.driverAccountId <> SessionVars.DriverId(Session) Then
                Throw New ArgumentOutOfRangeException("appId", "Cannot find application.")
            End If

            ' Load our navigation
            If Not Page.IsPostBack Then

                Dim links = {
                    {"Safety Performance History Investigation", "SafetyHistory.aspx"},
                    {"Request for Check of Driving Record", "CheckDrivingRecord.aspx"}}

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

                ' Add our links to the navigation
                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)

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

                    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 = "Submit"
                    btnNext2.Text = "Submit"
                ElseIf CurrentPage = "submit.aspx" Then
                    btnBack.Visible = False
                    btnNext.Visible = False
                End If

            End If

        Catch ex As ThreadAbortException
            Throw
        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 & "?xferId=" & transfer.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 & "?xferId=" & transfer.id)
    End Sub

End Class