Imports DigitalData.Modules.Logging
Imports System.Windows.Forms

Public Class FormHelper
    Private ReadOnly LogConfig As LogConfig
    Private ReadOnly Logger As Logger
    Private ReadOnly Form As Form

    Public Sub New(pLogConfig As LogConfig, pForm As Form)
        LogConfig = pLogConfig
        Logger = pLogConfig.GetLogger()
        Form = pForm
    End Sub

    Public Function ShowInfoMessage(pMessage As String, pTitle As String) As DialogResult
        Logger.Info(pMessage)

        Return ShowMessage(pMessage, pTitle, frmDialog.DialogType.Info)
    End Function

    Public Function ShowErrorMessage(pException As Exception, pTitle As String) As DialogResult
        Logger.Error(pException)

        Dim oMethodName = GetMethodName()
        Dim oBaseMessage = "In der Funktion '{0}' ist folgender Fehler aufgetreten: {1}"
        Dim oMessage = String.Format(oBaseMessage, oMethodName, vbNewLine & vbNewLine & pException.Message)

        If LogConfig.Debug Then
            oMessage &= vbNewLine & vbNewLine & "=== Debug Information ==="
            oMessage &= vbNewLine & vbNewLine & "Stacktrace:"
            oMessage &= vbNewLine & pException.StackTrace
        End If

        Return ShowMessage(oMessage, pTitle, frmDialog.DialogType.Error)
    End Function

    Public Function ShowWarningMessage(pMessage As String, pTitle As String) As DialogResult
        Logger.Info(pTitle & " - " & pMessage)
        Return ShowMessage(pMessage, pTitle, frmDialog.DialogType.Warning)
    End Function

    Public Function ShowQuestionMessage(pMessage As String, pTitle As String) As DialogResult
        Logger.Info(pTitle & " - " & pMessage)
        Return ShowMessage(pMessage, pTitle, frmDialog.DialogType.Question)
    End Function

    Public Function ShowSuccessMessage(pMessage As String, pTitle As String) As DialogResult
        Logger.Info(pTitle & " - " & pMessage)
        Return ShowMessage(pMessage, pTitle, frmDialog.DialogType.Success)
    End Function

    Private Function ShowMessage(pMessage As String, pTitle As String, pType As frmDialog.DialogType) As DialogResult
        Dim oForm As New frmDialog(pMessage, pTitle, pType)
        oForm.BringToFront()
        oForm.TopMost = True
        Return oForm.ShowDialog()
    End Function

    Private Function GetMethodName() As String
        Dim oStackTrace As New StackTrace()
        Dim oMethodName As String = oStackTrace.GetFrame(2).GetMethod.Name
        Return oMethodName
    End Function
End Class
