Imports System.IO
Public Class mySupplier
    Private Sub mySupplier_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        fillGrid(dgv_supplier, fileLoc_Supplier)
        StartPosition = FormStartPosition.CenterScreen

        txt_supplierID.BackColor = gray
        txt_supplierName.BackColor = gray
        txt_nOPS.BackColor = gray
    End Sub

    Private Sub fillText(ByVal rowIndex As Integer)
        If dgv_supplier.Rows.Count = 0 Then
            Return
        End If
        txt_supplierID.Text = dgv_supplier.Rows(rowIndex).Cells(0).Value
        txt_supplierName.Text = dgv_supplier.Rows(rowIndex).Cells(1).Value
        txt_nOPS.Text = dgv_supplier.Rows(rowIndex).Cells(2).Value
    End Sub

    Private Sub clearText()
        txt_supplierID.Text = ""
        txt_supplierName.Text = ""
        txt_nOPS.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_supplier)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
        dgvNavigator.SelectPrevious(dgv_supplier)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
        dgvNavigator.SelectNext(dgv_supplier)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
        dgvNavigator.SelectLast(dgv_supplier)
        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_Supplier)
        txt_supplierID.Text = newID
    End Sub

    Private Sub btn_save_Click(sender As Object, e As EventArgs) Handles btn_save.Click
        ' Checks before attempting to add to file
        If Check() = False Then
            Return
        End If

        Dim supplierID As Integer = txt_supplierID.Text


        If UniqueCheck(supplierID, fileLoc_Supplier) Then
            Dim newSupplier As String = (txt_supplierID.Text + "," + txt_supplierName.Text + "," + txt_nOPS.Text)
            SaveRecord(fileLoc_Supplier, newSupplier)
            fillGrid(dgv_supplier, fileLoc_Supplier)
        Else
            MsgBox("ID is not unique.")
        End If

    End Sub


    Private Function Check()

        'Presence check on the supplier ID
        If txt_supplierID.Text = "" Then
            MsgBox("please enter a supplier ID")
            Return False
        End If

        ' Integer check on the ID
        If Not IntCheck(txt_supplierID.Text) Then
            Return False
        End If

        ' Length check (and presence) on the length of the supplier
        If txt_supplierName.Text.Length < 3 Or txt_supplierName.Text.Length > 20 Then
            MsgBox("Invalid Supplier name, must be between 3 and 20 characters")
            Return False
        End If

        ' Number of products supplied can either be blank or an integer
        If Not txt_nOPS.Text = "" Then
            If Not IntCheck(txt_nOPS.Text) Then
                Return False
            End If
        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 data to ensure validity
        Dim ID As Integer = txt_supplierID.Text
        Dim newValues As String = (txt_supplierID.Text + "," + txt_supplierName.Text + "," + txt_nOPS.Text)
        EditRecord(ID, newValues, fileLoc_Supplier)
        fillGrid(dgv_supplier, fileLoc_Supplier)


    End Sub

    Private Sub btn_delete_Click(sender As Object, e As EventArgs) Handles btn_delete.Click

        Dim supplierID As Integer = txt_supplierID.Text


        'Declares an array called lines, which contains every line of from the text file
        Dim lines As String() = File.ReadAllLines(fileLoc_Product)
        Dim productIDs As New List(Of String)

        Dim NOPS As Integer = 0

        ' Does not allow a supplier to be deleted if they supply products in the system
        ' Does this by

        ' Splits product records by the comma
        ' Checks part 1 (supplier ID)
        ' if exists and is integer, compares to the supplier that is attempting to be deleted
        ' IF MATCH
        ' Increment Number of products supplied
        ' Add product ID to a list
        ' IF NOT MATCH
        ' Check next product
        '
        ' Done until checked every product

        ' THEN
        ' either no products are supplied, the supplier is deleted
        ' OR 
        ' error message telling user how many products are supplied, which ID's are supplied by the supplier
        For Each line As String In lines
            Dim parts As String() = line.Split(","c)
            If Integer.TryParse(parts(1), Nothing) AndAlso Integer.Parse(parts(1)) = supplierID Then
                NOPS = NOPS + 1
                productIDs.Add(parts(0))
            End If
        Next

        If NOPS = 0 Then
            DeleteRecord(dgv_supplier, fileLoc_Supplier, supplierID)
            fillGrid(dgv_supplier, fileLoc_Supplier)
        Else
            MsgBox("This supplier supplies " & NOPS & " products, you must remove these products before deleting the supplier:" & String.Join(", ", productIDs))
        End If

    End Sub

    Private Sub btn_nOPS_Click(sender As Object, e As EventArgs) Handles btn_nOPS.Click
        Check()
        Dim supplierID As Integer = txt_supplierID.Text


        'Declares an array called lines, which contains every line of from the text file
        Dim lines As String() = File.ReadAllLines(fileLoc_Product)


        Dim NOPS As Integer = 0

        ' Checks each product in product file
        ' Splits by ,
        ' If supplier ID field corresponds to the queried supplier
        ' Increment Number of Products Supplied (NOPS)
        ' Edit record to display the NOPS

        For Each line As String In lines
            Dim parts As String() = line.Split(","c)
            If Integer.TryParse(parts(1), Nothing) AndAlso Integer.Parse(parts(1)) = supplierID Then
                NOPS = NOPS + 1
            End If
        Next

        txt_nOPS.Text = NOPS

        Dim ID As Integer = txt_supplierID.Text
        Dim newValues As String = (txt_supplierID.Text + "," + txt_supplierName.Text + "," + txt_nOPS.Text)
        EditRecord(ID, newValues, fileLoc_Supplier)
        fillGrid(dgv_supplier, fileLoc_Supplier)

    End Sub
    'Takes user to main menu
    Private Sub btn_menu_Click(sender As Object, e As EventArgs) Handles btn_menu.Click
        Me.Visible = False
        myMenu.Visible = True
    End Sub

    'Sorts

    ' Supplier ID Ascending
    ' Supplier ID Descending

    ' Number of products supplied Ascending
    ' Number of products supplied Descending


    Private Sub btn_suppIdAsc_Click(sender As Object, e As EventArgs) Handles btn_suppIdAsc.Click
        AscSort(fileLoc_Supplier, 0)
        fillGrid(dgv_supplier, fileLoc_Supplier)
    End Sub

    Private Sub btn_suppIdDesc_Click(sender As Object, e As EventArgs) Handles btn_suppIdDesc.Click
        DescSort(fileLoc_Supplier, 0)
        fillGrid(dgv_supplier, fileLoc_Supplier)
    End Sub

    Private Sub btn_NopsAsc_Click(sender As Object, e As EventArgs) Handles btn_NopsAsc.Click
        AscSort(fileLoc_Supplier, 2)
        fillGrid(dgv_supplier, fileLoc_Supplier)
    End Sub

    Private Sub btn_NopsDesc_Click(sender As Object, e As EventArgs) Handles btn_NopsDesc.Click
        DescSort(fileLoc_Supplier, 2)
        fillGrid(dgv_supplier, fileLoc_Supplier)
    End Sub


End Class