package com.frameworkium.core.ui.listeners;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.*;
import org.openqa.selenium.support.events.WebDriverEventListener;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoggingListener implements WebDriverEventListener {

    private static final Logger logger = LogManager.getLogger();

    @Override
    public void afterChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysSent) {
        logger.debug("changed value of element with " + getLocatorFromElement(element));
    }

    @Override
    public void afterClickOn(WebElement element, WebDriver driver) {
        logger.debug("clicked element with " + getLocatorFromElement(element));
    }

    @Override
    public void afterFindBy(By by, WebElement element, WebDriver driver) {
        logger.debug("found element " + by);
    }

    @Override
    public void afterNavigateBack(WebDriver driver) {
        logger.debug("after back");
    }

    @Override
    public void afterNavigateForward(WebDriver driver) {
        logger.debug("after forward");
    }

    @Override
    public void beforeNavigateRefresh(WebDriver webDriver) {
        logger.debug("before Navigate Refresh");
    }

    @Override
    public void afterNavigateRefresh(WebDriver webDriver) {
        logger.debug("after Navigate Refresh");
    }

    @Override
    public void afterNavigateTo(String url, WebDriver driver) {
        logger.debug("navigated to " + url);
    }

    @Override
    public void afterScript(String script, WebDriver driver) {
        // Only log part of a long script
        // We already log the entire script in beforeScript
        logger.debug("ran script " + StringUtils.abbreviate(script, 128));
    }

    @Override
    public void beforeSwitchToWindow(String windowName, WebDriver driver) {
        logger.debug("before switch to window " + windowName);
    }

    @Override
    public void afterSwitchToWindow(String windowName, WebDriver driver) {
        logger.debug("after switch to window " + windowName);
    }

    @Override
    public void beforeChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend) {
        logger.debug("change value of element with " + getLocatorFromElement(element));
    }

    @Override
    public void beforeClickOn(WebElement element, WebDriver driver) {
        logger.debug("click element with " + getLocatorFromElement(element));
    }

    @Override
    public void beforeFindBy(By by, WebElement element, WebDriver driver) {
        logger.debug("find element " + by);
    }

    @Override
    public void beforeNavigateBack(WebDriver driver) {
        logger.debug("before back");
    }

    @Override
    public void beforeNavigateForward(WebDriver driver) {
        logger.debug("before forward");
    }

    @Override
    public void beforeNavigateTo(String url, WebDriver driver) {
        logger.debug("navigate to " + url);
    }

    @Override
    public void beforeScript(String script, WebDriver driver) {
        logger.debug("running script " + script);
    }

    @Override
    public void onException(Throwable thrw, WebDriver driver) {
        // Lots of caught exceptions being logged here
        logger.trace("Event listener onException().", thrw);
    }

    @Override
    public <X> void beforeGetScreenshotAs(OutputType<X> outputType) {
        logger.trace("Before GetScreenshotAs");
    }

    @Override
    public <X> void afterGetScreenshotAs(OutputType<X> outputType, X x) {
        logger.trace("After GetScreenshotAs");
    }

    @Override
    public void beforeGetText(WebElement webElement, WebDriver webDriver) {
        logger.trace("Before GetText");
    }

    @Override
    public void afterGetText(WebElement webElement, WebDriver webDriver, String s) {
        logger.trace("After GetText");
    }

    private String getLocatorFromElement(WebElement element) {
        String str = element.toString();
        Pattern p = Pattern.compile("->\\s(.*)(?=\\])");
        Matcher m = p.matcher(str);
        return m.find() && m.groupCount() > 0
                ? m.group(1)
                : str;
    }

    @Override
    public void beforeAlertAccept(WebDriver webDriver) {
        logger.debug("before alert accept");
    }

    @Override
    public void afterAlertAccept(WebDriver webDriver) {
        logger.debug("after alert accept");
    }

    @Override
    public void beforeAlertDismiss(WebDriver webDriver) {
        logger.debug("before alert dismiss");
    }

    @Override
    public void afterAlertDismiss(WebDriver webDriver) {
        logger.debug("after alert accept");
    }
}

@@ -461,16 +461,325 @@
 ger();%0A%0A
+    private static String getLocatorFromElement(WebElement element) %7B%0A        String str = element.toString();%0A        Pattern p = Pattern.compile(%22-%3E%5C%5Cs(.*)(?=%5C%5C%5D)%22);%0A        Matcher m = p.matcher(str);%0A        return m.find() && m.groupCount() %3E 0%0A                ? m.group(1)%0A                : str;%0A    %7D%0A%0A
     @Ove
@@ -776,32 +776,32 @@
 %0A%0A    @Override%0A
-
     public void 
@@ -901,24 +901,30 @@
 ogger.debug(
+() -%3E 
 %22changed val
@@ -1086,24 +1086,30 @@
 ogger.debug(
+() -%3E 
 %22clicked ele
@@ -1279,35 +1279,36 @@
 (%22found element 
-%22 +
+%7B%7D%22,
  by);%0A    %7D%0A%0A   
@@ -1901,27 +1901,28 @@
 avigated to 
-%22 +
+%7B%7D%22,
  url);%0A    %7D
@@ -2123,16 +2123,22 @@
 r.debug(
+() -%3E 
 %22ran scr
@@ -2646,16 +2646,22 @@
 r.debug(
+() -%3E 
 %22change 
@@ -2831,16 +2831,22 @@
 r.debug(
+() -%3E 
 %22click e
@@ -3013,16 +3013,23 @@
 .debug(%22
+before 
 find ele
@@ -3033,19 +3033,23 @@
 element 
-%22 +
+by %7B%7D%22,
  by);%0A  
@@ -3864,20 +3864,21 @@
 %22Before 
-GetS
+get s
 creensho
@@ -3870,33 +3870,34 @@
 e get screenshot
-A
+ a
 s%22);%0A    %7D%0A%0A    
@@ -4008,20 +4008,21 @@
 (%22After 
-GetS
+get s
 creensho
@@ -4022,17 +4022,18 @@
 reenshot
-A
+ a
 s%22);%0A   
@@ -4155,20 +4155,21 @@
 %22Before 
-GetT
+get t
 ext%22);%0A 
@@ -4305,319 +4305,18 @@
 ter 
-GetText%22);%0A    %7D%0A%0A    private String getLocatorFromElement(WebElement element) %7B%0A        String str = element.toString();%0A        Pattern p = Pattern.compile(%22-%3E%5C%5Cs(.*)(?=%5C%5C%5D)%22);%0A        Matcher m = p.matcher(str);%0A        return m.find() && m.groupCount() %3E 0%0A                ? m.group(1)%0A                : str
+get text%22)
 ;%0A  

Use lambdas for logging to prevent unnecessary evaluation of code.