using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Layout;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp.Templates;
using DevExpress.ExpressApp.Utils;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using Microsoft.VisualBasic.FileIO;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using DevExpress.ExpressApp.Xpo;

namespace Nuvolar.Module.Win.CSharp.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class GIROInfo_VC : ViewController
    {
        public GIROInfo_VC()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
            this.aGIROInfo_ImportCSV.TargetObjectType = typeof(GIROInfo);
            this.aGIROInfo_ImportCSV.TargetViewType = ViewType.ListView;
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            // Perform various tasks depending on the target View.
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            // Access and customize the target View control.
        }
        protected override void OnDeactivated()
        {
            // Unsubscribe from previously subscribed events and release other references and resources.
            base.OnDeactivated();
        }

        private void aGIROInfo_ImportCSV_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _onew = Application.CreateObjectSpace() as XPObjectSpace;
            OpenFileDialog _OpenFileDialog = new OpenFileDialog();

            Nuvolar.Module.Win.WaitFormClass _WaitFormClass = new WaitFormClass();

            _OpenFileDialog.Filter = "csv Files (*.csv)|*.csv";
            _OpenFileDialog.FilterIndex = 1;

            if (_OpenFileDialog.ShowDialog() == DialogResult.OK && (!String.IsNullOrEmpty(_OpenFileDialog.FileName)))
            {
                StreamReader _textStreamReader = new StreamReader(_OpenFileDialog.FileName,Encoding.Default);
                string _StrBuffer;

                _StrBuffer = _textStreamReader.ReadToEnd();
                using (TextFieldParser parser = new TextFieldParser(new StringReader(_StrBuffer)))
                {
                    parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
                    parser.SetDelimiters(";");

                    _WaitFormClass.Initwork(1, 1, 4652);
                    while (!parser.EndOfData)
                    {
                        try
                        {
                            string[] columns = parser.ReadFields();

                            GIROInfo _GIROInfo = _onew.FindObject<GIROInfo>(CriteriaOperator.Parse("GIROID=?",columns[0]));
                            if (_GIROInfo == null)
                            {
                                _GIROInfo = _onew.CreateObject<GIROInfo>();
                            }

                            _GIROInfo.GIROID = columns[0];
                            _GIROInfo.ShortName = columns[1];
                            _GIROInfo.Address = columns[2];

                            _onew.CommitChanges();
                            _WaitFormClass.DoWork();
                        }
                        catch (Exception)
                        {
                            throw;
                            _WaitFormClass.FinalWork();
                        }
                    }
                    _WaitFormClass.FinalWork();
                }
            }
        }

    }
}
