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.ExpressApp.Xpo;
using Nuvolar.Module.BusinessObjects;
using DevExpress.Xpo;

namespace Nuvolar.Module.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class StoragePDACollectionHeader_VC : Nuvolar.Module.StoragePDACollectionHeaderVC
    {
        public StoragePDACollectionHeader_VC()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
        }
        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 aStoragePDARow_Divide_Execute(object sender, PopupWindowShowActionExecuteEventArgs e)
        {
            XPObjectSpace _ocur = (XPObjectSpace)View.ObjectSpace;
            XPObjectSpace _onew = (XPObjectSpace)Application.CreateObjectSpace();
            StoragePDACollectionRowsH _StoragePDACollectionRowsH = (StoragePDACollectionRowsH)e.PopupWindow.View.SelectedObjects[0];
            StoragePDACollectionRows _StoragePDACollectionRows = (StoragePDACollectionRows)View.SelectedObjects[0];

            if (_StoragePDACollectionRowsH.QTT_Will > 0 && _StoragePDACollectionRows.QTT>= _StoragePDACollectionRowsH.QTT_Will)
            {
                StoragePDACollectionRows _StoragePDACollectionRows_Divided = _ocur.CreateObject<StoragePDACollectionRows>();
                _StoragePDACollectionRows_Divided.ConnectOid = _StoragePDACollectionRows.Oid;
                _StoragePDACollectionRows.ConnectOid = _StoragePDACollectionRows.Oid;
                _StoragePDACollectionRows.QTT = _StoragePDACollectionRows.QTT - _StoragePDACollectionRowsH.QTT_Will;

                _StoragePDACollectionRows_Divided.Barcode = _StoragePDACollectionRows.Barcode;
                _StoragePDACollectionRows_Divided.Description = _StoragePDACollectionRows.Description;
                _StoragePDACollectionRows_Divided.PacketNumber = _StoragePDACollectionRows.PacketNumber;
                _StoragePDACollectionRows_Divided.Products = _StoragePDACollectionRows.Products;
                _StoragePDACollectionRows_Divided.QTT = _StoragePDACollectionRowsH.QTT_Will;
                _StoragePDACollectionRows_Divided.SerialNumber = "";
                _StoragePDACollectionRows_Divided.StorageLocation = null;
                _StoragePDACollectionRows_Divided.StoragePDACollectionHeader = _StoragePDACollectionRows.StoragePDACollectionHeader;
                _ocur.CommitChanges();
             }

        }
        private void aStoragePDARow_Divide_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e)
        {
            XPObjectSpace _ocur = (XPObjectSpace)View.ObjectSpace;
            XPObjectSpace _onew = (XPObjectSpace)Application.CreateObjectSpace();
            StoragePDACollectionRowsH _StoragePDACollectionRowsH=_onew.CreateObject<StoragePDACollectionRowsH>();

            StoragePDACollectionRows _StoragePDACollectionRows = (StoragePDACollectionRows)View.SelectedObjects[0];
            Guid _ConnectOid = _StoragePDACollectionRows.ConnectOid;

            double _QTT_Remaining = 0;
            double _QTT_Divided = 0;
            double _QTT_Original = 0;
            if (_ConnectOid==Guid.Empty)
            {
                _QTT_Remaining = _StoragePDACollectionRows.QTT;
                _QTT_Divided = 0;
                _QTT_Original = _StoragePDACollectionRows.QTT;
            }
            else
            {
                XPCollection<StoragePDACollectionRows> _StoragePDACollectionRows_Collection = new XPCollection<StoragePDACollectionRows>(_ocur.Session, 
                    CriteriaOperator.Parse("ConnectOid=? and StoragePDACollectionHeader=?", _ConnectOid,_StoragePDACollectionRows.StoragePDACollectionHeader));
                foreach (StoragePDACollectionRows _SPDACR in _StoragePDACollectionRows_Collection)
                {
                    _QTT_Original += _SPDACR.QTT;
                    if (_SPDACR.StorageLocation!=null)
                    {
                        _QTT_Divided += _SPDACR.QTT;
                    }
                }
                _QTT_Remaining = _QTT_Original - _QTT_Divided;
            }
            _StoragePDACollectionRowsH.QTT_Original = _QTT_Original;
            _StoragePDACollectionRowsH.QTT_Divided = _QTT_Divided;
            _StoragePDACollectionRowsH.QTT_Remaining = _QTT_Remaining;

            e.View = Application.CreateDetailView(_onew, "StoragePDACollectionRowsH_DetailView", true, _StoragePDACollectionRowsH);
        }

        private void aStoragePDARow_Merge_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _ocur = (XPObjectSpace)View.ObjectSpace;
            StoragePDACollectionRows _StoragePDACollectionRows_Base = (StoragePDACollectionRows)View.SelectedObjects[0];
            List<StoragePDACollectionRows> _CC = new List<StoragePDACollectionRows>();
            foreach (StoragePDACollectionRows _StoragePDACollectionRows in View.SelectedObjects)
            {
                if (_StoragePDACollectionRows.Oid!=_StoragePDACollectionRows_Base.Oid)
                {
                    if (_StoragePDACollectionRows.ConnectOid==_StoragePDACollectionRows_Base.ConnectOid)
                    {
                        if (_StoragePDACollectionRows.StorageLocation==_StoragePDACollectionRows_Base.StorageLocation)
                        {
                            _CC.Add(_StoragePDACollectionRows);
                            _StoragePDACollectionRows_Base.QTT = _StoragePDACollectionRows_Base.QTT + _StoragePDACollectionRows.QTT;
                        }
                    }
                }

            }
            _ocur.Delete(_CC);
            _ocur.CommitChanges();
        }
    }
}
