Imports DevExpress.Spreadsheet
Imports DevExpress.Spreadsheet.Functions
Imports DevExpress.XtraSpreadsheet
Imports DevExpress.ExpressApp
Imports DevExpress.Data.Filtering

Public Class BallanceSpreadCustomFunctions
    Inherits Object
    Implements ICustomFunction

    Private Const functionName As String = "GETGLBALLANCE"
    Private ReadOnly functionParameters() As ParameterInfo
    Private _BallanceHeader As BallanceHeader
    Private _ocur As Xpo.XPObjectSpace
    Private Structure stBalance
        Dim Side As String
        Dim AccountNumber As String
        Dim Balance As String
        Dim Operation As String
    End Structure
    Private _BalanceArray() As stBalance
    Public Sub New(_p_BallanceHeader As BallanceHeader, _p_ocur As Xpo.XPObjectSpace)
        ' Missing optional parameters do not result in an error message.
        Me.functionParameters = New ParameterInfo() {New ParameterInfo(ParameterType.Value, ParameterAttributes.Required), New ParameterInfo(ParameterType.Value, ParameterAttributes.Optional)}
        _BallanceHeader = _p_BallanceHeader
        _ocur = _p_ocur
    End Sub

    Public Function Evaluate(parameters As IList(Of ParameterValue), context As EvaluationContext) As ParameterValue Implements IFunction.Evaluate
        Dim _GetGLBallance As Double = 0
        ReDim _BalanceArray(100)

        Try
            If parameters.Count = 2 Then

                MakeBalanceArray(parameters(1).TextValue)
                _GetGLBallance = Math.Round(GetValuefromArray(parameters(0).NumericValue) / 1000, 0, MidpointRounding.AwayFromZero)

            End If
        Catch ex As Exception

        End Try

        Return _GetGLBallance
    End Function

    Private Sub MakeBalanceArray(_BalanceString As String)
        Dim _act_ballancestring As String = ""
        Dim _act_arrayindex As Long = 0
        Dim i As Long = 0

        For i = LBound(_BalanceArray) To UBound(_BalanceArray)
            _BalanceArray(i).AccountNumber = ""
            _BalanceArray(i).Balance = ""
            _BalanceArray(i).Operation = ""
            _BalanceArray(i).Side = ""
        Next

        For i = 1 To Len(_BalanceString)
            _act_ballancestring = Mid(_BalanceString, i, 1)
            Select Case _act_ballancestring
                Case "T", "K"
                    _BalanceArray(_act_arrayindex).Side = _act_ballancestring
                Case "+", "-"
                    _act_arrayindex += 1
                    _BalanceArray(_act_arrayindex).Operation = _act_ballancestring
                Case Else
                    _BalanceArray(_act_arrayindex).AccountNumber += _act_ballancestring
            End Select
        Next

    End Sub
    Private Function GetValuefromArray(_Mode As Long) As Double
        Dim i As Long = 0
        Dim _GetValueFromArray As Double = 0

        For i = LBound(_BalanceArray) To UBound(_BalanceArray)
            Select Case _BalanceArray(i).Operation
                Case "+"
                    _GetValueFromArray += GetValuefromTrialBallance(_Mode, _BalanceArray(i).Side, _BalanceArray(i).AccountNumber)
                Case "-"
                    _GetValueFromArray -= GetValuefromTrialBallance(_Mode, _BalanceArray(i).Side, _BalanceArray(i).AccountNumber)
            End Select
        Next
        Return _GetValueFromArray
    End Function
    Private Function GetValuefromTrialBallance(_Mode As Long, _Side As String, _AccountNumber As String) As Double
        Dim _TrialBalanceHeader As TrialBalanceHeader
        Dim _GetValuefromTrialBallance As Double = 0
        Dim _TrialBalance As TrialBalance = Nothing
        If _Mode = 0 Then
            _TrialBalanceHeader = _BallanceHeader.TrialBalanceHeader
        Else
            _TrialBalanceHeader = _BallanceHeader.TrialBalanceHeaderBefore
        End If

        _TrialBalance = _ocur.FindObject(Of TrialBalance)(CriteriaOperator.Parse("TrialBalanceHeader=? and AccountNumber=?", _TrialBalanceHeader, _AccountNumber))
        If _TrialBalance IsNot Nothing Then
            If _Side = "T" Then
                _GetValuefromTrialBallance = _TrialBalance.DebitRAmountHUF
            End If
            If _Side = "K" Then
                _GetValuefromTrialBallance = _TrialBalance.CreditRAmountHUF
            End If
        End If
        Return _GetValuefromTrialBallance
    End Function
#Region "BASE"
    Public Function GetName(culture As Globalization.CultureInfo) As String Implements IFunction.GetName
        Return functionName
    End Function

    Public ReadOnly Property Name As String Implements IFunction.Name
        Get
            Return functionName
        End Get
    End Property

    Public ReadOnly Property Parameters As ParameterInfo() Implements IFunction.Parameters
        Get
            Return functionParameters
        End Get
    End Property

    Public ReadOnly Property ReturnType As ParameterType Implements IFunction.ReturnType
        Get
            Return ParameterType.Value
        End Get
    End Property

    Public ReadOnly Property Volatile As Boolean Implements IFunction.Volatile
        Get
            Return False
        End Get
    End Property
#End Region
End Class
