using Telerik.TestingFramework.Controls.KendoUI;
using Telerik.WebAii.Controls.Html;
using Telerik.WebAii.Controls.Xaml;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data.SqlClient;
using System.ServiceProcess;

using ArtOfTest.Common.UnitTesting;
using ArtOfTest.WebAii.Core;
using ArtOfTest.WebAii.Controls.HtmlControls;
using ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts;
using ArtOfTest.WebAii.Design;
using ArtOfTest.WebAii.Design.Execution;
using ArtOfTest.WebAii.ObjectModel;
using ArtOfTest.WebAii.Silverlight;
using ArtOfTest.WebAii.Silverlight.UI;

namespace MainpacUITest
{

    //
    // You can add custom execution steps by simply
    // adding a void function and decorating it with the [CodedStep] 
    // attribute to the test method. 
    // Those steps will automatically show up in the test steps on save.
    //
    // The BaseWebAiiTest exposes all key objects that you can use
    // to access the current testcase context. [i.e. ActiveBrowser, Find ..etc]
    //
    // Data driven tests can use the Data[columnIndex] or Data["columnName"] 
    // to access data for a specific data iteration.
    //
    // Example:
    //
    // [CodedStep("MyCustom Step Description")]
    // public void MyCustomStep()
    // {
    //        // Custom code goes here
    //      ActiveBrowser.NavigateTo("http://www.google.com");
    //
    //        // Or
    //        ActiveBrowser.NavigateTo(Data["url"]);
    // }
    //
        

    public class RestoreDatabase : BaseWebAiiTest
    {
        #region [ Dynamic Pages Reference ]

        private Pages _pages;

        /// <summary>
        /// Gets the Pages object that has references
        /// to all the elements, frames or regions
        /// in this project.
        /// </summary>
        public Pages Pages
        {
            get
            {
                if (_pages == null)
                {
                    _pages = new Pages(Manager.Current);
                }
                return _pages;
            }
        }

        #endregion
        
        // Add your test methods here... 
        [CodedStep(@"New Coded Step")]
        public void RestoreDatabase_CodedStep()
        {
            try
            {
                string strService = "MSSQLSERVER";
                ServiceController serv = new ServiceController(strService);
                if (serv != null)
                {
                    serv.Stop();
                    serv.WaitForStatus(ServiceControllerStatus.Stopped);
    
                    serv.Start();
                    serv.WaitForStatus(ServiceControllerStatus.Running);
                }
                Log.WriteLine("Restart service MSSQLSERVER successfully");
                
                strService = "IISADMIN";
                serv = new ServiceController(strService);
                if (serv != null)
                {
                    serv.Stop();
                    serv.WaitForStatus(ServiceControllerStatus.Stopped);
    
                    serv.Start();
                    serv.WaitForStatus(ServiceControllerStatus.Running);
                }
                Log.WriteLine("Restart service IISADMIN successfully");
                
                //string strConn = ConfigurationManager.AppSettings["MainpacAdvConnection"].ToString();
                string path = @"D:\Work\Mainpac\MainpacUITest\MainpacData";
                string sqlRestore = "Use master Restore Database [Mainpac2011] from disk='" + path + "'";
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Mainpac2011;Integrated Security=True");
                if(string.Equals(con.State.ToString(),"Closed"))
                {
                    con.Close();
                    con.Open();
                }

                SqlCommand cmd = new SqlCommand(sqlRestore, con);
                cmd.ExecuteNonQuery();
                con.Close();
                Log.WriteLine("Restore database successfully");
            }
            catch (SqlException sqlEx)
            {
                Log.WriteLine(sqlEx.ToString());
                RestoreDatabase_CodedStep();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Restore database failed");
            }
        }
    }
}
