Imports System.Web
Imports System.Web.Services
Imports Newtonsoft.Json

Public Class TaskHandler
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim command = context.Request.RequestType.ToLower
        If command = "get" Then
            ReadTask(context)
        ElseIf command = "put" Then
            UpdateTask(context)
        ElseIf command = "post" Then
            AddTask(context)
        ElseIf command = "delete" Then
            DeleteTask(context)
        Else
            Throw New Exception("unknown request type")
        End If
    End Sub
  
    Private Sub DeleteTask(context As HttpContext)
        Dim deletedTasks = JsonConvert.DeserializeObject(Of List(Of Models.Task))(context.Request.Params("tasks"))

        For Each deleted As Models.Task In deletedTasks
            Dim existing = Models.Database.TaskData.First(Function(t) t.Id = deleted.Id)
            If existing IsNot Nothing Then
                Models.Database.TaskData.Remove(existing)
            End If
        Next

        Dim jsonData = JsonConvert.SerializeObject(Nothing)
        context.Response.ContentType = "text/json"
        context.Response.Write(jsonData)
    End Sub
  
    Private Sub AddTask(context As HttpContext)
        Dim newTasks = JsonConvert.DeserializeObject(Of List(Of Models.Task))(context.Request.Params("tasks"))
        For Each newTask As Models.Task In newTasks
            Dim largestId = Models.Database.TaskData.OrderByDescending(Function(t) t.Id).First().Id
            newTask.Id = largestId + 1
            Models.Database.TaskData.Add(newTask)
        Next

        Dim jsonData = JsonConvert.SerializeObject(newTasks)
        context.Response.ContentType = "text/json"
        context.Response.Write(jsonData)
    End Sub

    Private Sub UpdateTask(ByVal context As HttpContext)
        Dim updatedTasks = JsonConvert.DeserializeObject(Of List(Of Models.Task))(context.Request.Params("tasks"))
        Dim existingTasks = New List(Of Models.Task)()
        For Each updated As Models.Task In updatedTasks
            Dim existing = Models.Database.TaskData.First(Function(t) t.Id = updated.Id)
            existing.Description = updated.Description
            existing.IsComplete = updated.IsComplete
            existing.CreatedOn = updated.CreatedOn
            existing.Priority = updated.Priority
            existing.Id = updated.Id
            existingTasks.Add(existing)
        Next

        Dim jsonData = JsonConvert.SerializeObject(existingTasks)
        context.Response.ContentType = "text/json"
        context.Response.Write(jsonData)
    End Sub

    Private Sub ReadTask(ByVal context As HttpContext)
        Dim taskID = -1
        Integer.TryParse(context.Request("taskID"), taskID)

        Dim jsonData = JsonConvert.SerializeObject(Models.Database.TaskData)
        context.Response.ContentType = "text/json"
        context.Response.Write(jsonData)
    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class