using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EffiProz.Core;
using EffiProz.Core.Results;
using EffiProz.Core.Persist;
using EffiProz.Core.Navigators;
using EffiProz.Core.Engine;

namespace Sample1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnFile_Click(object sender, RoutedEventArgs e)
        {
            SessionInterface sessionProxy = DatabaseManager.NewSession(DatabaseURL.S_FILE,
                "TestDb", "sa", "", new EffiProzProperties(), "", 0);

            string sql = "CREATE TABLE Test(ID int, Name varchar(100));";
            Result resultOut = Result.NewExecuteDirectRequest();
            resultOut.SetMainString(sql);

            Result resultIn = sessionProxy.Execute(resultOut);
            if (resultIn.IsError())
            {
                tbkText.Text = "Error in EffiProz";
            }

            sql = "INSERT INTO Test(ID , Name ) VALUES(2,'Bus');";
            resultOut.SetMainString(sql);
            resultIn = sessionProxy.Execute(resultOut);
            if (resultIn.IsError())
            {
                tbkText.Text = "Error in EffiProz";
            }

            sql = "SELECT * FROM TEST;";
            resultOut.SetMainString(sql);
            resultIn = sessionProxy.Execute(resultOut);
            if (resultIn.IsError())
            {
                tbkText.Text = "Error in EffiProz";
            }

            RowSetNavigator nav = resultIn.GetNavigator();
            object[] values = nav.GetNext();

            tbkText.Text = String.Format("ID = {0}, Name = {1} ", values[0], values[1]);

            sessionProxy.Close();
        }

        private void btnMem_Click(object sender, RoutedEventArgs e)
        {
            SessionInterface sessionProxy = DatabaseManager.NewSession(DatabaseURL.S_MEM,
                "Test", "sa", "", new EffiProzProperties(), "", 0);

            string sql = "CREATE TABLE Test(ID int, Name varchar(100));";
            Result resultOut = Result.NewExecuteDirectRequest();
            resultOut.SetMainString(sql);

            Result resultIn = sessionProxy.Execute(resultOut);
            if (resultIn.IsError())
            {              
                tbkText.Text = "Error in EffiProz";
            }

            sql = "INSERT INTO Test(ID , Name ) VALUES(1,'Car');";
            resultOut.SetMainString(sql);
            resultIn = sessionProxy.Execute(resultOut);
            if (resultIn.IsError())
            {
                tbkText.Text = "Error in EffiProz";
            }

            sql = "SELECT * FROM TEST;";
            resultOut.SetMainString(sql);
            resultIn = sessionProxy.Execute(resultOut);
            if (resultIn.IsError())
            {
                tbkText.Text = "Error in EffiProz";
            }

           RowSetNavigator nav =  resultIn.GetNavigator();
           object[] values = nav.GetNext();

           tbkText.Text = String.Format("ID = {0}, Name = {1} ", values[0], values[1]);

           sessionProxy.Close();
        }    
      
    }
}
