﻿Imports System.Text
Imports Superbowl_Daily_2.SystemFunctions
Imports Superbowl_Daily_2.ProjectFunctions
Imports Superbowl_Daily_2.db
Imports Superbowl_Daily_2.Export_Process

Public Class dataExport

   Public intRecordsTotal, intRecordsExported, intRecordsFail As Integer
   Public Message As String = ""
   Public DT As DataTable
   Public Filename As String
   Public ProcessName As String = ""
   Public blnError As Boolean = False
   Public ParentProcess As Export_Process


   Public Sub New(ByVal table As DataTable, ByVal filename As String, Optional ep As Export_Process = Nothing, Optional ProcessName As String = "Export Class")
      Me.Filename = filename
      Me.DT = table

      Me.intRecordsTotal = 0
      Me.intRecordsExported = 0
      Me.intRecordsFail = Me.DT.Rows.Count

      Me.ProcessName = ProcessName
      Me.ParentProcess = ep

   End Sub


   Public Sub Execute()

      Dim i As Integer = 0
      Dim str As New StringBuilder
      Dim strDelimiter As String = ""
      ' 20140923 - v2.1.2 - changed export to use | and no delimiters - pj
      Dim strSep As String = "|"
      Dim strValue As String = ""


      For Each dc As DataColumn In Me.DT.Columns
         'For Each columnField As Object In dc.ColumnName
         str.Append(strDelimiter & dc.ToString & strDelimiter & strSep)
         'Next
      Next

      str.Replace(strSep, vbNewLine, str.Length - 1, 1) 'remove last ,

      For Each dr As DataRow In Me.DT.Rows
         Try
            For intCol As Integer = 0 To Me.DT.Columns.Count - 1
               If Me.DT.Columns(intCol).DataType.ToString() = "System.String" Then
                  'If Me.DT.Columns(intCol).isStringType Is Boolean.Equals(True) Then
                  'strDelimiter = """"
                  ' 20140923 - v2.1.2 - changed export to use | and no delimiters - pj
                  'strDelimiter = ""
               Else
                  'strDelimiter = ""
               End If

               strValue = dr(intCol).ToString
               strValue = strValue.Replace("""", "")
               strValue = strValue.Replace(",", "")
               strValue = strValue.Replace("%", "")
               strValue = strValue.Replace(";", "")

               str.Append(strDelimiter & strValue & strDelimiter & strSep)
            Next

            'For Each rowField As Object In dr.ItemArray
            'Next
            str.Replace(strSep, vbNewLine, str.Length - 1, 1)
            i += 1
         Catch ex As Exception
            WriteLog("error", Me.ProcessName, "Export Import", "Error writing record! [" & ex.Message & "] {" & i & "/" & Me.intRecordsTotal & "}")

         Finally
            'post
            If Not IsNothing(Me.ParentProcess) Then
               Me.ParentProcess.Progress()
            End If

         End Try
      Next

      Me.intRecordsExported = i

      Try
         My.Computer.FileSystem.WriteAllText(Filename, str.ToString, False, Encoding.Default)
         Me.Message = "Export complete: " & Filename
         Me.blnError = False

         'MessageBox.Show(i & " Rows Exported Successfully...")
      Catch ex As Exception
         Me.blnError = True
         Me.Message = ex.Message & Chr(13) & Chr(10) & ex.StackTrace
         'MessageBox.Show("Write Error")
      End Try

   End Sub


   'Dim writer As System.IO.StreamWriter
   'Try
   '    writer = New System.IO.StreamWriter(filename)

   '    ' first write a line with the columns name
   '    Dim sep As String = ""
   '    Dim builder As New System.Text.StringBuilder
   '    For Each col As DataColumn In table.Columns
   '        builder.Append(sep).Append(col.ColumnName)
   '        sep = sepChar
   '    Next
   '    writer.WriteLine(builder.ToString())

   '    ' then write all the rows
   '    For Each row As DataRow In table.Rows
   '        sep = ""
   '        builder = New System.Text.StringBuilder

   '        For Each col As DataColumn In table.Columns
   '            builder.Append(sep).Append(row(col.ColumnName))
   '            sep = sepChar
   '        Next
   '        writer.WriteLine(builder.ToString())
   '    Next
   'Catch ex As Exception
   '    MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
   'Finally
   '    If Not writer Is Nothing Then writer.Close()
   'End Try



   'Dim str As New StringBuilder
   'For Each dc As DataColumn In ds.Tables(0).Columns
   '    'For Each columnField As Object In dc.ColumnName
   '    str.Append(dc.ToString & ",")
   '    'Next
   '    ' str.Replace(",", vbNewLine, str.Length - 1, 1)
   'Next

   'str.Replace(",", vbNewLine, str.Length - 1, 1)

   'For Each dr As DataRow In ds.Tables(0).Rows
   '    For Each rowField As Object In dr.ItemArray
   '        str.Append(rowField.ToString & ",")
   '    Next
   '    str.Replace(",", vbNewLine, str.Length - 1, 1)
   'Next

   'Try
   '    My.Computer.FileSystem.WriteAllText("C:\temp\info.csv", str.ToString, False)
   'Catch ex As Exception
   '    MessageBox.Show("Write Error")
   'End Try
End Class
