using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.ReportsV2;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using System.Diagnostics;
using DevExpress.XtraReports;

namespace Nuvolar.Module.Controllers
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class InvoiceHeader_VC : ViewController
    {
        public InvoiceHeader_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 aPrintSelectedInvoicesV2_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            //var stopwatch = new Stopwatch();
            //stopwatch.Start();

            XPObjectSpace _ocur = View.ObjectSpace as XPObjectSpace;
            InvoiceHeader _InvoiceHeader = View.SelectedObjects[0] as InvoiceHeader;
            IReportDataV2 reportData = _ocur.FindObject<ReportDataV2>(CriteriaOperator.Parse("[DisplayName] = 'Kimenő számla'"));

            string handle = ReportDataProvider.ReportsStorage.GetReportContainerHandle(reportData);
            Frame.GetController<ReportServiceController>().ShowPreview(handle,CriteriaOperator.Parse("InvoiceHeader.Oid=?",_InvoiceHeader.Oid));
            //stopwatch.Stop();
            //long elapsed_time = stopwatch.ElapsedMilliseconds;

            //MessageBox.Show(elapsed_time.ToString("#,##0.00"), "Time", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void aPrintSelectedInvoicesV2V_Execute(object sender, SimpleActionExecuteEventArgs e)
        {

            Debug.WriteLine("============ REPORT START ===========================================================================");
            XPObjectSpace _ocur = View.ObjectSpace as XPObjectSpace;
            InvoiceHeader _InvoiceHeader = View.SelectedObjects[0] as InvoiceHeader;
            IReportDataV2 reportData = _ocur.FindObject<ReportDataV2>(CriteriaOperator.Parse("[DisplayName] = 'Kimenő számla'"));
            XtraReport report = ReportDataProvider.ReportsStorage.LoadReport(reportData);
            var dataSource = (DevExpress.Persistent.Base.ReportsV2.ISupportCriteria)report.DataSource;
            dataSource.Criteria = CriteriaOperator.Parse("InvoiceHeader.Oid=? and InvoiceHeader.Customers.Oid=?", _InvoiceHeader.Oid,_InvoiceHeader.Customers.Oid);

            string handle = ReportDataProvider.ReportsStorage.GetReportContainerHandle(reportData);
            //Frame.GetController<ReportServiceController>().SetupBeforePrint(report, null, CriteriaOperator.Parse("InvoiceHeader.Oid=?", _InvoiceHeader.Oid), true, null, false);
            Frame.GetController<ReportServiceController>().SetupBeforePrint(report, null,null , false, null, false);
            //report.PrintDialog();
            report.ShowPreview();
            Debug.WriteLine("============ REPORT STOP ===========================================================================");
        }

        private void aPrintSelectedInvoicesV2PDF_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            Debug.WriteLine("============ REPORT START ===========================================================================");
            XPObjectSpace _ocur = View.ObjectSpace as XPObjectSpace;
            InvoiceHeader _InvoiceHeader = View.SelectedObjects[0] as InvoiceHeader;
            //IReportDataV2 reportData = _ocur.FindObject<ReportDataV2>(CriteriaOperator.Parse("[DisplayName] = 'Kimenő számla'"));
            //XtraReport report = ReportDataProvider.ReportsStorage.LoadReport(reportData);

            Nuvolar.Module.ReportV2.Report_Invoice _RI=new Nuvolar.Module.ReportV2.Report_Invoice();
            //ReportPrintTool _PrintTool = new ReportPrintTool(_RI as IReport);


            //string handle = ReportDataProvider.ReportsStorage.GetReportContainerHandle(reportData);
            //Frame.GetController<ReportServiceController>().ShowPreview(handle, CriteriaOperator.Parse("InvoiceHeader.Oid=?", _InvoiceHeader.Oid));
            _RI.FilterString = CriteriaOperator.Parse("InvoiceHeader=?", _InvoiceHeader.Oid).ToString();

            //var dataSource = (DevExpress.Persistent.Base.ReportsV2.ISupportCriteria)_RI.DataSource;
            
            Frame.GetController<ReportServiceController>().SetupBeforePrint(_RI, null, CriteriaOperator.Parse("InvoiceHeader=?",_InvoiceHeader.Oid), true, null, false);
            _RI.ShowPreview();
            Debug.WriteLine("============ REPORT STOP ===========================================================================");
            //_RI.DataSource=_ocur.Session.da
            //(_RI.DataSource as DevExpress.DataAccess.Sql.SqlDataSource).Connection.ConnectionString = _ocur.Session.ConnectionString;

            //_RI.ExportToPdf(@"c:\temp\kutya.pdf");
            //Process.Start(@"c:\temp\kutya.pdf");
        }
    }
}
