Imports System.Drawing.Drawing2D

Imports Stax.AppFramework

Namespace UI
    Partial Public Class MessageBoxForm
        Inherits Form

#Region " Designer "
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub

        Private components As System.ComponentModel.IContainer

        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.SuspendLayout()
            '
            'Msg
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(356, 318)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Name = "Msg"
            Me.ShowInTaskbar = False
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
            Me.ResumeLayout(False)

        End Sub
#End Region

        Private ButtonSize As Size = New Size(75, 24)
        Private ButtonAlignment As HorizontalAlignment = HorizontalAlignment.Center
        Private MaxMessageWidth As Integer = SystemInformation.WorkingArea.Width \ 2
        Private AllButtonsWidth As Integer
        Private ButtonGap As Integer = 6
        Private ButtonTextMargin As Integer = 10
        Private ImageSize As Size
        Private ImagePlusMarginWidth As Integer
        Private Msg As Msg
        Private MarginValue As Integer = 12
        Private TextSize As Size
        Private CheckBox As CheckBox

        Public Sub New(ByVal msg As Msg)
            InitializeComponent()
            Me.Msg = msg
        End Sub

        Private PressedButtonValue As Button

        Public Property PressedButton() As Button
            Get
                Return PressedButtonValue
            End Get
            Set(ByVal Value As Button)
                PressedButtonValue = Value
            End Set
        End Property

        Public Shadows Function ShowDialog() As DialogResult
            Init()
            Return MyBase.ShowDialog()
        End Function

        Private Sub Init()
            Beep()
            Dim g As Graphics = CreateGraphics()

            If Not Msg.Image Is Nothing Then
                ImageSize = Msg.Image.Size
                ImagePlusMarginWidth = ImageSize.Width + MarginValue
            End If

            Dim tempTextSize As SizeF = g.MeasureString(Msg.Text, Font, MaxMessageWidth)
            TextSize = New Size(RoundUp(tempTextSize.Width), RoundUp(tempTextSize.Height))

            If TextSize.Width < 100 Then
                TextSize.Width = 100
            End If

            Dim clientHeight As Integer = MarginValue + MarginValue + ButtonSize.Height + MarginValue

            If Msg.Image Is Nothing OrElse TextSize.Height > Msg.Image.Height Then
                clientHeight += TextSize.Height
            Else
                clientHeight += Msg.Image.Height
            End If

            If Msg.ShowCheckBox Then
                clientHeight += 12
            End If

            ClientSize = New Size(MarginValue + ImagePlusMarginWidth + _
                TextSize.Width + MarginValue, clientHeight)

            If Msg.ShowCheckBox Then
                CheckBox = New CheckBox
                CheckBox.Text = Msg.CheckBoxText
                CheckBox.Left = MarginValue
                CheckBox.AutoSize = True
                CheckBox.BackColor = Color.Transparent

                CheckBox.Top = ClientSize.Height - MarginValue - ButtonSize.Height - 20
                Controls.Add(CheckBox)

                If ClientSize.Width < CheckBox.Width + MarginValue + MarginValue Then
                    ClientSize = New Size(CheckBox.Width + MarginValue + _
                        MarginValue, ClientSize.Height)
                End If
            End If

            Dim nextButtonLeft As Integer = MarginValue

            If Msg.ButtonList.Count = 0 Then
                Msg.AddButton("OK", Windows.Forms.DialogResult.OK)
            End If

            For Each i As Button In Msg.ButtonList
                i.Height = ButtonSize.Height
                i.Width = ButtonSize.Width
                i.Top = ClientSize.Height - MarginValue - ButtonSize.Height

                Dim stringSize As SizeF = g.MeasureString(i.Text, Font)

                If (stringSize.Width + ButtonTextMargin) > ButtonSize.Width Then
                    i.Width = CInt(stringSize.Width + ButtonTextMargin)
                End If

                i.Left = nextButtonLeft
                AddHandler i.Click, AddressOf ButtonClick
                Controls.Add(i)
                nextButtonLeft += i.Width + ButtonGap
                AllButtonsWidth = i.Left + i.Width - MarginValue
            Next

            If ClientSize.Width < AllButtonsWidth + MarginValue + MarginValue Then
                ClientSize = New Size(AllButtonsWidth + MarginValue + MarginValue, ClientSize.Height)
            End If

            If ClientSize.Width > AllButtonsWidth + MarginValue + MarginValue Then
                If ButtonAlignment = HorizontalAlignment.Center Then
                    Dim leftFirst As Integer = (ClientSize.Width - AllButtonsWidth) \ 2
                    Dim offset As Integer = leftFirst - Msg.ButtonList(0).Left

                    For Each i As Button In Msg.ButtonList
                        i.Left += offset
                    Next
                ElseIf ButtonAlignment = HorizontalAlignment.Right Then
                    Dim leftFirst As Integer = ClientSize.Width - AllButtonsWidth - MarginValue
                    Dim offset As Integer = leftFirst - Msg.ButtonList(0).Left

                    For Each i As Button In Msg.ButtonList
                        i.Left += offset
                    Next
                End If
            End If

            If Msg.Title Is Nothing Then
                Text = Application.ProductName
            Else
                Text = Msg.Title
            End If
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            Dim g As Graphics = e.Graphics
            Dim h As Integer = MarginValue

            If Appearance.IsMsgGradient Then
                Using b As New LinearGradientBrush(ClientRectangle, Color.White, Appearance.GradientColorLight, LinearGradientMode.Horizontal)
                    g.FillRectangle(b, ClientRectangle)
                End Using
            End If

            If Not Msg.Image Is Nothing Then
                g.DrawImage(Msg.Image, MarginValue, MarginValue)

                If Msg.Image.Height > TextSize.Height Then
                    h = MarginValue + (Msg.Image.Height - TextSize.Height) \ 2
                End If
            End If

            Dim rect As New Rectangle(MarginValue + ImagePlusMarginWidth, h, _
                TextSize.Width, TextSize.Height)

            Dim sb As New SolidBrush(Msg.TextColor)
            g.DrawString(Msg.Text, Msg.Font, sb, rect)
            sb.Dispose()
        End Sub

        Private Function RoundUp(ByVal value As Double) As Integer
            Return CInt(Math.Ceiling(value))
        End Function

        Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
            Dim b As Button = CType(sender, Button)
            PressedButton = b
            DialogResult = Msg.ButtonResults(b)
            Close()
        End Sub

        Private Sub Msg_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
            If Msg.ShowCheckBox Then
                Msg.Checked = CheckBox.Checked
            End If
        End Sub
    End Class
End Namespace