using Telerik.TestStudio.Translators.Common;
using Telerik.TestingFramework.Controls.TelerikUI.Blazor;
using Telerik.TestingFramework.Controls.KendoUI.Angular;
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 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;
using ArtOfTest.WebAii.Win32.Dialogs;

namespace TestProject229
{

    public class downloadname : 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...
        private static string downloadtext = "empty";

        private static void MoveMouse()
        {
            System.Threading.Thread.Sleep(500);
            Manager.Current.Desktop.KeyBoard.KeyDown(System.Windows.Forms.Keys.Down);
            System.Threading.Thread.Sleep(500);
            Manager.Current.Desktop.KeyBoard.KeyDown(System.Windows.Forms.Keys.Down);
            System.Threading.Thread.Sleep(500);
            Manager.Current.Desktop.KeyBoard.KeyPress(System.Windows.Forms.Keys.Enter);
            System.Threading.Thread.Sleep(1000);
        }

        private static void CopyToClipboard()
        {
            System.Threading.Thread.Sleep(2000);
            string text = string.Empty;
            Exception ThreadEx = null;
            System.Threading.Thread staThread = new System.Threading.Thread(
            delegate ()
            {
                try
                {
                    //Manager.Current.Desktop.KeyBoard.KeyDown(System.Windows.Forms.Keys.Control);
                    //Manager.Current.Desktop.KeyBoard.KeyPress(System.Windows.Forms.Keys.A);
                    //Manager.Current.Desktop.KeyBoard.KeyUp(System.Windows.Forms.Keys.Control);
                    System.Threading.Thread.Sleep(500);
                    Manager.Current.Desktop.KeyBoard.KeyDown(System.Windows.Forms.Keys.Control);
                    Manager.Current.Desktop.KeyBoard.KeyPress(System.Windows.Forms.Keys.C);
                    Manager.Current.Desktop.KeyBoard.KeyUp(System.Windows.Forms.Keys.Control);
                    System.Threading.Thread.Sleep(200);

                    text = System.Windows.Forms.Clipboard.GetText();
                    System.Windows.Forms.Clipboard.Clear();
                }
                catch (Exception ex)
                {
                    ThreadEx = ex;
                }
            });
            staThread.SetApartmentState(System.Threading.ApartmentState.STA);
            staThread.Start();
            staThread.Join();
            Manager.Current.Log.WriteLine(text);
            downloadtext = text;
        }

        [CodedStep(@"Click on Download button and get window text")]
        public void downloadname_CodedStep2()
        {
            ArtOfTest.WebAii.Win32.Dialogs.IEDownloadDialog ddialog = new IEDownloadDialog(ActiveBrowser, DialogButton.CANCEL, Manager.Desktop); ;
            ArtOfTest.WebAii.Win32.Dialogs.GenericDialog ddialogJS = new GenericDialog(ActiveBrowser, "Save As", true);
            
            if (Manager.ActiveBrowser.BrowserType == BrowserType.InternetExplorer)
            {
                ddialog.HandlerDelegate = new DialogHandlerDelegate(HandelerIE);
                Manager.DialogMonitor.AddDialog(ddialog);
            }
            if(Manager.ActiveBrowser.BrowserType == BrowserType.Chrome)
            {
                ddialogJS.HandlerDelegate = new DialogHandlerDelegate(HandelerChrome);
                Manager.DialogMonitor.AddDialog(ddialogJS);
            }
            
            Manager.DialogMonitor.Start();
            //click to invoke download dialog
            Pages.SampleTextFilesAnd.DownloadLink.Click(false);
            //wait for the filename to be captured before closing the dialog
            System.Threading.Thread.Sleep(5000);
            //Log the filename in the execution log
            Log.WriteLine(downloadtext);
            
            //Close the download dialog in the browser and remove the dialog from monitoring
            Manager.Current.Desktop.KeyBoard.KeyPress(System.Windows.Forms.Keys.Escape);
            Manager.DialogMonitor.RemoveDialog(ddialog);
            Manager.DialogMonitor.RemoveDialog(ddialogJS);
        }
        
        void HandelerIE(IDialog ddialog)
        {
            ddialog.Window.SetActive();
            ddialog.Window.SetFocus();
            Manager.Current.Log.WriteLine(ddialog.Window.Caption.ToString());
            MoveMouse();
            CopyToClipboard();
            Manager.Current.Desktop.KeyBoard.KeyPress(System.Windows.Forms.Keys.Escape);
            ddialog.HandleCount = 1;
            ddialog.CurrentState = DialogCurrentState.Handled;
            ddialog.Window.Close();
        }
        
        void HandelerChrome(IDialog ddialogJS)
        {
            ddialogJS.Window.SetActive();
            ddialogJS.Window.SetFocus();
            Manager.Current.Log.WriteLine(ddialogJS.Window.Caption.ToString());
            CopyToClipboard();
            ddialogJS.HandleCount = 1;
            ddialogJS.CurrentState = DialogCurrentState.Handled;
            ddialogJS.Window.Close();
        }
    }
}
