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 System.Collections.Generic;
using System.Reflection;
using System.Threading;
using EffiProz.Core.Results;
using EffiProz.Core.Engine;
using EffiProz.Core.Persist;

namespace SelfTestSL
{
    public class TestStatus
    {
        public TestStatus(string name, string status)
        {
            Name = name;
            Status = status;
        }
        public string Name { get; set; }
        public string Status { get; set; }
    }

   

    public partial class MainPage : UserControl
    {
        List<TestStatus> _testList = new List<TestStatus>();
        Thread _worker = null;
        int _count = 0;

        public MainPage()
        {
            InitializeComponent();          
            dg.ItemsSource = _testList;
            ExecuteScripts();
        }

        public delegate void UpdateDelegate(string name, string status);

        private void UpdateGrid(string name, string status)
        {
            _testList.Add(new TestStatus( String.Format("{0}. {1}", ++_count, name), status ));
            dg.ItemsSource = null;
            dg.ItemsSource = _testList;    
        }

        private void ExecuteScripts()
        {
            _worker = new Thread(new ThreadStart(RunTest));
            _worker.Start();

        }

        private void RunTest()
        {

            string[] files = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            foreach (var script in files)
            {
                //if (!_started)
                //    break;
                if (!script.StartsWith("SelfTestSL.Scripts"))
                    continue;
                string name = script.Substring("SelfTestSL.Scripts".Length + 1);

              
                bool success = false;
                SessionInterface sessionProxy = ConnectToMemoryDB();
                try
                {
                    success = TestUtil.testScript(sessionProxy, script);

                }
                catch (Exception) { }
                finally
                {
                    ShutDown(sessionProxy);
                }
                object[] vals = new object[] { name, success ? "Passed" : "Failed" };

                dg.Dispatcher.BeginInvoke(new UpdateDelegate(UpdateGrid), vals);
            }
        }

        private SessionInterface ConnectToMemoryDB()
        {
            string dbName = GetNewDatabaseName();
            EffiProzProperties props = new EffiProzProperties();
            SessionInterface sessionProxy = DatabaseManager.NewSession(DatabaseURL.S_MEM, dbName,
                      "SA", "", props, "", 0);
            return sessionProxy;
        }

        public void ShutDown(SessionInterface sessionProxy)
        {
            Result resultOut = Result.NewExecuteDirectRequest();
            resultOut.SetMainString("SHUTDOWN;");
            sessionProxy.Execute(resultOut);
            sessionProxy.Close();
            //CleanConn();
            sessionProxy = null;
            System.GC.Collect();
        }

        static int databaseCounter = 0;

        public static string GetNewDatabaseName()
        {
            return "DB" + databaseCounter++;
        }
    }

    
}
