Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Net.Mail
Imports System.Windows.Forms

Public Class cErrorHandler

    Public Shared Sub ERR(message As String, stack As String, functionName As String, Optional OP As String = "", Optional title As String = "", Optional mailadess As String = "al@verag.ag", Optional recieverMailAdress As String = "", Optional zusatz As String = "", Optional errCode As String = Nothing)
        If OP = "" Then OP = VERAG_PROG_ALLGEMEIN.cAllgemein.ERR_OP_GLOBAL ' GLobal gesetzes Errorhandling! (Standart=SHOW)

        'If Form.ActiveForm IsNot Nothing Then
        '    Dim bm As New Bitmap(Form.ActiveForm.Width, Form.ActiveForm.Height)
        '    Dim g As Graphics = Graphics.FromImage(bm)
        '    g.CopyFromScreen(Form.ActiveForm.Location, New Point(0, 0), New Size(Form.ActiveForm.Width, Form.ActiveForm.Height))
        '    bm.Save("C:\formgrab.bmp", Drawing.Imaging.ImageFormat.Bmp)
        'End If

        Try

            Select Case OP
            Case ERROR_OP.MAIL
                sendERRORperMail(title & " | " & My.Application.Info.AssemblyName, message, stack, mailadess, zusatz, functionName)
            Case ERROR_OP.SHOW
                Dim c As New cProgramFunctions
                Dim url = c.MakeScreenshot()

                Dim f As New frmErrorMeldung(If(functionName <> "", "Fehler in der Funktion '" & functionName & "'" & vbNewLine & vbNewLine, "") & message & vbNewLine & zusatz, stack, title, url)
                f.ShowDialog()
            Case ERROR_OP.SHOW_MSGBOX
                MsgBox(If(functionName <> "", "Fehler in der Funktion '" & functionName & "'" & vbNewLine & vbNewLine, "") & message & vbNewLine & stack & vbNewLine & zusatz, MsgBoxStyle.OkOnly, If(title <> "", title, "Fehler"))
        End Select

        VERAG_PROG_ALLGEMEIN.cERS.saveErr(OP, message, stack, errCode, functionName, recieverMailAdress, zusatz)


        Catch ex As Exception
            Try
                VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)

            Catch ex2 As Exception

            End Try
        End Try

    End Sub

    Public Shared Sub sendERRORperMail(title, errmsg, errstack, recieverMailAdress, zusatz, functionName)
        Dim Msg As New MailMessage
        Dim myCredentials As New System.Net.NetworkCredential
        'myCredentials.UserName = "support@verag.ag"
        'myCredentials.Password = "$up0Rt2809!"
        myCredentials.UserName = "al@verag.ag"
        myCredentials.Password = "verag#2"

        Msg.IsBodyHtml = False
        Dim mySmtpsvr As New SmtpClient()
        mySmtpsvr.Host = "owa.verag.ag" 'bei web.de
        mySmtpsvr.Port = 25 '587 '25
        mySmtpsvr.UseDefaultCredentials = False
        mySmtpsvr.Credentials = myCredentials

        Try
            Msg.From = New MailAddress("al@verag.ag")
            Msg.To.Add(recieverMailAdress)

            Msg.Subject = title '"ERROR - DAKOSY Einarbeitung"
            Msg.Body = "Uhrzeit: " & Now.ToShortDateString & " " & Now.ToShortTimeString
            Msg.Body = "Function: " & functionName
            Msg.Body &= vbNewLine
            Msg.Body &= vbNewLine
            Msg.Body &= "Fehlermeldung: " & errmsg
            Msg.Body &= vbNewLine
            Msg.Body &= vbNewLine
            Msg.Body &= "Stacktrace: " & errstack
            Msg.Body &= vbNewLine
            If zusatz <> "" Then
                Msg.Body &= "Zusatz: " & zusatz
                Msg.Body &= vbNewLine
            End If
            Msg.Body &= errstack
            mySmtpsvr.Send(Msg)
        Catch ex As Exception
            Try
                mySmtpsvr.Host = "192.168.0.107" 'bei web.de ' 235
                mySmtpsvr.Send(Msg)

            Catch ex2 As Exception
                ' MsgBox(ex.Message & ex.StackTrace)
            End Try


        End Try

    End Sub

End Class

Public Class ERROR_OP
    Shared Property SHOW As String = "SHOW"
    Shared Property SHOW_MSGBOX As String = "SHOW_MSGBOX"
    Shared Property MAIL As String = "MAIL"
    Shared Property TOFILE As String = "TOFILE"
    Shared Property LOG As String = "LOG" 'Nur fürs Protokoll, keine Fehlermeldung
End Class
