Imports XCCLibrary
Imports Excel = Microsoft.Office.Interop.Excel

Public Class GUI
    Public Shared filepath As String = "C:\Users\Anton\Documents\Exjobb"
    'Public Shared filepath As String = "C:\Users\xperd\Documents"

    Dim excelApp As Excel.Application = New Excel.Application
    Dim excelWB As Excel.Workbook

    Public Shared dataBase As New Dictionary(Of String, DataTable)

    ' --- Start method (main) for GUI ---
    Sub GUI_load() Handles MyBase.Load
        Me.Width = 1400
        Me.Height = 800

        AddHandler DrawingPanel.Paint, AddressOf GUI_Drawing_Panel.DrawingPanel_Paint
        GUI_Drawing_Panel.Load_DrawingPanel()

        adminClass.loadSettings(GUI_Settings.filesFolder, GUI_Settings.HLCtFolder, "SolidWorks")

        Get_Database()

        CheckBox_WholeMeshWidths.Checked = True

        GUI_Functions.pointsMeasurements.Add("Lmes", {1, 2, 1})
        GUI_Functions.pointsMeasurements.Add("Wmes", {2, 3, 2})
    End Sub

    Private Sub Get_Database()
        'Make csv files of each sheet in excel
        Dim excelApp As Excel.Application = New Excel.Application
        Dim excelWB As Excel.Workbook
        excelApp.DisplayAlerts = False
        Dim wSNames As New List(Of String)
        excelWB = excelApp.Workbooks.Open(GUI_Settings.HLCtFolder & "\Database\Databas.xlsx")

        For i = 1 To 4
            Dim activeSheet As Excel.Worksheet
            activeSheet = excelWB.Sheets(i)
            wSNames.Add(activeSheet.Name)
            activeSheet.SaveAs(GUI_Settings.HLCtFolder & "\Database\" & wSNames(i - 1), Excel.XlFileFormat.xlCSV)

            System.Runtime.InteropServices.Marshal.ReleaseComObject(activeSheet)
        Next
        excelWB.Close()
        excelApp.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excelWB)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp)

        'For Each WS In excelWB.Worksheets
        '    WS.SaveAs(GUI_Settings.HLCtFolder & ("\") & WS.Name, Excel.XlFileFormat.xlCSV)
        'Next

        'Read all CSV files to datatables
        For i = 0 To wSNames.Count - 1
            Dim lines = IO.File.ReadAllLines(GUI_Settings.HLCtFolder & "\Database\" & wSNames(i) & ".csv")
            Dim tempDT As New DataTable
            Dim words As String() = lines(0).Split(New Char() {","c})

            For j = 0 To words.Count - 1
                Dim colName As String = words(j).Split("[")(0)
                colName = colName.Substring(0, colName.Length - 1)
                Dim type As String = words(j).Split("[")(1).Split("]")(0)

                If type = "str" Then
                    tempDT.Columns.Add(colName, GetType(String))
                ElseIf type = "int" Then
                    tempDT.Columns.Add(colName, GetType(Integer))
                ElseIf type = "doub" Then
                    tempDT.Columns.Add(colName, GetType(String))    'FIXA
                Else
                    tempDT.Columns.Add(colName, GetType(Boolean))
                End If
            Next

            For j = 1 To lines.Count - 1
                Dim values As String() = lines(j).Split(New Char() {","c})
                tempDT.Rows.Add()
                For k = 0 To values.Count - 1
                    Try
                        tempDT.Rows(tempDT.Rows.Count - 1)(k) = values(k)
                    Catch ex As Exception
                        If values(k) <> "" AndAlso values(k) = 0 Then
                            tempDT.Rows(tempDT.Rows.Count - 1)(k) = False
                        ElseIf values(k) <> "" AndAlso values(k) = 1 Then
                            tempDT.Rows(tempDT.Rows.Count - 1)(k) = True
                        End If
                    End Try
                Next
            Next
            dataBase.Add(wSNames(i), tempDT)
        Next
    End Sub

    Public Sub Create_TextBox(TextBoxName As String, TextBoxText As String, TextBoxLeft As Integer, TextBoxTop As Integer,
                              TextBoxWidth As Integer)
        Dim textBoxTemp As New TextBox
        textBoxTemp.Name = TextBoxName
        textBoxTemp.Text = TextBoxText
        textBoxTemp.Left = TextBoxLeft
        textBoxTemp.Top = TextBoxTop
        textBoxTemp.Width = TextBoxWidth
        textBoxTemp.Font = New Font("Microsoft Sans Serif", 10)
        Me.Controls.Add(textBoxTemp)
    End Sub

    Public Sub Create_LabelGUI(LabelName As String, LabelText As String, LabelLeft As Integer, LabelTop As Integer)
        Dim LabelTemp As New Label
        LabelTemp.Name = LabelName
        LabelTemp.Text = LabelText
        LabelTemp.Left = LabelLeft
        LabelTemp.Top = LabelTop
        LabelTemp.Font = New Font("Microsoft Sans Serif", 10)
        Me.Controls.Add(LabelTemp)
    End Sub

    Public Sub Create_LabelPanel(LabelName As String, LabelText As String, LabelLeft As Integer, LabelTop As Integer)
        Dim LabelTemp As New Label
        LabelTemp.Name = LabelName
        LabelTemp.Text = LabelText
        LabelTemp.Left = LabelLeft
        LabelTemp.Top = LabelTop
        LabelTemp.Font = New Font("Microsoft Sans Serif", 7)
        LabelTemp.BackColor = Color.Transparent
        DrawingPanel.Controls.Add(LabelTemp)
    End Sub

    ' ---------------------------------- Gratings Data ----------------------------------
    ' --- When user changes grating type ---
    Private Sub ComboBox_TypeChooser_TextChanged(sender As Object, e As EventArgs) Handles ComboBox_TypeChooser.TextChanged
        GUI_Gratings_Data.TypeChanged(sender, e)
    End Sub


    ' --- When user changes grating material ---
    Private Sub ComboBox_Material_TextChanged(sender As Object, e As EventArgs) Handles ComboBox_Material.TextChanged
        GUI_Gratings_Data.MaterialChanged(sender, e)
    End Sub

    ' --- When user changes mesh size ---
    Private Sub ComboBox_MeshSize_TextChanged(sender As Object, e As EventArgs) Handles ComboBox_MeshSize.TextChanged
        GUI_Gratings_Data.MeshChanged(sender, e)
    End Sub

    ' --- When user changes grating height ---
    Private Sub ComboBox_Height_TextChanged(sender As Object, e As EventArgs) Handles ComboBox_Height.TextChanged
        GUI_Gratings_Data.HeightChanged(sender, e)
    End Sub

    ' --- When user changes grating thickness ---
    Private Sub ComboBox_Thickness_TextChanged(sender As Object, e As EventArgs) Handles ComboBox_Thickness.TextChanged
        GUI_Gratings_Data.ThicknessChanged(sender, e)
    End Sub

    Private Sub CheckBox_WholeMeshWidths_CheckedChanged(sender As Object, e As EventArgs) _
        Handles CheckBox_WholeMeshWidths.CheckedChanged

        GUI_Gratings_Data.WholeMeshWidthsChanged(sender, e)
    End Sub

    ' --- Grating width changed ---
    Private Sub WidthB_TextChanged(sender As Object, e As EventArgs) Handles ComboBox_Width.TextChanged
        GUI_Gratings_Data.WidthChanged(sender, e)
    End Sub

    ' --- Check if width is a number ---
    Private Sub ComboBox_Width_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComboBox_Width.KeyPress
        GUI_Gratings_Data.Check_IfNumber(e)
    End Sub

    ' --- Grating length changed ---
    Private Sub LengthBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox_Length.TextChanged
        GUI_Gratings_Data.LengthChanged(sender, e)
    End Sub

    ' --- Check if length is a number ---
    Private Sub ComboBox_Length_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox_Length.KeyPress
        GUI_Gratings_Data.Check_IfNumber(e)
    End Sub

    ' ---------------------------------- Functions ----------------------------------
    ' --- When angle button is pressed ---
    Private Sub AngleButton_Click(sender As Object, e As EventArgs) Handles AngleButton.Click
        GUI_Functions.AngleButton(sender, e)
    End Sub

    ' --- When square corner button is pressed ---
    Private Sub Button_Square_Click(sender As Object, e As EventArgs) Handles Button_Square.Click
        GUI_Functions.SquareButtton(sender, e)
    End Sub

    ' --- When square side button is pressed ---
    Private Sub Button_SquareSide_Click(sender As Object, e As EventArgs) Handles Button_SquareSide.Click
        GUI_Functions.SquareSideButtton(sender, e)
    End Sub

    ' ---------------------------------- Settings ----------------------------------
    ' --- Settings button clicked ---
    Private Sub SettingsButton_Click(sender As Object, e As EventArgs) Handles SettingsButton.Click
        GUI_Settings.ButtonClicked(sender, e)
    End Sub

    ' ---------------------------------- Export to SW ----------------------------------
    ' --- When export to SW button is pressed ---
    Private Sub ExportSWButton_Click(sender As Object, e As EventArgs) Handles ExportSWButton.Click
        SW_HLCt_Gratings.BuildGrating()
        'SW_Drawing_Gratings.CreateDrawing()
    End Sub


End Class