' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Public Class MyInptBox

    Public result As Object
    Private result_type As Integer

    Public Sub my_showdialog(ByVal Texte As String, ByVal result_type_ As Integer, ByVal result_max_length As Integer, ByVal default_response As String)
        apiBlockInput(0)
        RichTextBox1.Text = vbCrLf & Texte
        result_type = result_type_
        TextBox1.MaxLength = result_max_length
        TextBox1.Text = default_response
        TextBox1.Focus()
        TextBox1.Select()
        TextBox1.SelectAll()
        Me.ShowDialog()
    End Sub
    Private Sub OK_Button_Click(sender As Object, e As EventArgs) Handles OK_Button.Click

        Select Case result_type

            Case VarType(New Decimal)
                TextBox1.Text = Replace(TextBox1.Text, ".", ",")
                Try
                    CDec(TextBox1.Text).ToString()
                    result = CDec(TextBox1.Text)
                Catch ex As Exception
                    Exit Sub
                End Try

            Case VarType(New Integer)
                Try
                    CInt(TextBox1.Text).ToString()
                    result = CInt(TextBox1.Text)
                Catch ex As Exception
                    Exit Sub
                End Try
            Case VarType(New String(""))
                result = TextBox1.Text
        End Select
        Me.Close()


    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            OK_Button_Click(Nothing, Nothing)
        End If
    End Sub


    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Select Case result_type
            Case VarType(New Decimal)
                TextBox1.Text = Replace(TextBox1.Text, ".", ",")
                TextBox1.SelectionStart = TextBox1.Text.Length + 1
                Try
                    CDec(TextBox1.Text).ToString()
                    TextBox1.ForeColor = Color.Black
                    OK_Button.Enabled = True
                Catch ex As Exception
                    TextBox1.ForeColor = Color.Red
                    OK_Button.Enabled = False
                    Exit Sub
                End Try

            Case VarType(New Integer)
                Try
                    CInt(TextBox1.Text).ToString()
                    TextBox1.ForeColor = Color.Black
                    OK_Button.Enabled = True
                Catch ex As Exception
                    TextBox1.ForeColor = Color.Red
                    OK_Button.Enabled = False
                    Exit Sub
                End Try
            Case VarType(New String(""))

        End Select
    End Sub

    Private Sub MyInptBox_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        round()
        round2(RichTextBox1)
        round2(OK_Button)
        round2(TextBox1)
        RichTextBox1.SelectAll()
        RichTextBox1.SelectionAlignment = HorizontalAlignment.Center
    End Sub
    Private Sub round()
        Me.FormBorderStyle = FormBorderStyle.None
        'Me.Height = 300
        'Me.Width = 400
        Dim p As New Drawing2D.GraphicsPath()
        p.StartFigure()
        p.AddArc(New Rectangle(0, 0, 40, 40), 180, 90)
        p.AddLine(40, 0, Me.Width - 40, 0)
        p.AddArc(New Rectangle(Me.Width - 40, 0, 40, 40), -90, 90)
        p.AddLine(Me.Width, 40, Me.Width, Me.Height - 40)
        p.AddArc(New Rectangle(Me.Width - 40, Me.Height - 40, 40, 40), 0, 90)
        p.AddLine(Me.Width - 40, Me.Height, 40, Me.Height)
        p.AddArc(New Rectangle(0, Me.Height - 40, 40, 40), 90, 90)
        p.CloseFigure()
        Me.Region = New Region(p)
    End Sub
End Class