using System;
using System.IO;
using System.Security;
using System.Threading;
using System.Drawing.Imaging;

using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;

using TechTalk.SpecFlow;
using Testing.Api;

using Microsoft.Dynamics365.UIAutomation.Api;
using Microsoft.Dynamics365.UIAutomation.Browser;

namespace Testing.Specs
{
    /*
    // PUT THE ROLLOW IN FEATURE FILE (LandingPage.feature)

    Feature: LandingPage
        In order to avoid silly mistakes
        As a math idiot
        I want to be told the sum of two numbers

    @mytag
    Scenario: Login with special credential
        Given I have logged in as 'User01.Test'
      And I navigate to 'Landing Page'
        And I click button 'MeFlex'
        When I click link 'My account'
      Then I click link 'Personal info'
        Then I take a screen shot
      Then I click link 'Subscriptions'
      Then I take a screen shot
    */

    [Binding]
    public class LandingPageSteps
    {
        private readonly string _userdomain = System.Configuration.ConfigurationManager.AppSettings["OnlineUserDomain"];
        private readonly SecureString _password = System.Configuration.ConfigurationManager.AppSettings["OnlinePassword"].ToSecureString();
        private readonly Uri _baseUri = new Uri(System.Configuration.ConfigurationManager.AppSettings["OnlineBaseUrl"].ToString());

        public static SharePointBrowser _browser;

        [Given(@"I have logged in as '(.*)'")]
        public void GivenIHaveLoggedInAs(string p0)
        {
            _browser.LoginPage.Login(new Uri("https://portal.office.com"), string.Format("{0}@{1}", p0, _userdomain).ToSecureString(), _password, ADFSLoginAction);
        }

        [Given(@"I navigate to '(.*)'")]
        public void GivenINavigateTo(string p0)
        {
            _browser.Navigate(string.Format("{0}{1}", _baseUri, "/sites/pppdev"));
        }

        public void ADFSLoginAction(LoginRedirectEventArgs args)
        {
            var d = args.Driver;
            d.FindElement(By.Id("passwordInput")).SendKeys(args.Password.ToUnsecureString());
            d.ClickWhenAvailable(By.Id("submitButton"), new TimeSpan(0, 0, 2));
            d.WaitForPageToLoad();
        }

        [Given(@"I click button '(.*)'")]
        public void GivenIClickButton(string p0)
        {
        }

        [When(@"I click link '(.*)'")]
        public void WhenIClickLink(string p0)
        {
        }

        [Then(@"I click link '(.*)'")]
        public void ThenIClickLink(string p0)
        {
        }

        [Then(@"I take a screen shot")]
        public void ThenITakeAScreenShot()
        {
            string fileNameTimestap = DateTime.Now.ToString("yyyyMMddhhmmss");

            _browser.Driver.WaitForPageToLoad();
            _browser.TakeWindowScreenShot(
                "C:\\Temp\\screenshot_" + fileNameTimestap + ".png",
            ScreenshotImageFormat.Png);
        }
    }

    /*
    // PUT THE ROLLOW IN FEATURE FILE (Google.feature)

    Feature: Google
        In order to use PPM site
        As a normal user
        I want to be able to browse the site

    @mytag
    Scenario: Test Google Search
        Given I go to Google website
        And I search for Avanade wiki
        And I click on first link
        Then I am at the Avanade wiki page
        Then I capture screenshot

    @mytag
    Scenario: Login to PPM
        Given I login as User05.Test@whinniy.com
        Then I am at the landing page

    @mytag
    Scenario: Check Project Team Portfolio Management
        Given I am on the landing page
        And I click on Project Portfolio Management for Project Team
        Then I see a list of projects
    */

    [Binding]
    public class GoogleSearch
    {
        IWebDriver driver = null;

        [Given(@"I go to Google website")]
        public void GivenIGoToGoogleWebsite()
        {
            driver = new ChromeDriver();
            driver.Url = "http://www.google.com/";
            driver.Manage().Window.Maximize();

            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
        }

        [Given(@"I search for Avanade wiki")]
        public void GivenISearchForAvanade()
        {
            Thread.Sleep(2000);
            driver.FindElement(By.Id("lst-ib")).SendKeys("Avanade wiki");

            Thread.Sleep(2000);
            driver.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
        }

        [Given(@"I click on first link")]
        public void GivenIClickOnFirstLink()
        {
            Thread.Sleep(2000);
            driver.FindElement(By.LinkText("Avanade - Wikipedia")).Click();

            //IWebElement ele = driver.FindElement(By.LinkText("Avanade - Wikipedia"));
            //Actions actions = new Actions(driver);
            //actions.Click(ele).Perform();
        }

        [Then(@"I am at the Avanade wiki page")]
        public void ThenIGoToTheAvanadeWikiPage()
        {
            Thread.Sleep(2000);
            driver.FindElement(By.ClassName("firstHeading"));

            // able to land/find this page
        }

        [Then(@"I capture screenshot")]
        public void ThenICaptureScreenshot()
        {
            Thread.Sleep(2000);
            Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
            ss.SaveAsFile(@"C:\Temp\Screenshot.jpg", ScreenshotImageFormat.Jpeg);
        }
    }  
}