Imports System.Globalization
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Database
    Public Shared database As New DataSet

    ' --- Retrive the Excel database --- 
    Public Shared Sub Retrive_Database()
        Dim excelApp As Excel.Application = New Excel.Application
        Dim excelWB As Excel.Workbook


        ' --- READ EXCEL FILE DATA FROM .TXT FILES ---
        'Dim excelData As String() = System.IO.File.ReadAllLines("C:\Users\Anton\Documents\Exjobb\Inspo\excelData.txt")

        'excelWB = excelApp.Workbooks.Open("C:\Users\Anton\Documents\Exjobb\Inspo\loadData.xlsx")
        'Dim activeSheet As Excel.Worksheet
        'activeSheet = excelWB.Sheets(1)

        'For i = 0 To excelData.Length - 1
        '    If i Mod 2 = 1 Then
        '        Dim words As String() = excelData(i).Split(" ")
        '        For j = 1 To words.Length - 1
        '            activeSheet.Cells(Math.Floor(i / 2) + 1, j).Value = words(j)
        '        Next
        '    End If
        'Next

        'excelWB.Save()

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



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

        For i = 1 To 5
            Dim activeSheet As Excel.Worksheet
            activeSheet = excelWB.Sheets(i)
            wSNames.Add(activeSheet.Name)
            activeSheet.SaveAs(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)

        ' USe this when all sheets are to be read
        'For Each WS In excelWB.Worksheets
        '    WS.SaveAs(GUI_Settings.HLCtFolder & ("\") & WS.Name, Excel.XlFileFormat.xlCSV)
        'Next


        ' Read all .csv files to local database
        For i = 0 To wSNames.Count - 1
            Dim tempDT As New DataTable With {
                .TableName = wSNames(i)
            }

            Dim lines = IO.File.ReadAllLines(Settings.HLCtFolder & "\Database\" & wSNames(i) & ".csv")

            ' Create DataTable columns 
            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 = "dbl" Then
                    tempDT.Columns.Add(colName, GetType(Double))
                Else
                    tempDT.Columns.Add(colName, GetType(Boolean))
                End If
            Next

            ' Fill the DataTable with all data 
            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
                    If tempDT.Columns(k).DataType = GetType(Double) Then
                        values(k) = CDbl(Val(values(k)))
                    End If
                    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

            ' Add DataTable to local database (DataSet)
            database.Tables.Add(tempDT)
        Next
    End Sub
End Class
