Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports System.Xml

Public Class CurrencyRateVC
    Inherits DevExpress.ExpressApp.ViewController
    Public Event InitWork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
    Public Event DoWork()
    Public Event FinalWork
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub

    Private Sub aSyncroniseMNBCurrencyRate_Execute(sender As System.Object, e As DevExpress.ExpressApp.Actions.SimpleActionExecuteEventArgs) Handles aSyncroniseMNBCurrencyRate.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _uow As New UnitOfWork(_onew.Session.DataLayer)
        SyncroniseMNBCurrencyRate(_uow)

    End Sub
    Sub SyncroniseMNBCurrencyRate(_uow As UnitOfWork)
        Dim _XML As String
        Dim _MNBArfolyamService As New MNBArfolyamService.MNBArfolyamServiceSoapClient
        Dim _CurrencyName_Collection As XPCollection(Of CurrencyName) = New XPCollection(Of CurrencyName)(_uow, CriteriaOperator.Parse("1=1"))
        Dim _CurrencyRate_Collection As XPCollection(Of CurrencyRate) = New XPCollection(Of CurrencyRate)(_uow, CriteriaOperator.Parse("Active=True"))
        Dim _CurrencyRate As CurrencyRate

        Dim _BeginDate As Date = "2011-01-01"
        Dim _StartDate As Date
        Dim _EndDate As Date = String.Format("{0}-{1}-{2}", Now.Year, Now.Month, Now.Day)

        Dim _XMLDocument As New XmlDocument
        Dim _XmlNode1 As Xml.XmlNode
        Dim _XmlNode2 As Xml.XmlNode

        Dim _DateValid As Date
        Dim _RateAverage As Double


        If _MNBArfolyamService IsNot Nothing Then

            If _CurrencyRate_Collection.Count > 0 Then 'Kitöröljük az Active beállítást
                For Each _CurrencyRate In _CurrencyRate_Collection
                    _CurrencyRate.Active = False
                Next
                _uow.CommitChanges()
            End If

            For Each _CurrencyName In _CurrencyName_Collection

                If _CurrencyName.ShortName = "HUF" Then  'A HUF árfolyam nem értelmezett
                    Continue For
                End If

                _CurrencyRate_Collection = New XPCollection(Of CurrencyRate)(_uow, CriteriaOperator.Parse("CurrencyName.ShortName=?", _CurrencyName.ShortName))
                If _CurrencyRate_Collection.Count > 0 Then
                    _CurrencyRate_Collection.Sorting.Add(New SortProperty("DateValid", DB.SortingDirection.Descending))
                    _StartDate = DateAdd(DateInterval.Day, 1, _CurrencyRate_Collection(0).DateValid)
                Else
                    _StartDate = _BeginDate
                End If

                _EndDate = DateAdd(DateInterval.Day, 30, _StartDate)
                If _EndDate > Now.Date Then
                    _EndDate = Now.Date
                End If

                Do While _StartDate <= Now.Date

                    '_XML = _MNBArfolyamService.GetExchangeRates(_StartDate, _EndDate, _CurrencyName.ToString)
                    Dim _GetExchangeRatesRequestBody As New MNBArfolyamService.GetExchangeRatesRequestBody
                    Dim _GetExchangeRatesResponseBody As MNBArfolyamService.GetExchangeRatesResponseBody

                    _GetExchangeRatesRequestBody.startDate = _StartDate
                    _GetExchangeRatesRequestBody.endDate = _EndDate
                    _GetExchangeRatesRequestBody.currencyNames = _CurrencyName.ToString

                    _GetExchangeRatesResponseBody = _MNBArfolyamService.GetExchangeRates(_GetExchangeRatesRequestBody)

                    _XML = _GetExchangeRatesResponseBody.GetExchangeRatesResult

                    _XMLDocument.LoadXml(_XML.ToString)

                    For Each _XmlNode1 In _XMLDocument.SelectNodes("MNBExchangeRates")

                        For Each _XmlNode2 In _XmlNode1.SelectNodes("Day")

                            _RateAverage = _XmlNode2.InnerText
                            _DateValid = _XmlNode2.Attributes("date").Value

                            _CurrencyRate = _uow.FindObject(Of CurrencyRate)(CriteriaOperator.Parse("CurrencyName.ShortName=? And DateValid=?", _CurrencyName.ShortName, _DateValid))

                            If _CurrencyRate Is Nothing Then
                                _CurrencyRate = New CurrencyRate(_uow)
                                _CurrencyRate.CurrencyName = _CurrencyName
                                _CurrencyRate.DateValid = _DateValid
                                _CurrencyRate.RateAverage = _RateAverage
                                _CurrencyRate.IsMNB = True

                                _CurrencyRate.Save()
                            End If
                          
                        Next
                    Next

                    _StartDate = DateAdd(DateInterval.Day, 1, _EndDate.Date)
                    _EndDate = DateAdd(DateInterval.Day, 30, _StartDate)
                    If _EndDate > Now.Date Then
                        _EndDate = Now.Date
                    End If
                Loop

                _CurrencyRate_Collection = New XPCollection(Of CurrencyRate)(_uow, CriteriaOperator.Parse("CurrencyName.ShortName=?", _CurrencyName.ShortName))
                If _CurrencyRate_Collection.Count > 0 Then
                    _CurrencyRate_Collection.Sorting.Add(New SortProperty("DateValid", DB.SortingDirection.Descending))
                    _StartDate = DateAdd(DateInterval.Day, 1, _CurrencyRate_Collection(0).DateValid)
                    _CurrencyRate_Collection(0).Active = True
                    _CurrencyRate_Collection(0).Save()
                End If
            Next

        End If

        _uow.CommitChanges()
    End Sub

    ''// Az összes CurrencyName XPCollection-ra ! (aktuális évet megelőző év -> mai napig
    '_XML = _MNBArfolyamService.GetExchangeRates("2012-03-02", "2012-03-05", "EUR")
    '_XML = _MNBArfolyamService.GetExchangeRates("2012-03-02", "2012-03-05", "USD")
    '_XML = _MNBArfolyamService.GetExchangeRates("2012-03-02", "2012-03-05", "CHF")
    '_XML = _MNBArfolyamService.GetExchangeRates("2012-03-02", "2012-03-05", "GBP")
End Class
