Imports System.Globalization
Imports DevExpress.CodeParser
Imports DevExpress.Data.Controls
Imports DevExpress.XtraPrinting

Public Class ClassFormat
    Public Const CURRENCY_FORMAT = "C2"
    Public Const DECIMAL_FORMAT = "F"
    Public Const DATE_FORMAT = "d"
    Public Const DATETIME_FORMAT = "G"

    Public Shared Function GetFormatString(pFormatString As String) As String
        Select Case pFormatString
            Case ClassControlCreator.CONTROL_TYPE_CURRENCY
                Return CURRENCY_FORMAT

            Case ClassControlCreator.CONTROL_TYPE_DOUBLE
                Return DECIMAL_FORMAT

            Case ClassControlCreator.CONTROL_TYPE_DATE
                Return DATE_FORMAT

            Case ClassControlCreator.CONTROL_TYPE_DATETIME
                Return DATETIME_FORMAT

            Case Else
                Return String.Empty

        End Select
    End Function

    ''' <summary>
    ''' Converts a string according to the type information, using the invariant culture
    ''' </summary>
    ''' <param name="pValue"></param>
    ''' <param name="pType"></param>
    ''' <returns></returns>
    Public Shared Function GetConvertedValue(pValue As Object, pType As String) As Object
        Dim oConvertedValue
        LOGGER.Debug($"GetConvertedValue: {pType}")

        Select Case pType
            Case ClassControlCreator.CONTROL_TYPE_DOUBLE
                If Double.TryParse(pValue, NumberStyles.Float, CultureInfo.InvariantCulture, oConvertedValue) Then
                    Return oConvertedValue
                End If

            Case ClassControlCreator.CONTROL_TYPE_CURRENCY
                Try
                    LOGGER.Debug($"GetConvertedValue: Converting {pValue.ToString} to Currency ")
                    If Double.TryParse(pValue, NumberStyles.Currency, CultureInfo.InvariantCulture, oConvertedValue) Then
                        Return oConvertedValue
                    End If
                Catch ex As Exception
                    LOGGER.Error(ex)
                End Try


            Case ClassControlCreator.CONTROL_TYPE_INTEGER
                If Integer.TryParse(pValue, NumberStyles.Integer, CultureInfo.InvariantCulture, oConvertedValue) Then
                    Return oConvertedValue
                End If
            Case Else
                LOGGER.Debug($"GetConvertedValue - Case ELSE - pType is {pType}")
                Try
                    oConvertedValue = pValue.ToString
                Catch ex As Exception
                    LOGGER.Warn($"Error in GetConvertedValue:  pType is {pType} - converting value to String")
                    oConvertedValue = ""
                End Try

        End Select

        Return oConvertedValue
    End Function

    ''' <summary>
    ''' Converts values to their respective data type and then back to string 
    ''' according to the current culture
    ''' </summary>
    ''' <param name="pValue"></param>
    ''' <returns></returns>
    Public Shared Function GetStringValue(pValue As Object) As String
        Select Case pValue.GetType
            Case GetType(Single)
                Return DirectCast(pValue, Single).ToString(CultureInfo.CurrentCulture)

            Case GetType(Double)
                Return DirectCast(pValue, Double).ToString(CultureInfo.CurrentCulture)

            Case GetType(Decimal)
                Return DirectCast(pValue, Decimal).ToString(CultureInfo.CurrentCulture)

            Case GetType(Date)
                Return DirectCast(pValue, Date).ToString(CultureInfo.CurrentCulture)

            Case GetType(DateTime)
                Return DirectCast(pValue, DateTime).ToString(CultureInfo.CurrentCulture)

            Case Else
                Return pValue.ToString
        End Select
    End Function
End Class
