 // https://github.com/dmachi/webdriver-js
 // https://code.google.com/p/selenium/source/browse/javascript/webdriver/webdriver.js
 // http://www.seleniumhq.org/docs/03_webdriver.jsp#introducing-webdriver
 // $x("//ul[@class='themes']");
 
 var webdriver = require('selenium-webdriver'),
     assert = require("assert"),
     test = require('selenium-webdriver/testing');

 test.describe('Copy default theme', function () {
     var driver, timeout = 10000;

     // utility function that logs in
     function logIn() {
         driver.get('http://designer.beautify.io/index.html');
         driver.findElement(webdriver.By.name('username')).sendKeys('scripted-staging');
         driver.findElement(webdriver.By.name('password')).sendKeys('scr1pted');
         driver.findElement(webdriver.By.className('btn-primary')).click();
         return driver.wait(function () {
             return driver.getTitle().then(function (title) {
                 return 'beautify - My Themes' === title;
             });
         }, timeout, 'Waiting for login');
     }

     // utility function that navigates to default themes
     function navigateToDefaultThemes() {
         driver.findElement(webdriver.By.linkText("Default Themes")).click();
         return driver.wait(function () {
             return driver.getTitle().then(function (title) {
                 return 'beautify - Default Themes' === title;
             });
         }, timeout, 'Waiting for browser to navigate to default themes');
     }

     // utility function that selects a default theme with the given index
     function selectDefaultThemeByIndex(index) {
         return driver.findElement(webdriver.By.xpath("(//ul[@class='themes'])[2]/li[" + (index + 1) + "]")).click();
     }

     // utility function that clicks the copy submenu button for the theme with the given index
     function copyDefaultThemeWithIndexBySubmenuClick(index) {
         var xpath = "(//ul[@class='themes'])[2]/li[" + (index + 1) + "]//button[@title='Copy']";
         return driver.findElement(webdriver.By.xpath(xpath)).click();
     }

     function waitForModalDialog() {
         // waits for the modal dialog to appear or close. Bootstrap has an animation that means we have to wait
         // for a couple of seconds to give the modal dialog time to appear.
         return driver.sleep(2000)
     }

     function clickModalOKButton(index) {
         var xpath = "//div[@class='modal fade in']//button[@class='btn btn-primary']";
         return driver.findElement(webdriver.By.xpath(xpath)).click();
     }

     function clickThemePreviewCopyButton() {
         var xpath = "//div[@class='theme-preview']//button[@class='btn btn-primary copy']";
         return driver.findElement(webdriver.By.xpath(xpath)).click();
     }

     function enterTextIntoModalDialog(text) {
         var xpath = "//div[@class='modal fade in']//input[@class='control-input']";
         return driver.findElement(webdriver.By.xpath(xpath)).sendKeys(text);
     }

     test.beforeEach(function () {
         driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
     });

     test.afterEach(function () {
         driver.quit();
     });

     test.it('should be possible to copy default theme via clicking on the phone mockup copy button', function () {
         logIn();
         navigateToDefaultThemes();
         selectDefaultThemeByIndex(3);
         clickThemePreviewCopyButton();
         waitForModalDialog();
         enterTextIntoModalDialog("My theme copy");
         clickModalOKButton();
         waitForModalDialog();
         driver.sleep(2000);

         // verify that the newly created theme is being edited
         var themeNameElement = driver.findElement(webdriver.By.xpath("//h3[@class='title']/input"));
         themeNameElement.getAttribute('value').then(function (value) {
             assert.equal(value, 'My theme copy');
         });
     });

     test.it('should be possible to copy default theme via submenu click', function () {
         logIn();
         navigateToDefaultThemes();
         copyDefaultThemeWithIndexBySubmenuClick(3);
         waitForModalDialog();
         enterTextIntoModalDialog("My theme copy");
         clickModalOKButton();
         waitForModalDialog();
         driver.sleep(2000);

         // verify that the newly created theme is being edited
         var themeNameElement = driver.findElement(webdriver.By.xpath("//h3[@class='title']/input"));
         themeNameElement.getAttribute('value').then(function (value) {
             assert.equal(value, 'My theme copy');
         });
     });
 });