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 System.IO;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using DevExpress.Xpo;
using System;
using System.Linq;
using DevExpress.Xpo;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.Model;

namespace SISBusiness.Module
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class CustomersOrderVC : ViewController
    {
        public CustomersOrderVC()
        {
            InitializeComponent();
            // Target required Views (via the TargetXXX properties) and create their Actions.
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            Frame.GetController<CustomersOrderVC>().aCustomer_SysnBusinessDirectory.Active.SetItemValue("", false);
            Frame.GetController<CustomersOrderVC>().aOpenWindowsExplorer.Active.SetItemValue("", false);
            if (View.Id == "DBV_OfferOutRows_ListView")
            {
                Frame.GetController<CustomersOrderVC>().aCustomer_SysnBusinessDirectory.Active.SetItemValue("", true);
            }
            if (View.Id.Contains("OfferOutHeader_ListView"))
            {
                Frame.GetController<CustomersOrderVC>().aCustomer_SysnBusinessDirectory.Active.SetItemValue("", true);
            }
            DevExpress.ExpressApp.ListView _ListView = View as DevExpress.ExpressApp.ListView;
            if (_ListView != null)
            {
                IModelListView _IModelListView = (IModelListView)_ListView.Model;
                switch (_IModelListView.ModelClass.Name)
                {
                    case "SISBusiness.Module.Customers":
                    case "SISBusiness.Module.CustomersOrder":
                        Frame.GetController<CustomersOrderVC>().aOpenWindowsExplorer.Active.SetItemValue("", true);        
                        break;
                }
            }
            switch (View.Id)
            {
                case "DBV_OfferOutRows_ListView":
                case "DBV_Products_ListView":
                    Frame.GetController<CustomersOrderVC>().aOpenWindowsExplorer.Active.SetItemValue("", true);
                    break;
            }
        }
        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 aOpenWindowsExplorer_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _ocur = (XPObjectSpace)View.ObjectSpace;
            XPObjectSpace _onew = (XPObjectSpace)Application.CreateObjectSpace();
            XPBaseObject _Object = (XPBaseObject)View.SelectedObjects[0];
            
            DevExpress.ExpressApp.ListView _ListView = (DevExpress.ExpressApp.ListView)View;
            if (_ListView != null) 
            {
                IModelListView _IModelListView = (IModelListView)_ListView.Model;
                switch (_IModelListView.ModelClass.Name)
                {
                    case "SISBusiness.Module.Products":
                        _Object = ((Products)_Object).ProductGroups;
                        break;
                    case "SISBusiness.Module.Contacts":
                        _Object = ((Contacts)_Object).Customers;
                        break;
                    case "SISBusiness.Module.OfferOutRows":
                        _Object = ((OfferOutRows)_Object).OfferOutHeader;
                        break;
    
                }
            }
            try
            {
                if (_Object != null)
                {
                    if (string.IsNullOrEmpty(_Object.GetMemberValue("DocumentPath").ToString()) == false)
                    {
                        if (Directory.Exists(_Object.GetMemberValue("DocumentPath").ToString()))
                        {
                            Process.Start(_Object.GetMemberValue("DocumentPath").ToString());
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            
        }

        private void aCustomer_SysnBusinessDirectory_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
              XPObjectSpace _ocur= (XPObjectSpace)View.ObjectSpace;
              Customers _Customers=null;
              OfferOutHeader _OfferOutHeader = null;
              switch (View.Id)
              {
                  case "DBV_OfferOutRows_ListView":
                      _OfferOutHeader = ((OfferOutRows)View.SelectedObjects[0]).OfferOutHeader;
                      _Customers=_OfferOutHeader.Customers;
                      break;
                  case "OfferOutHeader_ListView":
                  case "OfferOutHeader_ListView_Editable":
                      _OfferOutHeader = ((OfferOutHeader)View.SelectedObjects[0]);
                      _Customers = _OfferOutHeader.Customers;
                      break;
                  default:
                      return;
                      break;
              }
              if (_Customers.MasterBusinessDirectory == null)
              {
                  BusinessDirectory _CustomersBaseBusinessDirectory = _ocur.FindObject<BusinessDirectory>(CriteriaOperator.Parse("ShortName='Ügyfelek' AND IsNull(BusinessDirectory_Parent)=True"));

                  if (_CustomersBaseBusinessDirectory == null)
                  {
                      _CustomersBaseBusinessDirectory = new BusinessDirectory(_ocur.Session);
                      var _with1 = _CustomersBaseBusinessDirectory;
                      _with1.ShortName = "Ügyfelek";
                      _with1.BusinessDirectory_Parent = null;
                  }

                  BusinessDirectory _CustomersBusinessDirectory = new BusinessDirectory(_ocur.Session);
                  _CustomersBusinessDirectory.ShortName = string.Format("{0}  {1} {2}", _Customers.FullName, _Customers.Address1.City, _Customers.CustomerNumber);
                  _CustomersBusinessDirectory.BusinessDirectory_Parent = _CustomersBaseBusinessDirectory;
                  _CustomersBusinessDirectory.Save();
                  _Customers.MasterBusinessDirectory = _CustomersBusinessDirectory;
                  _ocur.CommitChanges();
              }

                if (_OfferOutHeader.MasterBusinessDirectory == null) 
                {
                    BusinessDirectory _OfferOutBusinessDirectory = new BusinessDirectory(_ocur.Session);
                    _OfferOutBusinessDirectory.ShortName = string.Format("{0}", _OfferOutHeader.DocumentNumber);
                    _OfferOutBusinessDirectory.BusinessDirectory_Parent = _OfferOutHeader.Customers.MasterBusinessDirectory;
                    _OfferOutBusinessDirectory.Save();
                    _OfferOutHeader.MasterBusinessDirectory = _OfferOutBusinessDirectory;
                    _ocur.CommitChanges();
                }
        }
    }
}
