using FWHandler;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UITests
{
    public class UITestFixture
    {
        protected string appUrl = PathProvider.PathToApp;
        protected int waitStandart = (int)EnumProviderTryTiming.StandartTry;
        protected int waitSlow = (int)EnumProviderTryTiming.SlowTry;
        protected int waitFast = (int)EnumProviderTryTiming.FastTry;
        protected string ErrorPicturePathway = string.Empty;
        //private static bool buildInfoIsWritten = false;

        [OneTimeSetUp]
        public virtual void PreCondition()
        {
            //App.AppStart(appUrl);
            Console.WriteLine("{0}", "Notepad 1.0");
            ValueProvider.DevtoolsVersion = "1.00";
            ValueProvider.DevtoolsBuildInfo = "5668";

        }
        [SetUp]
        public virtual void Haha()
        {
            App.AppStart(appUrl);
        }
        [TearDown]
        public virtual void PostCondition()
        {
            if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
            {
                CaptureScr.CaptureScreen(ErrorPicturePathway);
                Console.WriteLine("{0} Screen:{1}", GuesErrorProvider.GuessError_NoAssertError, ErrorPicturePathway);
            }
            App.AppClose();
        }
        [OneTimeTearDown]
        public virtual void PostCondition_Final()
        {
           // App.AppClose(waitStandart);
        }

        protected virtual void InitUiTest([CallerMemberName] string methodName = null)
        {
            ErrorPicturePathway = PathProvider.PathToErrorScreen + methodName + ".jpg";
        }

        protected string TakeStringBetween(string str, string firstPart, string secondPart)
        {
            int firstIndex = str.IndexOf(firstPart) + firstPart.Length;
            int secondIndex = str.LastIndexOf(secondPart);
            return str.Substring(firstIndex, secondIndex - firstIndex);
        }
     

        protected void WaitUntilFinish(string element, string param1, string param2)
        {
            int counter = 0;
            string name = Window.ElementGetName(element, 4);
            if (name.Contains(param1) && name.Contains(param2))
            {
                try
                {
                    while (Label.IsVisible(name) == true && counter != 10)
                    {
                        Thread.Sleep(1000);
                        counter++;
                    }
                }
                catch (Exception ex) { }
            }
        }
        protected void WaitUntilActiveIsFinished(string element)// Window.ElementExists("LongOperationForm" == true)
        {
            int counter = 0;
            try
            {
                while (Window.ElementExists(element) && counter != 10)
                {
                    Thread.Sleep(waitFast);
                    counter++;
                }
            }
            catch { }
        }

       


    }
}



