package com.bayer.edam;

import static org.junit.Assert.fail;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class ToolboxVideoExporter {
	private WebDriver driver;
	private String baseUrl;
	private boolean acceptNextAlert = true;
	private StringBuffer verificationErrors = new StringBuffer();
	private Robot robot;
	

	@Before
	public void setUp() throws Exception {
		System.setProperty("webdriver.gecko.driver", "F:\\Toolbox\\geckodriver.exe");
		FirefoxOptions options = new FirefoxOptions();
		options.setBinary("C:\\Users\\gexce\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
		driver = new FirefoxDriver(options);
		baseUrl = "https://bayertoolbox.com";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		
		
		robot = new Robot();
	}

	@Test
	public void testCase() throws Exception {
	    driver.get(baseUrl + "/");
	    driver.findElement(By.id("MainContent_tbxUserName")).clear();
	    driver.findElement(By.id("MainContent_tbxUserName")).sendKeys("alexander.swagemakers@bayer.com");
	    driver.findElement(By.id("MainContent_tbxPassword")).clear();
	    driver.findElement(By.id("MainContent_tbxPassword")).sendKeys("bayer123");
	    driver.findElement(By.id("MainContent_RememberMe")).click();
	    driver.findElement(By.id("MainContent_btnLogin")).click();
		
		driver.get(baseUrl + "/Assets/AssetDetailNational.aspx?AssetID=22936");
		driver.findElement(By.cssSelector("#MainContent_aDownload > strong")).click();
		driver.findElement(By.id("MainContent_btnDownloadOk")).click();
		
//		robot.delay(5000);
		
		robot.keyPress(KeyEvent.VK_ALT);
		robot.keyPress(KeyEvent.VK_S);
		robot.keyRelease(KeyEvent.VK_S);
		robot.keyRelease(KeyEvent.VK_ALT);

		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
		
		robot.keyPress(KeyEvent.VK_1); robot.keyRelease(KeyEvent.VK_1);
		robot.keyPress(KeyEvent.VK_2); robot.keyRelease(KeyEvent.VK_2);
		robot.keyPress(KeyEvent.VK_3); robot.keyRelease(KeyEvent.VK_3);
		robot.keyPress(KeyEvent.VK_A); robot.keyRelease(KeyEvent.VK_A);
		robot.keyPress(KeyEvent.VK_PERIOD); robot.keyRelease(KeyEvent.VK_PERIOD);
		robot.keyPress(KeyEvent.VK_Z); robot.keyRelease(KeyEvent.VK_Z);
		robot.keyPress(KeyEvent.VK_I); robot.keyRelease(KeyEvent.VK_I);
		robot.keyPress(KeyEvent.VK_P); robot.keyRelease(KeyEvent.VK_P);
		
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
		
	}

	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	private boolean isAlertPresent() {
		try {
			driver.switchTo().alert();
			return true;
		} catch (NoAlertPresentException e) {
			return false;
		}
	}

	private String closeAlertAndGetItsText() {
		try {
			Alert alert = driver.switchTo().alert();
			String alertText = alert.getText();
			if (acceptNextAlert) {
				alert.accept();
			} else {
				alert.dismiss();
			}
			return alertText;
		} finally {
			acceptNextAlert = true;
		}
	}
}
