// Path: rss-2-kindle-web-test/src/main/java/org/roag/exceptions/UnexpectedPageError.java
// public class UnexpectedPageError extends AssertionError {
//     private static final long serialVersionUID = 1L;
//     private final String detail;
// 
//     public UnexpectedPageError(final String expectedPageName, final Page expectedPage) {
//         super("Unexpected Page: current page should be " + expectedPageName);
//         this.detail = ErrorMessages.screenshot(WebDriverRunner.driver())
//                 + System.lineSeparator()
//                 + "Page url: " + expectedPage.getUrl() + System.lineSeparator()
//                 + "Page path: " + expectedPage.getPath();
//     }
// 
//     @Override
//     public String toString() {
//         return this.getLocalizedMessage() + this.detail;
//     }
// }


import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.WebDriverRunner;
import io.qameta.allure.Allure;
import io.qameta.allure.Step;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.roag.exceptions.UnexpectedPageError;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import static com.codeborne.selenide.Selenide.sleep;
    public static <T extends Page> T at(Class<T> pageClazz) {
        return verifyAtPage(extractPageName(pageClazz), pageClazz);
    }

    private static <T extends Page> String extractPageName(Class<T> pageClazz) {
        String name = pageClazz.getSimpleName();
        String[] nameParts = StringUtils.splitByCharacterTypeCamelCase(name);
        return StringUtils.join(nameParts, " ");
    }

    @Step("Verify That {pageName} Is Displayed")
    private static <T extends Page> T verifyAtPage(String pageName, Class<T> pageClass) {
        T page = instantiatePage(pageName, pageClass);
        if (!isPageDisplayed(pageName, page)) {
            attachAll();
            throw new UnexpectedPageError(pageName, page);
        }
        return page;
    }

    @Step("Create {pageName} Object")
    @SuppressWarnings("PMD.UnusedFormalParameter")
    private static <T extends Page> T instantiatePage(String pageName, Class<T> pageClass) {
        try {
            return pageClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

// Path: rss-2-kindle-web-test/src/main/java/org/roag/exceptions/UnexpectedPageError.java
// public class UnexpectedPageError extends AssertionError {
//     private static final long serialVersionUID = 1L;
//     private final String detail;
// 
//     public UnexpectedPageError(final String expectedPageName, final Page expectedPage) {
//         super("Unexpected Page: current page should be " + expectedPageName);
//         this.detail = ErrorMessages.screenshot(WebDriverRunner.driver())
//                 + System.lineSeparator()
//                 + "Page url: " + expectedPage.getUrl() + System.lineSeparator()
//                 + "Page path: " + expectedPage.getPath();
//     }
// 
//     @Override
//     public String toString() {
//         return this.getLocalizedMessage() + this.detail;
//     }
// }


// Path: rss-2-kindle-web-test/src/main/java/org/roag/pages/PageUtils.java
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.WebDriverRunner;
import io.qameta.allure.Allure;
import io.qameta.allure.Step;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.roag.exceptions.UnexpectedPageError;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import static com.codeborne.selenide.Selenide.sleep;

    public static <T extends Page> T at(Class<T> pageClazz) {
        return verifyAtPage(extractPageName(pageClazz), pageClazz);
    }

    private static <T extends Page> String extractPageName(Class<T> pageClazz) {
        String name = pageClazz.getSimpleName();
        String[] nameParts = StringUtils.splitByCharacterTypeCamelCase(name);
        return StringUtils.join(nameParts, " ");
    }

    @Step("Verify That {pageName} Is Displayed")
    private static <T extends Page> T verifyAtPage(String pageName, Class<T> pageClass) {
        T page = instantiatePage(pageName, pageClass);
        if (!isPageDisplayed(pageName, page)) {
            attachAll();
            throw new UnexpectedPageError(pageName, page);
        }
        return page;
    }

    @Step("Create {pageName} Object")
    @SuppressWarnings("PMD.UnusedFormalParameter")
    private static <T extends Page> T instantiatePage(String pageName, Class<T> pageClass) {
        try {
            return pageClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Step("Evaluate {pageName} isDisplayed Method")