using System;
using System.Linq;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Updating;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Security;
using Nuvolar.Module.BusinessObjects.nopCommerce;
using DevExpress.Xpo.DB;
using DevExpress.ExpressApp.Reports;
using System.Reflection;
//using DevExpress.ExpressApp.Reports;
//using DevExpress.ExpressApp.PivotChart;
//using DevExpress.ExpressApp.Security.Strategy;
//using Nuvolar.Module.CSharp.Module.BusinessObjects;

namespace Nuvolar.Module.CSharp.DatabaseUpdate
{
    // For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppUpdatingModuleUpdatertopic
    public class Updater : ModuleUpdater
    {
        public Updater(IObjectSpace objectSpace, Version currentDBVersion) :
            base(objectSpace, currentDBVersion)
        {
        }
        public override void UpdateDatabaseAfterUpdateSchema()
        {
            base.UpdateDatabaseAfterUpdateSchema();
            //string name = "MyName";
            //DomainObject1 theObject = ObjectSpace.FindObject<DomainObject1>(CriteriaOperator.Parse("Name=?", name));
            //if(theObject == null) {
            //    theObject = ObjectSpace.CreateObject<DomainObject1>();
            //    theObject.Name = name;
            //}
            //NAVInvoiceReport _NAVInvoiceReport = ObjectSpace.FindObject<NAVInvoiceReport>(CriteriaOperator.Parse("1=1"));
            //if (_NAVInvoiceReport == null)
            //    _NAVInvoiceReportObjectSpace.CreateObject<DomainObject1>();
            //{
            //}
            nopCParameters _nopCParameters=ObjectSpace.FindObject<nopCParameters>(CriteriaOperator.Parse("1=1"));
            if (_nopCParameters==null)
            {
                _nopCParameters = ObjectSpace.CreateObject<nopCParameters>();
                _nopCParameters.Save();
            }
            // Le kell kérdezni az összes reportod
            XPObjectSpace _ocur = (XPObjectSpace)ObjectSpace;
            Assembly _assembly = Assembly.GetExecutingAssembly();

            SelectedData _SelectedData = _ocur.Session.ExecuteQuery("select ObjectTypeName from ReportData");
            foreach (SelectStatementResultRow _SelectStatementResultRow in _SelectedData.ResultSet[0].Rows)
            {
                foreach (ReportData reportData in ObjectSpace.GetObjects<ReportData>(new BinaryOperator("dataTypeName", _SelectStatementResultRow.Values[0])))
                {
                    try
                    {
                        string _AssemblyQualifiedName = "{0}, Nuvolar.Module, Version=15.2.5.1009, Culture=neutral, PublicKeyToken=null";
                        XafReport report = (XafReport)reportData.LoadReport(_ocur);
                        report.DataType = Type.GetType(string.Format(_AssemblyQualifiedName, _SelectStatementResultRow.Values[0].ToString()));
                        
                        string content = System.Text.Encoding.UTF8.GetString(reportData.Content);
                        if (content.ToString().Contains("SISBusiness"))
                        {
                            string newContent = content.Replace("SISBusiness", "Nuvolar");
                            reportData.Content = System.Text.Encoding.ASCII.GetBytes(newContent);
                        }

                        reportData.SaveReport(report);
                        _ocur.CommitChanges();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
        public override void UpdateDatabaseBeforeUpdateSchema()
        {
            base.UpdateDatabaseBeforeUpdateSchema();
            //if(CurrentDBVersion < new Version("1.1.0.0") && CurrentDBVersion > new Version("0.0.0.0")) {
            //    RenameColumn("DomainObject1Table", "OldColumnName", "NewColumnName");
            //}
        }
    }
}
