Imports System.IO
Imports System.Net.Security
Imports System.Net.WebRequestMethods
Imports System.Runtime.Remoting
Imports System.Windows.Forms.LinkLabel
Imports File = System.IO.File

Public Class mytimetable
    Private Sub myStaff_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        StartPosition = FormStartPosition.CenterScreen
        fillGrid(dgv_timetable, fileLoc_timetable)

        ' Sets the colours of the items
        txt_timetableID.BackColor = gray
        DTP_timetableDate.BackColor = gray
        txt_shiftID1.BackColor = gray
        txt_shiftID2.BackColor = gray
        txt_shiftID3.BackColor = gray
        txt_shiftID4.BackColor = gray
        txt_shiftID5.BackColor = gray
        txt_staffID1.BackColor = gray
        txt_staffID2.BackColor = gray
        txt_staffID3.BackColor = gray
        txt_staffID4.BackColor = gray
        txt_staffID5.BackColor = gray
        txt_staffname1.BackColor = gray
        txt_staffname2.BackColor = gray
        txt_staffname3.BackColor = gray
        txt_staffname4.BackColor = gray
        txt_staffname5.BackColor = gray
        txt_start1.BackColor = gray
        txt_start2.BackColor = gray
        txt_start3.BackColor = gray
        txt_start4.BackColor = gray
        txt_start5.BackColor = gray
        txt_end1.BackColor = gray
        txt_end2.BackColor = gray
        txt_end3.BackColor = gray
        txt_end4.BackColor = gray
        txt_end5.BackColor = gray
    End Sub

    Private Sub fillText(ByVal rowIndex As Integer)
        txt_timetableID.Text = dgv_timetable.Rows(rowIndex).Cells(0).Value
        If Not dgv_timetable.Rows(rowIndex).Cells(1).Value = DateTime.MinValue Then
            ' avoids crashes by not putting empty date into DateTimePicker
            DTP_timetableDate.Value = dgv_timetable.Rows(rowIndex).Cells(1).Value
        End If

        ' Fills the text boxes

        txt_shiftID1.Text = dgv_timetable.Rows(rowIndex).Cells(2).Value
        txt_shiftID2.Text = dgv_timetable.Rows(rowIndex).Cells(3).Value
        txt_shiftID3.Text = dgv_timetable.Rows(rowIndex).Cells(4).Value
        txt_shiftID4.Text = dgv_timetable.Rows(rowIndex).Cells(5).Value
        txt_shiftID5.Text = dgv_timetable.Rows(rowIndex).Cells(6).Value
        txt_staffID1.Text = FindField(fileLoc_Shift, txt_shiftID1.Text, 2)
        txt_staffID2.Text = FindField(fileLoc_Shift, txt_shiftID2.Text, 2)
        txt_staffID3.Text = FindField(fileLoc_Shift, txt_shiftID3.Text, 2)
        txt_staffID4.Text = FindField(fileLoc_Shift, txt_shiftID4.Text, 2)
        txt_staffID5.Text = FindField(fileLoc_Shift, txt_shiftID5.Text, 2)
        txt_staffname1.Text = Decryption(FindField(fileLoc_Staff, txt_staffID1.Text, 1))
        txt_staffname2.Text = Decryption(FindField(fileLoc_Staff, txt_staffID2.Text, 1))
        txt_staffname3.Text = Decryption(FindField(fileLoc_Staff, txt_staffID3.Text, 1))
        txt_staffname4.Text = Decryption(FindField(fileLoc_Staff, txt_staffID4.Text, 1))
        txt_staffname5.Text = Decryption(FindField(fileLoc_Staff, txt_staffID5.Text, 1))
        txt_start1.Text = FindField(fileLoc_Shift, txt_shiftID1.Text, 3)
        txt_start2.Text = FindField(fileLoc_Shift, txt_shiftID2.Text, 3)
        txt_start3.Text = FindField(fileLoc_Shift, txt_shiftID3.Text, 3)
        txt_start4.Text = FindField(fileLoc_Shift, txt_shiftID4.Text, 3)
        txt_start5.Text = FindField(fileLoc_Shift, txt_shiftID5.Text, 3)
        txt_end1.Text = FindField(fileLoc_Shift, txt_shiftID1.Text, 4)
        txt_end2.Text = FindField(fileLoc_Shift, txt_shiftID2.Text, 4)
        txt_end3.Text = FindField(fileLoc_Shift, txt_shiftID3.Text, 4)
        txt_end4.Text = FindField(fileLoc_Shift, txt_shiftID4.Text, 4)
        txt_end5.Text = FindField(fileLoc_Shift, txt_shiftID5.Text, 4)



    End Sub




    Private Sub clearText()

        ' Clears text boxes

        txt_timetableID.Text = ""
        DTP_timetableDate.CustomFormat = " "
        txt_shiftID1.Text = ""
        txt_shiftID2.Text = ""
        txt_shiftID3.Text = ""
        txt_shiftID4.Text = ""
        txt_shiftID5.Text = ""
        txt_staffID1.Text = ""
        txt_staffID2.Text = ""
        txt_staffID3.Text = ""
        txt_staffID4.Text = ""
        txt_staffID5.Text = ""
        txt_staffname1.Text = ""
        txt_staffname2.Text = ""
        txt_staffname3.Text = ""
        txt_staffname4.Text = ""
        txt_staffname5.Text = ""
        txt_start1.Text = ""
        txt_start2.Text = ""
        txt_start3.Text = ""
        txt_start4.Text = ""
        txt_start5.Text = ""
        txt_end1.Text = ""
        txt_end2.Text = ""
        txt_end3.Text = ""
        txt_end4.Text = ""
        txt_end5.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_timetable)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
        dgvNavigator.SelectPrevious(dgv_timetable)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
        dgvNavigator.SelectNext(dgv_timetable)
        fillText(dgvNavigator.CurrentIndex)
    End Sub

    Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
        dgvNavigator.SelectLast(dgv_timetable)
        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_timetable)
        txt_timetableID.Text = newID

        Dim newshiftID As Integer = AddRecord(fileLoc_Shift)
        txt_shiftID1.Text = newshiftID
        txt_shiftID2.Text = newshiftID + 1
        txt_shiftID3.Text = newshiftID + 2
        txt_shiftID4.Text = newshiftID + 3
        txt_shiftID5.Text = newshiftID + 4

    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 timetableID As String = txt_timetableID.Text

        Dim newShift1 As String = txt_shiftID1.Text
        Dim newShift2 As String = txt_shiftID2.Text
        Dim newShift3 As String = txt_shiftID3.Text
        Dim newShift4 As String = txt_shiftID4.Text
        Dim newShift5 As String = txt_shiftID5.Text

        Dim StaffID1 As String = txt_staffID1.Text
        Dim StaffID2 As String = txt_staffID2.Text
        Dim StaffID3 As String = txt_staffID3.Text
        Dim StaffID4 As String = txt_staffID4.Text
        Dim StaffID5 As String = txt_staffID5.Text



        Dim shiftID1 As String = shiftCreate(newShift1, timetableID, StaffID1, txt_start1.Text, txt_end1.Text)
        Dim shiftID2 As String = shiftCreate(newShift2, timetableID, StaffID2, txt_start2.Text, txt_end2.Text)
        Dim shiftID3 As String = shiftCreate(newShift3, timetableID, StaffID3, txt_start3.Text, txt_end3.Text)
        Dim shiftID4 As String = shiftCreate(newShift4, timetableID, StaffID4, txt_start4.Text, txt_end4.Text)
        Dim shiftID5 As String = shiftCreate(newShift5, timetableID, StaffID5, txt_start5.Text, txt_end5.Text)



        If UniqueCheck(timetableID, fileLoc_timetable) Then
            Dim newtimetable As String = txt_timetableID.Text & "," & DTP_timetableDate.Value.Date & "," & newShift1 & "," & newShift2 & "," & newShift3 & "," & newShift4 & "," & newShift5
            SaveRecord(fileLoc_timetable, newtimetable)
            fillGrid(dgv_timetable, fileLoc_timetable)
        Else
            MsgBox("ID is not unique.")
        End If

    End Sub

    Private Function Check()
        If txt_timetableID.Text = "" Then
            MsgBox("please enter an timetable ID")
            Return False
        End If

        If IntCheck(txt_timetableID.Text) Then
        Else
            Return False
        End If

        If Not (IntCheck(txt_shiftID1.Text) AndAlso IntCheck(txt_shiftID2.Text) AndAlso IntCheck(txt_shiftID3.Text) AndAlso IntCheck(txt_shiftID4.Text) AndAlso IntCheck(txt_shiftID5.Text)) Then
            MsgBox("Please enter valid Shift IDs for all shifts")
            Return False
        End If


        Return True
    End Function







    Private Function shiftCreate(ByVal shiftID As String, ByVal timetableID As String, Optional staffID As String = "", Optional ShiftStart As String = "", Optional ShiftEnd As String = "")

        If shiftCheck(shiftID, timetableID, staffID, ShiftStart, ShiftEnd) = False Then
            Return False
        End If




        Dim newShift As String = shiftID & "," & timetableID & "," & staffID & "," & ShiftStart & "," & ShiftEnd
        SaveRecord(fileLoc_Shift, newShift)
        Return newShift

    End Function


    Private Function shiftCheck(ByVal shiftID As String, ByVal timetableID As String, Optional staffID As String = "", Optional ShiftStart As String = "", Optional ShiftEnd As String = "")


        If Not IntCheck(shiftID) Then
            Return False
        End If

        If Not IntCheck(timetableID) Then
            Return False
        End If

        If Not staffID = "" Then
            BinSearch(staffID, fileLoc_Staff, 0)
        End If



        If Not ShiftStart = "" AndAlso Not ShiftEnd = "" Then

            Dim partStart() As String = ShiftStart.Split(":")
            Dim partEnd() As String = ShiftEnd.Split(":")

            IntCheck(partStart(0))
            IntCheck(partStart(1))

            IntCheck(partEnd(0))
            IntCheck(partEnd(1))

            Dim hourStart As Integer = CInt(partStart(0))
            Dim minuteStart As Integer = CInt(partStart(1))
            If hourStart < 0 Or hourStart > 24 Then
                MsgBox("invalid Hour")
                Return False
            End If

            If minuteStart < 0 Or minuteStart > 60 Then
                MsgBox("invalid minute")
                Return False
            End If

            Dim hourEnd As Integer = CInt(partEnd(0))
            Dim minuteEnd As Integer = CInt(partEnd(1))
            If hourEnd < 0 Or hourEnd > 24 Then
                MsgBox("invalid Hour")
                Return False
            End If

            If minuteEnd < 0 Or minuteEnd > 60 Then
                MsgBox("invalid minute")
                Return False
            End If

            If hourStart > hourEnd Then
                Return False
            End If

            If hourStart = hourEnd Then
                If minuteStart > minuteEnd Then
                    Return False
                End If
            End If



        End If

        If Not staffID = "" Then
            If BinSearch(staffID, fileLoc_Staff, 0) = False 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

        Dim ID As Integer = txt_timetableID.Text
        Dim ServiceUse As Integer = FindField(fileLoc_timetable, txt_timetableID.Text, 8)
        Dim newValues As String = txt_timetableID.Text & "," & DTP_timetableDate.Value.Date & "," & txt_shiftID1.Text & "," & txt_shiftID2.Text & "," & txt_shiftID3.Text & "," & txt_shiftID4.Text & "," & txt_shiftID5.Text
        EditRecord(ID, newValues, fileLoc_timetable)
        fillGrid(dgv_timetable, fileLoc_timetable)


        Dim Shift1 As String = txt_shiftID1.Text
        Dim Shift2 As String = txt_shiftID2.Text
        Dim Shift3 As String = txt_shiftID3.Text
        Dim Shift4 As String = txt_shiftID4.Text
        Dim Shift5 As String = txt_shiftID5.Text

        Dim StaffID1 As String = txt_staffID1.Text
        Dim StaffID2 As String = txt_staffID2.Text
        Dim StaffID3 As String = txt_staffID3.Text
        Dim StaffID4 As String = txt_staffID4.Text
        Dim StaffID5 As String = txt_staffID5.Text



        shiftCheck(txt_shiftID1.Text, txt_timetableID.Text, StaffID1, txt_start1.Text, txt_end1.Text)
        Dim newShift1 As String = txt_shiftID1.Text & "," & txt_timetableID.Text & "," & StaffID1 & "," & txt_start1.Text & "," & txt_end1.Text
        EditRecord(txt_shiftID1.Text, newShift1, fileLoc_Shift)

        shiftCheck(txt_shiftID2.Text, txt_timetableID.Text, StaffID2, txt_start2.Text, txt_end2.Text)
        Dim newShift2 As String = txt_shiftID2.Text & "," & txt_timetableID.Text & "," & StaffID2 & "," & txt_start2.Text & "," & txt_end2.Text
        EditRecord(txt_shiftID2.Text, newShift2, fileLoc_Shift)

        shiftCheck(txt_shiftID3.Text, txt_timetableID.Text, StaffID3, txt_start3.Text, txt_end3.Text)
        Dim newShift3 As String = txt_shiftID3.Text & "," & txt_timetableID.Text & "," & StaffID3 & "," & txt_start3.Text & "," & txt_end3.Text
        EditRecord(txt_shiftID3.Text, newShift3, fileLoc_Shift)

        shiftCheck(txt_shiftID4.Text, txt_timetableID.Text, StaffID4, txt_start4.Text, txt_end4.Text)
        Dim newShift4 As String = txt_shiftID4.Text & "," & txt_timetableID.Text & "," & StaffID4 & "," & txt_start4.Text & "," & txt_end4.Text
        EditRecord(txt_shiftID4.Text, newShift4, fileLoc_Shift)

        shiftCheck(txt_shiftID5.Text, txt_timetableID.Text, StaffID5, txt_start5.Text, txt_end5.Text)
        Dim newShift5 As String = txt_shiftID5.Text & "," & txt_timetableID.Text & "," & StaffID5 & "," & txt_start5.Text & "," & txt_end5.Text
        EditRecord(txt_shiftID5.Text, newShift5, fileLoc_Shift)

    End Sub
    Private Sub btn_delete_Click(sender As Object, e As EventArgs) Handles btn_delete.Click
        If txt_timetableID.Text = "" Then
            Return
        End If
        Dim appID As Integer = txt_timetableID.Text
        DeleteRecord(dgv_timetable, fileLoc_timetable, appID)



        fillGrid(dgv_timetable, fileLoc_timetable)
    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





    ' Takes the when a box is clicked or ID is placed to put the staff name from the staff text file
    Private Sub txt_staffname1_Click(sender As Object, e As EventArgs) Handles txt_staffname1.Click
        txt_staffname1.Text = Decryption(FindField(fileLoc_Staff, txt_staffID1.Text, 1))
    End Sub

    Private Sub txt_staffname2_Click(sender As Object, e As EventArgs) Handles txt_staffname1.Click
        txt_staffname2.Text = Decryption(FindField(fileLoc_Staff, txt_staffID2.Text, 1))
    End Sub
    Private Sub txt_staffname3_Click(sender As Object, e As EventArgs) Handles txt_staffname1.Click
        txt_staffname3.Text = Decryption(FindField(fileLoc_Staff, txt_staffID3.Text, 1))
    End Sub
    Private Sub txt_staffname4_Click(sender As Object, e As EventArgs) Handles txt_staffname1.Click
        txt_staffname4.Text = Decryption(FindField(fileLoc_Staff, txt_staffID4.Text, 1))
    End Sub
    Private Sub txt_staffname5_Click(sender As Object, e As EventArgs) Handles txt_staffname1.Click
        txt_staffname5.Text = Decryption(FindField(fileLoc_Staff, txt_staffID5.Text, 1))
    End Sub

    Private Sub txt_staffID1_TextChanged(sender As Object, e As EventArgs) Handles txt_staffID1.TextChanged
        txt_staffname1.Text = Decryption(FindField(fileLoc_Staff, txt_staffID1.Text, 1))
    End Sub

    Private Sub txt_staffID2_TextChanged(sender As Object, e As EventArgs) Handles txt_staffID2.TextChanged
        txt_staffname2.Text = Decryption(FindField(fileLoc_Staff, txt_staffID2.Text, 1))
    End Sub

    Private Sub txt_staffID3_TextChanged(sender As Object, e As EventArgs) Handles txt_staffID3.TextChanged
        txt_staffname3.Text = Decryption(FindField(fileLoc_Staff, txt_staffID3.Text, 1))
    End Sub

    Private Sub txt_staffID4_TextChanged(sender As Object, e As EventArgs) Handles txt_staffID4.TextChanged
        txt_staffname4.Text = Decryption(FindField(fileLoc_Staff, txt_staffID4.Text, 1))
    End Sub

    Private Sub txt_staffID5_TextChanged(sender As Object, e As EventArgs) Handles txt_staffID5.TextChanged
        txt_staffname5.Text = Decryption(FindField(fileLoc_Staff, txt_staffID5.Text, 1))
    End Sub
End Class