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 DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Reports;
using DevExpress.XtraReports.UI;
using DevExpress.Persistent.Base.ReportsV2;
using System.IO;

namespace Nuvolar.Module.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class ReportV2_VC : ViewController
    {
        public ReportV2_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 aConvertToReportV2_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            XPObjectSpace _ocur = View.ObjectSpace as XPObjectSpace;
            foreach (ReportData _ReportData in View.SelectedObjects)
            {
                if (_ocur.FindObject<ReportDataV2>(new BinaryOperator("DisplayName", _ReportData.ReportName)) == null)
                {
                    ReportDataV2 reportDataV2 = _ocur.CreateObject<ReportDataV2>();
                    reportDataV2.DisplayName = _ReportData.ReportName;
                    XafReport report = (XafReport)_ReportData.LoadReport(_ocur);
                    XtraReport reportV2 = new XtraReport();
                    CollectionDataSource dataSource = new CollectionDataSource();
                    dataSource.ObjectTypeName = report.DataType.FullName;
                    dataSource.CriteriaString = report.Filtering.Filter;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        report.SaveLayout(ms);
                        ms.Seek(0, SeekOrigin.Begin);
                        reportV2.LoadLayout(ms);
                        reportV2.DataSource = dataSource;
                    }
                    DevExpress.ExpressApp.ReportsV2.ReportDataProvider.ReportsStorage.SaveReport(reportDataV2, reportV2);

                }
            }
            _ocur.CommitChanges();
        }
    }
}
