Imports System.Data.SqlClient
Imports System.Threading

Public Class StartApplication
    Inherits System.Web.UI.Page

    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            Dim contractId = Logic.Conversion.ToInteger(Request.Params("clientId"))
            Dim driverId = SessionVars.DriverId(Session)

            ' analytics
            analytics.LoadByContractId(contractId)

            ' If the driver is signed in and has an app for the contract already...
            Dim app = New Logic.DriverApplicationSummary(driverId, contractId)
            If app.id > 0 And {"incomplete", "submitted", "in review"}.Contains(app.status) Then
                Response.Redirect("application/Default.aspx?appId=" & app.id)
            End If
            If app.id > 0 And app.status = "transfer pending" Then
                Response.Redirect("../Default.aspx")
            End If

            If driverId = 0 Then
                phLoggedIn.Visible = False
                phGuest.Visible = True
            Else
                phLoggedIn.Visible = True
                phGuest.Visible = False
            End If

            If Not Page.IsPostBack Then
                Dim contract As New Logic.ServiceContract(contractId)
                txtCompany.Text = contract.customer.name
                txtCity.Text = contract.customer.city
                ddlState.SelectedValue = contract.customer.state
            End If

        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 btnCancel_Click() Handles btnCancel.Click, btnCancel2.Click
        Response.Redirect("Default.aspx")
    End Sub

    Private Sub btnSubmit_Click() Handles btnSubmit.Click
        Try

            Dim clientId = Logic.Conversion.ToInteger(Request.Params("clientId"))
            Dim driverId = SessionVars.DriverId(Session)
            Dim app = New Logic.DriverApplicationSummary(driverId, clientId)

            ' can happen if the session expires
            If driverId = 0 Then Exit Sub

            If app.id = 0 Or Not {"incomplete", "submitted", "in review"}.Contains(app.status) Then
                app.Create(createdBy:="[Driver]")
            End If

            Response.Redirect("application/Default.aspx?appId=" & app.id)

        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 btnNew_Click() Handles btnNew.Click
        SessionVars.PostLoginUrl(Session) = Request.Url.ToString()
        Response.Redirect("CreateAccount.aspx")
    End Sub

    Private Sub btnReturning_Click() Handles btnReturning.Click
        Dim returnAddress = Request.Url.ToString()
        Dim safeAddress = HttpUtility.UrlEncode(returnAddress)
        Response.Redirect("Login.aspx?r=" & safeAddress)
    End Sub

End Class