using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using Nuvolar.Module.BusinessObjects.nopCommerce;

namespace Nuvolar.Module.Controllers.nopCommerce
{
    // For more typical usage scenarios, be sure to check out https://documentation.devexpress.com/eXpressAppFramework/clsDevExpressExpressAppViewControllertopic.aspx.
    public partial class nopC_VC : ViewController
    {
        public nopC_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 anopC_LoadHU_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            nopCParameters _nopCParameters=(nopCParameters)View.SelectedObjects[0];

            StreamReader _textStreamReader;
            string _StrBuffer;
            Assembly _assembly = Assembly.GetExecutingAssembly();
            _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("Nuvolar.Module.XML.nopCommerce_hu.xml"));
            _StrBuffer = _textStreamReader.ReadToEnd();
            DevExpress.Xpo.Session _Session = new DevExpress.Xpo.Session();

            _Session.ConnectionString = DevExpress.Xpo.DB.MSSqlConnectionProvider.GetConnectionString(string.Format("{0},{1}", _nopCParameters.SQLServer, _nopCParameters.SQLPort),
                _nopCParameters.SQLUserName, _nopCParameters.SQLPassword, "NCC");
            _Session.AutoCreateOption = DevExpress.Xpo.DB.AutoCreateOption.SchemaAlreadyExists;

            if (_Session.IsConnected) { _Session.Disconnect(); };


            _Session.Connect();
            XmlReaderSettings _XmlReaderSettings = new XmlReaderSettings();
            using (XmlReader _XMLReader = XmlReader.Create(new StringReader(_StrBuffer)))
            {
               
                while (_XMLReader.Read())
                {
                    string _ResourceName;
                    string _ResourceValue;
                    _XMLReader.ReadToFollowing("LocaleResource");
                    _XMLReader.MoveToFirstAttribute();
                    _ResourceName = _XMLReader.Value;
                    _XMLReader.ReadToFollowing("Value");
                    if (_XMLReader.Name=="Value")
                    {
                        _ResourceValue = _XMLReader.ReadElementContentAsString();
                        //string _SQL = "if not exists";
                        string _SQL= " delete from [NCC].[dbo].[LocaleStringResource] where [LanguageId]=2 and [ResourceName]='{0}' and [ResourceValue]='{1}'\n";
                        _SQL += " insert [NCC].[dbo].[LocaleStringResource] ([LanguageId],[ResourceName],[ResourceValue]) select 2,'{0}','{1}' ";
                        _Session.ExecuteNonQuery(string.Format(_SQL,_ResourceName,_ResourceValue.Replace("'","''")));
                    }
                }
            }
        }
    }
}
