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;

namespace SISBusiness.Module.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class ZoomListViewVC : ViewController
    {
        public ZoomListViewVC()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            Frame.GetController<ZoomListViewVC>().aZoomListView.Active.SetItemValue("", false);
            if (View.Id.Contains("BusinessDirectory"))
            {
                Frame.GetController<ZoomListViewVC>().aZoomListView.Active.SetItemValue("", true);
            
            }
            if (View.Id == "DBV_OfferOutRows_ListView")
            {
                Frame.GetController<ZoomListViewVC>().aZoomListView.Active.SetItemValue("", true);
            }
            if (View.Id == "DBV_OrderInRows_ListView")
            {
                Frame.GetController<ZoomListViewVC>().aZoomListView.Active.SetItemValue("", true);
            }
            if (View.Id == "BusinessDirectory_ListView_Zoom")
            {
                Frame.GetController<ZoomListViewVC>().aZoomListView.Active.SetItemValue("", false);
                Frame.GetController<SISBusiness.Module.BusinessDirectory_VC>().aBusinessDirectory_SetRoleEnabled.Active.SetItemValue("", false);
                Frame.GetController<SISBusiness.Module.BusinessDirectory_VC>().aBusinessDirectory_SetRoleDisabled.Active.SetItemValue("", false);
                Frame.GetController<SISBusiness.Module.BusinessDirectory_VC>().aBusinessDirectory_NewDashboard.Active.SetItemValue("", false);
                Frame.GetController<SISBusiness.Module.BusinessDirectory_VC>().aBusinessDirectory_OpenDashboardWithDesigner.Active.SetItemValue("", false);
                //Frame.GetController<SISBusiness.Module.BusinessDirectory_VC>().aBusinessDirectory_OpenDashboardWithViewer.Active.SetItemValue("", false);
            }
            if (View.Id == "Customers_Customers_BusinessDirectory_ListView") 
            {
                Frame.GetController<SISBusiness.Module.BusinessDirectory_VC>().aBusinessDirectory_SetRoleEnabled.Active.SetItemValue("", false);
                Frame.GetController<SISBusiness.Module.BusinessDirectory_VC>().aBusinessDirectory_SetRoleDisabled.Active.SetItemValue("", false);
                Frame.GetController<SISBusiness.Module.BusinessDirectory_VC>().aBusinessDirectory_NewDashboard.Active.SetItemValue("", false);
                Frame.GetController<SISBusiness.Module.BusinessDirectory_VC>().aBusinessDirectory_OpenDashboardWithDesigner.Active.SetItemValue("", false);
                Frame.GetController<SISBusiness.Module.BusinessDirectory_VC>().aBusinessDirectory_OpenDashboardWithViewer.Active.SetItemValue("", false);
            }
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            
        }
        protected override void OnDeactivated()
        {
            if (View.Id.Contains("BusinessDirectory"))
            {
                Frame.GetController<ZoomListViewVC>().aZoomListView.Active.SetItemValue("", false);
            
            }
            if (View.Id == "DBV_OfferOutRows_ListView")
            {
                Frame.GetController<ZoomListViewVC>().aZoomListView.Active.SetItemValue("", false);
            }
            if (View.Id == "DBV_OrderInRows_ListView")
            {
                Frame.GetController<ZoomListViewVC>().aZoomListView.Active.SetItemValue("", false);
            }
            base.OnDeactivated();
        }

        private void aZoomListView_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _ocur = (XPObjectSpace)View.ObjectSpace;
            XPObjectSpace _onew = (XPObjectSpace)_ocur.CreateNestedObjectSpace();
            CollectionSource _CollectionSource=new CollectionSource(_onew,typeof(BusinessDirectory));
            string _ListViewID = "BusinessDirectory_ListView_Zoom";
            _CollectionSource.BeginUpdateCriteria();
            _CollectionSource.Criteria.Add("Clear",CriteriaOperator.Parse("1=2"));
            _CollectionSource.EndUpdateCriteria();

            CollectionSourceBase _CollectionSourceOriginal=null;
            if (View.Id.Contains("BusinessDirectory"))
            {
                _CollectionSourceOriginal = ((ListView)View).CollectionSource;
                if (_CollectionSourceOriginal.List!=null) 
                {
                    foreach (var item in _CollectionSourceOriginal.List)
                    {
                        _CollectionSource.Add(_onew.GetObject(item));    
                    }
                }
            }
            if (View.Id == "DBV_OfferOutRows_ListView") {
                if (View.SelectedObjects.Count > 0)
                {
                    _ListViewID = "OfferOutRows_ListView_Zoom";
                    _CollectionSource = new CollectionSource(_onew, typeof(OfferOutRows));
                    _CollectionSource.BeginUpdateCriteria();
                    _CollectionSource.Criteria.Add("Clear", CriteriaOperator.Parse("1=2"));
                    _CollectionSource.EndUpdateCriteria();
                    _CollectionSourceOriginal = ((ListView)View).CollectionSource;
                    if (_CollectionSourceOriginal.List != null)
                    {
                        for (int i = 0; i < _CollectionSourceOriginal.List.Count; i++)
                        {
                            _CollectionSource.Add(_onew.GetObject(_CollectionSourceOriginal.List[i]));
                        }
                    }
                }
            }
            if (View.Id == "DBV_OrderInRows_ListView")
            {
                if (View.SelectedObjects.Count > 0)
                {
                    _ListViewID = "OrderInRows_ListView_Zoom";
                    _CollectionSource = new CollectionSource(_onew, typeof(OrderInRows));
                    _CollectionSource.BeginUpdateCriteria();
                    _CollectionSource.Criteria.Add("Clear", CriteriaOperator.Parse("1=2"));
                    _CollectionSource.EndUpdateCriteria();
                    _CollectionSourceOriginal = ((ListView)View).CollectionSource;
                    if (_CollectionSourceOriginal.List != null)
                    {
                        for (int i = 0; i < _CollectionSourceOriginal.List.Count; i++)
                        {
                            _CollectionSource.Add(_onew.GetObject(_CollectionSourceOriginal.List[i]));
                        }
                    }
                }
            }

            ShowViewParameters _ShowViewParameters=new ShowViewParameters();
            ShowViewSource _ShowViewSource = new ShowViewSource(Frame, null);

            _ShowViewParameters.CreatedView = Application.CreateListView(_ListViewID, _CollectionSource, false);
            _ShowViewParameters.NewWindowTarget = NewWindowTarget.Separate;
            _ShowViewParameters.TargetWindow = TargetWindow.NewModalWindow;
            
            Application.ShowViewStrategy.ShowView(_ShowViewParameters, _ShowViewSource);
        }
    }
}
