Imports System.IO
Imports System.Runtime.Remoting

Public Class myService
    Private Sub myService_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        fillGrid(dgv_service, FileLoc_Service)
        StartPosition = FormStartPosition.CenterScreen

        txt_serviceID.BackColor = gray
        txt_productID.BackColor = gray
        txt_serviceName.BackColor = gray
        txt_price.BackColor = gray
        txt_product.BackColor = gray
        txt_time.BackColor = gray
        txt_serviceDesc.BackColor = gray

    End Sub

    Private Sub fillText(ByVal rowIndex As Integer)
        txt_serviceID.Text = dgv_service.Rows(rowIndex).Cells(0).Value
        txt_serviceName.Text = dgv_service.Rows(rowIndex).Cells(2).Value
        txt_productID.Text = dgv_service.Rows(rowIndex).Cells(1).Value
        txt_price.Text = dgv_service.Rows(rowIndex).Cells(3).Value
        txt_product.Text = FindField(fileLoc_Product, txt_productID.Text, 2)
        txt_time.Text = dgv_service.Rows(rowIndex).Cells(4).Value
        txt_serviceDesc.Text = dgv_service.Rows(rowIndex).Cells(5).Value
    End Sub

    Private Sub clearText()
        txt_serviceID.Text = ""
        txt_productID.Text = ""
        txt_serviceName.Text = ""
        txt_price.Text = ""
        txt_product.Text = ""
        txt_time.Text = ""
        txt_serviceDesc.Text = ""
    End Sub

    'Navigation
    Private dgvNavigator As New DataGridViewNavigator()
    Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
        dgvNavigator.SelectFirst(dgv_service)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
        dgvNavigator.SelectPrevious(dgv_service)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
        dgvNavigator.SelectNext(dgv_service)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
        dgvNavigator.SelectLast(dgv_service)
        fillText(dgvNavigator.CurrentIndex)
    End Sub
    '----------------------------------------------------------------------------------------------'
    Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
        clearText()
        Dim newID As Integer = AddRecord(FileLoc_Service)
        txt_serviceID.Text = newID
    End Sub

    Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click

        If Check() = False Then
            Return

        End If





        Dim serviceID As Integer = txt_serviceID.Text

        If UniqueCheck(serviceID, FileLoc_Service) Then
            Dim newservice As String = (txt_serviceID.Text + "," + txt_productID.Text + "," + txt_serviceName.Text + "," + txt_price.Text + "," + txt_time.Text + "," + txt_serviceDesc.Text)
            SaveRecord(FileLoc_Service, newservice)
            fillGrid(dgv_service, FileLoc_Service)
        Else
            MsgBox("ID is not unique.")
        End If
        'check presence of supplier ID in Supplier ID file
    End Sub

    Private Function Check()

        ' Presence check on the service ID textbox
        If txt_serviceID.Text = "" Then
            MsgBox("Please enter a service ID")
            Return False
        End If

        ' Checks that the ID entered is an integer
        If Not IntCheck(txt_serviceID.Text) Then
            MsgBox("Please enter a valid service ID")
            Return False
        End If

        ' Checks that the product ID is an integer
        If Not IntCheck(txt_productID.Text) Then
            MsgBox("Please enter a valid Product ID")
            Return False
        End If

        ' After checking that it is an integer, it is converted to a variable that is an integer, checking first avoids crashes
        Dim productID As Integer = txt_productID.Text
        If BinSearch(productID, fileLoc_Product, 0) = False Then
            MsgBox("invalid Product ID")
            Return False
        End If

        ' Checks the legnth of the services name, must be within the boundaries
        If txt_serviceName.Text.Length < 3 Or txt_serviceName.Text.Length > 20 Then
            MsgBox("Service name invalid, must be between 3 and 20 characters")
            Return False
        End If

        ' Checks that the price is an integer before attempting to convert
        IntCheck(txt_price.Text)
        Dim Price As Integer = txt_price.Text
        If Price < 0 Or Price > 200 Then
            MsgBox("Invalid Price, must be between 0 and 200 (pounds)")
            Return False
        End If

        ' Checks that the time is an integer before attempting to convert
        IntCheck(txt_time.Text)
        Dim Time As Integer = txt_time.Text
        If Time < 0 Or Time > 200 Then
            MsgBox("Service time invalid, must be between 0 and 200 (minutes)")
            Return False
        End If

        ' Sets a boundary for the lengths of service description
        If txt_serviceDesc.Text.Length < 0 Or txt_serviceDesc.Text.Length > 200 Then
            MsgBox("Invalid service description, must be between 0 and 200 characters")
            Return False
        End If

        Return True

    End Function




    Private Sub btn_edit_Click(sender As Object, e As EventArgs) Handles btn_edit.Click
        If Check() = False Then
            Return
        End If
        ' Checks new data before making adjustments
        Dim ID As Integer = txt_serviceID.Text
        Dim newValues As String = (txt_serviceID.Text + "," + txt_productID.Text + "," + txt_serviceName.Text + "," + txt_price.Text + "," + txt_time.Text + "," + txt_serviceDesc.Text)
        EditRecord(ID, newValues, FileLoc_Service)
        fillGrid(dgv_service, FileLoc_Service)
    End Sub

    Private Sub btn_delete_Click(sender As Object, e As EventArgs) Handles btn_delete.Click
        If IntCheck(txt_serviceID.Text) = False Then
            Return

        End If
        Dim serviceID As Integer = txt_serviceID.Text
        DeleteRecord(dgv_service, FileLoc_Service, serviceID)
        fillGrid(dgv_service, FileLoc_Service)
    End Sub




    Private Sub btn_menu_Click(sender As Object, e As EventArgs) Handles btn_menu.Click
        Me.Visible = False
        myMenu.Visible = True
    End Sub

    Private Sub btn_SerIdAsc_Click(sender As Object, e As EventArgs) Handles btn_SerIdAsc.Click
        AscSort(FileLoc_Service, 0)
        fillGrid(dgv_service, FileLoc_Service)
    End Sub

    Private Sub btn_SerIdDesc_Click(sender As Object, e As EventArgs) Handles btn_SerIdDesc.Click
        DescSort(FileLoc_Service, 0)
        fillGrid(dgv_service, FileLoc_Service)
    End Sub

    Private Sub btn_prodAsc_Click(sender As Object, e As EventArgs) Handles btn_prodAsc.Click
        AscSort(FileLoc_Service, 1)
        fillGrid(dgv_service, FileLoc_Service)
    End Sub

    Private Sub btn_prodDesc_Click(sender As Object, e As EventArgs) Handles btn_prodDesc.Click
        DescSort(FileLoc_Service, 1)
        fillGrid(dgv_service, FileLoc_Service)
    End Sub

    Private Sub btn_priAsc_Click(sender As Object, e As EventArgs) Handles btn_priAsc.Click
        AscSort(FileLoc_Service, 3)
        fillGrid(dgv_service, FileLoc_Service)
    End Sub

    Private Sub btn_priDesc_Click(sender As Object, e As EventArgs) Handles btn_priDesc.Click
        DescSort(FileLoc_Service, 3)
        fillGrid(dgv_service, FileLoc_Service)
    End Sub

    Private Sub btn_timeAsc_Click(sender As Object, e As EventArgs) Handles btn_timeAsc.Click
        AscSort(FileLoc_Service, 4)
        fillGrid(dgv_service, FileLoc_Service)
    End Sub

    Private Sub btn_timeDesc_Click(sender As Object, e As EventArgs) Handles btn_timeDesc.Click
        DescSort(FileLoc_Service, 4)
        fillGrid(dgv_service, FileLoc_Service)
    End Sub

    Private Sub txt_productID_TextChanged(sender As Object, e As EventArgs) Handles txt_productID.TextChanged
        txt_product.Text = FindField(fileLoc_Product, txt_productID.Text, 2)
    End Sub
End Class
