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.ExpressApp.Editors
Imports System.Windows.Forms
Imports DevExpress.Data.Filtering
Imports DevExpress.Persistent.BaseImpl
Imports System.Data.OleDb

Public Class ZIPHUVC
    Inherits DevExpress.ExpressApp.ViewController

    Dim _WaitFormClass As New WaitFormClass

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overloads Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "Address_DetailView" Then
            For Each editor As PropertyEditor In (CType(View, DetailView)).GetItems(Of PropertyEditor)()
                If editor.PropertyName = "ZipPostal" Then
                    AddHandler editor.ControlCreated, AddressOf editor_ControlCreated
                    Exit For
                End If
            Next editor
        End If

    End Sub
    Private Sub editor_ControlCreated(ByVal sender As Object, ByVal e As EventArgs)
        ' You can access the corresponding property editor control by the following code:
        Dim editorControl As Control = TryCast((CType(sender, PropertyEditor)).Control, Control)
        AddHandler editorControl.HandleCreated, AddressOf editorControl_HandleCreated

        Dim _ZipPostal As DevExpress.XtraEditors.TextEdit = TryCast(editorControl, DevExpress.XtraEditors.TextEdit)
        If _ZipPostal IsNot Nothing Then

            AddHandler _ZipPostal.TextChanged, AddressOf ZipPostal_TextChanged
        End If
    End Sub
    Private Sub editorControl_HandleCreated(ByVal sender As Object, ByVal e As EventArgs)
        ' Sometimes it is necessary to access editor's control after it is visible (its handle is created).
        RemoveHandler (CType(sender, Control)).HandleCreated, AddressOf editorControl_HandleCreated
    End Sub
    Private Sub ZipPostal_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _StringEdit As DevExpress.ExpressApp.Win.Editors.StringEdit = TryCast(sender, DevExpress.ExpressApp.Win.Editors.StringEdit)

        If _StringEdit.Text.Length = 4 Then
            Dim _ZIPHU As ZIPHU = _ocur.FindObject(Of ZIPHU)(CriteriaOperator.Parse("Zip=?", _StringEdit.Text))

            If _ZIPHU IsNot Nothing Then
                Dim _Address As Address = TryCast(View.SelectedObjects(0), Address)
                _Address.ZipPostal = _StringEdit.Text
                _Address.City = _ZIPHU.City
                _Address.StateProvince = _ZIPHU.County
            End If
        End If
    End Sub

    Private Sub aAddress_LoadZIPHUFromXLS_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aAddress_LoadZIPHUFromXLS.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _ZIP As New ArrayList
        Dim _City As New ArrayList
        Dim _Country As New ArrayList

        Dim _OpenFileDialog As New OpenFileDialog
        _OpenFileDialog.Filter = "XLS file (.xls)|*.xls|XLSX file (.xlsx)|*.xlsx|All Files (*.*)|*.*"


        If _OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim _xlsFile As String = _OpenFileDialog.FileName
            Dim _cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;"
            _cs += "Data Source=" & _xlsFile.ToString & ";"
            _cs += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
            Dim conn As New OleDbConnection(_cs)
            Dim cmd As New OleDbCommand
            Dim dr As OleDbDataReader
            Dim dr1 As OleDbDataReader
            Dim mSQL As String
            Dim _UpdatedCount As Long = 0


            Try

                mSQL = "select * from [Munka1$]"
                cmd.CommandTimeout = 60
                cmd.Connection = conn
                cmd.CommandType = CommandType.Text
                cmd.CommandText = mSQL
                conn.Open()

                If conn.State = ConnectionState.Open Then
                    Dim _Count As Long = 0

                    dr1 = cmd.ExecuteReader(CommandBehavior.SingleResult)

                    While dr1.Read
                        If dr1(0).ToString <> "" And dr1(0).ToString <> "ZIP" Then
                            _Count += 1
                        End If
                    End While
                    dr1.Close()

                    dr = cmd.ExecuteReader(CommandBehavior.SingleResult)
                    _WaitFormClass.Initwork(1, 1, _Count)
                    While dr.Read
                        If dr(0).ToString <> "" And dr(0).ToString <> "ZIP" Then

                            _ZIP.Add(dr(0).ToString)
                            _City.Add(dr(1).ToString)
                            _Country.Add(dr(2).ToString)
                            
                            _WaitFormClass.DoWork
                            System.Windows.Forms.Application.DoEvents()
                        End If
                    End While

                    dr.Close()
                End If
                conn.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
                conn.Close()
            End Try
            _WaitFormClass.Finalwork
        End If


        Dim _LineIndex As Long = 0
        For Each _ZIP_String As String In _ZIP
            System.Windows.Forms.Application.DoEvents()


            Dim _ZIPHU As ZIPHU = _onew.FindObject(Of ZIPHU)(CriteriaOperator.Parse("Zip=? AND City=? AND County=?", _ZIP(_LineIndex), _City(_LineIndex), _Country(_LineIndex)))
            If _ZIPHU Is Nothing Then
                With _ZIPHU
                    .ID = _LineIndex + 1
                    .Zip = _ZIP(_LineIndex)
                    .City = _City(_LineIndex)
                    .County = _Country(_LineIndex)
                End With
            End If

            Dim _Country_O As Country = _onew.FindObject(Of Country)(CriteriaOperator.Parse("Name=?", _Country(_LineIndex)), True)
            If _Country_O Is Nothing Then
                _Country_O = New Country(_onew.Session)
                With _Country_O
                    .Name = _Country(_LineIndex)
                End With
            End If
            _LineIndex += 1
        Next
        _WaitFormClass.Finalwork

        _onew.CommitChanges()
    End Sub
End Class
