Imports System.Threading
Imports System.IO

Imports System.Collections.Generic
Imports System.Drawing
Imports Newtonsoft

Imports System.Text
Imports System.Web
Imports System.Net.Http
Imports System.Net

Module Module1
    Dim Base64_File As String
    Dim JsonString As String
    Dim mimetype As String
    Dim OutputFile As String

    Sub Main()
        Console.WriteLine(Now.ToString, "yyyy-mm-dd hh:MM:ss" + ": Start")
        Dim args As String() = Environment.GetCommandLineArgs()

        If IsNothing(args(1)) Then

        End If

        Console.WriteLine(Now.ToString, "yyyy-mm-dd hh:MM:ss" + ": Fileconvert")

        OutputFile = My.Settings.TempDir + "\" + My.Settings.Filename
        Base64_File = ConvertFileToBase64(OutputFile)
        JsonString = ""
        Console.WriteLine(Now.ToString, "yyyy-mm-dd hh:MM:ss" + ": Create DataStream")
        Create_OnBase_Datastream(My.Settings.Dokumentid)


        Console.WriteLine(Now.ToString, "yyyy-mm-dd hh:MM:ss" + ": Start UpLoad")
        If args(1) = "A" Then
            Dim data = Encoding.UTF8.GetBytes(JsonString)
            Dim myUri As New Uri(My.Settings.url)

            Dim result_post = SendRequest_A(myUri, data, "application/json", "POST")
        End If
        Console.WriteLine(Now.ToString, "yyyy-mm-dd hh:MM:ss" + ": Stop Upload")

    End Sub

    Public Function ConvertFileToBase64(ByVal fileName As String) As String

        Dim ReturnValue As String = ""

        If My.Computer.FileSystem.FileExists(fileName) Then
            Using BinaryFile As FileStream = New FileStream(fileName, FileMode.Open)
                Dim BinRead As BinaryReader = New BinaryReader(BinaryFile)
                Dim BinBytes As Byte() = BinRead.ReadBytes(CInt(BinaryFile.Length))
                ReturnValue = Convert.ToBase64String(BinBytes)
                BinaryFile.Close()
            End Using
        End If
        Return ReturnValue
    End Function

    Public Function Transfer_Onbase(ByVal dokumentid As String) As Boolean
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter(My.Settings.TempDir + "\" + dokumentid + ".json", True)
        file.Write(JsonString)
        file.Close()
        Return True
    End Function
    Private Sub Create_OnBase_Datastream(ByVal dokumentid As String)
        Dim ds As New DataSet
        Dim od As New OnBaseDokument
        od.dokumentTyp = "Brief"
        od.bpNummer = "1032777"
        od.personNummer = ""
        od.dokumentDatei = Base64_File

        od.dokumentDatum = Now.ToString("dd.mm.yyyy")
        od.dateiTyp = "PDF"

        Dim Attribute As New List(Of attribute)
        Dim p As New attribute With
        {
          .fieldname = "EDOKA Dokument-ID",
          .fieldvalue = dokumentid
        }
        Attribute.Add(p)
        od.attributes = Attribute
        JsonString = ""
        JsonString = Newtonsoft.Json.JsonConvert.SerializeObject(od, Newtonsoft.Json.Formatting.Indented)



    End Sub
    Private Function SendRequest_A(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String
        Dim response As String
        Dim request As WebRequest

        request = WebRequest.Create(uri)
        request.ContentLength = jsonDataBytes.Length
        request.ContentType = contentType
        request.Method = method
        Try
            Using requestStream = request.GetRequestStream
                requestStream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
                requestStream.Close()

                Using responseStream = request.GetResponse.GetResponseStream
                    Using reader As New StreamReader(responseStream)
                        response = reader.ReadToEnd()
                    End Using
                End Using
            End Using


            Return response
        Catch ex As Exception
            Return ""
        End Try

    End Function



End Module
