Imports System.Data.SqlClient

Public Class DeleteApplication
    Inherits System.Web.UI.Page

    ' Normally, we are called from JS and should perform our work and immediately return.
    ' If the 'return' parameter is missing, then we got here via the 'delete' link and JS is disabled.
    Private Sub Me_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim appId = Logic.Conversion.ToInteger(Request.Params("id"))

        Dim contractId = 0
        Using connection As New SqlConnection(Logic.Database.connectionString)
            connection.Open()
            Dim cmd As New SqlCommand("SELECT ClientId FROM web_DriverApplication WHERE id=@appId", connection)
            cmd.Parameters.AddWithValue("appId", appId)
            contractId = cmd.ExecuteScalar()
        End Using
        Master.Master.CurrentClient.VerifyContractAccess(contractId)
            
        Dim perms = Master.Master.CurrentClient.Permissions
        If Not perms.Contains("View Applications Page") Then Throw New HttpException(403, "Forbidden")

        Dim returnUri = Request.Params("return")
        If Not String.IsNullOrEmpty(returnUri) Then
            _LoadForm(appId)
            Master.SetParentPage("Applications.aspx")
        Else
            _Delete(appId)
            _SafeRedirect(returnUri)
        End If

    End Sub

    ' Cancel -- only clicked if no JavaScript
    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Response.Redirect("Applications.aspx")
    End Sub

    ' Okay -- only clicked if no JavaScript
    Private Sub btnOkay_Click(sender As Object, e As EventArgs) Handles btnOkay.Click
        _Delete(litId.Text)
        Response.Redirect("Applications.aspx")
    End Sub

#Region "Internals"

    Private Sub _Delete(appId As Integer)
        Try
            Dim app = New Logic.DriverApplicationSummary(appId)
            app.Delete(deletedBy:="[Client]")
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
    End Sub

    Private Sub _LoadForm(appId As Integer)
        Try
            With New Logic.DriverApplicationSummary(appId)
                litId.Text = .id
                litTitle.Text = .applicantName
                litSubmitted.Text = .dateSubmitted
            End With
        Catch ex As Exception
            Dim errorId = Logic.Error.Log(ex)
            Response.Redirect("/Error.aspx?id=" & errorId)
        End Try
    End Sub

    Private Sub _SafeRedirect(destination As String)

        ' Magic...  true if other is same origin as this website
        Dim isSameOrigin = Function(other As String) As Boolean
                               Return 0 = Uri.Compare(Request.Url, New Uri(other),
                                   UriComponents.SchemeAndServer, UriFormat.SafeUnescaped, StringComparison.Ordinal)
                           End Function

        ' Redirect, if valid
        If destination.Length AndAlso isSameOrigin(destination) Then
            Response.Redirect(destination)
        End If

        ' On fail, just return to Applications
        Response.Redirect("Applications.aspx")

    End Sub

#End Region

End Class