package com.sdl.selenium.web.link;

import com.sdl.selenium.web.WebDriverConfig;
import com.sdl.selenium.web.WebLocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;

import java.util.ArrayList;
import java.util.List;

public class WebLink extends WebLocator {
    private static final Logger LOGGER = LoggerFactory.getLogger(WebLink.class);
    
    private String oldTab;

    public WebLink() {
        setClassName("WebLink");
        setTag("a");
    }

    public WebLink(WebLocator container) {
        this();
        setContainer(container);
    }

    public WebLink(WebLocator container, String text) {
        this(container);
        setText(text);
    }

    public boolean openInNewWindow() {
        try {
            boolean open = click();
            WebDriver driver = WebDriverConfig.getDriver();
            List<String> winList = new ArrayList<String>(driver.getWindowHandles());
            oldTab = winList.get(0);
            String newTab = winList.get(winList.size() - 1);
            return open && driver.switchTo().window(newTab) != null; // switch to new tab
        } catch (NoSuchWindowException e) {
            return false;
        }
    }

    public boolean returnDefaultWindow() {
        try {
            WebDriver driver = WebDriverConfig.getDriver();
            driver.close();
            return driver.switchTo().window(oldTab) != null;
        } catch (NoSuchWindowException e) {
            return false;
        }
    }
}

@@ -122,67 +122,8 @@
 r;%0D%0A
-import org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory;%0D%0A
 impo
@@ -207,16 +207,75 @@
 Driver;%0D
+%0Aimport org.slf4j.Logger;%0D%0Aimport org.slf4j.LoggerFactory;%0D
 %0A%0D%0Aimpor
@@ -561,24 +561,70 @@
 tTag(%22a%22);%0D%0A
+        setTemplate(%22title%22, %22@title='%25s'%22);%0D%0A
     %7D%0D%0A%0D%0A   

add title template for WebLink