using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Xpo;
using Nuvolar.Module.BusinessObjects;
//using Nuvolar.Module.Win;

namespace Nuvolar.Module.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class NewCustomersFromVC : ViewController
    {
        //private WaitFormClass _WaitFormClass;
        private XPObjectSpace _onew;
        private int _Counter = 0;
        public NewCustomersFromVC()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            //_WaitFormClass = new WaitFormClass();
            // 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 aNewCustomersFrom_Customize(object sender, CustomizePopupWindowParamsEventArgs e)
        {
            XPObjectSpace _onew = (XPObjectSpace)Application.CreateObjectSpace();
            NewCustomersFrom _NewCustomersFrom = _onew.CreateObject<NewCustomersFrom>();
            CustomersFrom _CustomersFrom = _onew.FindObject<CustomersFrom>(CriteriaOperator.Parse("IsDefault=True"));
            if (_CustomersFrom != null)
            {
                if (_CustomersFrom.CustomerBankAccounts.Count == 1)
                {
                    _NewCustomersFrom.Bank_AccountNumber = _CustomersFrom.CustomerBankAccounts[0].AccountNumber;
                    _NewCustomersFrom.Bank_IBAN = _CustomersFrom.CustomerBankAccounts[0].IBAN;
                    _NewCustomersFrom.Bank_SWIFT = _CustomersFrom.CustomerBankAccounts[0].SWIFT;
                    _NewCustomersFrom.Bank_ShortName = _CustomersFrom.CustomerBankAccounts[0].ShortName;
                }
            }
            e.View = Application.CreateDetailView(_onew, "NewCustomersFrom_DetailView_Stock", true, _NewCustomersFrom);
        }
        private void aNewCustomersFrom_Execute(object sender, PopupWindowShowActionExecuteEventArgs e)
        {
            Frame.GetController<DevExpress.ExpressApp.Validation.PersistenceValidationController>().Active.SetItemValue("", false);
            NewCustomersFrom _NewCustomersFrom = (NewCustomersFrom)e.PopupWindow.View.SelectedObjects[0];
            _onew = (XPObjectSpace)e.PopupWindow.View.ObjectSpace;

            if (_NewCustomersFrom!=null) {
                
                _NewCustomersFrom.InitWork += NewCustomersFrom_InitWork;
                _NewCustomersFrom.DoWork += NewCustomersFrom_DoWork;
                _NewCustomersFrom.FinalWork += NewCustomersFrom_FinalWork;

                GeneralFunction.GLOBAL_HAS_Audit = false;
                _NewCustomersFrom.AddNewCustomersFrom();
                _onew.CommitChanges();
                GeneralFunction.GLOBAL_HAS_Audit = true;
            }
            Frame.GetController<DevExpress.ExpressApp.Validation.PersistenceValidationController>().Active.SetItemValue("", true);
        }
        private void aGenerateDEMOData_Customize(object sender, CustomizePopupWindowParamsEventArgs e)
        {
            XPObjectSpace _onew = (XPObjectSpace)Application.CreateObjectSpace();
            NewCustomersFrom _NewCustomersFrom = _onew.CreateObject<NewCustomersFrom>();
            CustomersFrom _CustomersFrom = _onew.FindObject<CustomersFrom>(CriteriaOperator.Parse("IsDefault=True"));

            _NewCustomersFrom.CustomersFrom = _CustomersFrom;
            e.View = Application.CreateDetailView(_onew, "NewCustomersFrom_DetailView_Demo", true, _NewCustomersFrom);
        }
        private void aGenerateDEMOData_Execute(object sender, PopupWindowShowActionExecuteEventArgs e)
        {
            NewCustomersFrom _NewCustomersFrom = (NewCustomersFrom)e.PopupWindow.View.SelectedObjects[0];
            _onew = (XPObjectSpace)e.PopupWindow.View.ObjectSpace;

            if (_NewCustomersFrom != null)
            {

                _NewCustomersFrom.InitWork += NewCustomersFrom_InitWork;
                _NewCustomersFrom.DoWork += NewCustomersFrom_DoWork;
                _NewCustomersFrom.FinalWork += NewCustomersFrom_FinalWork;

                GeneralFunction.GLOBAL_HAS_Audit = false;
                _NewCustomersFrom.GenerateDEMOData(_onew);
                _onew.CommitChanges();
                GeneralFunction.GLOBAL_HAS_Audit = true;
            }
        }
        private void NewCustomersFrom_FinalWork(object sender, WorkEventArgs e)
        {
            //_WaitFormClass.FinalWork();
        }
        private void NewCustomersFrom_DoWork(object sender, WorkEventArgs e)
        {
            //_WaitFormClass.DoWork();
            _Counter++;

            if (_Counter % 100 == 0)
            {
                _onew.CommitChanges();
            }
       }
        private void NewCustomersFrom_InitWork(object sender, WorkEventArgs e)
        {
            //_WaitFormClass.Initwork(1, 1, e.Count);
            //_WaitFormClass.Caption = e.Message;          
        }
       
    }
}
