package ru.kovbasa.pages;

import com.microsoft.playwright.Page;
import com.microsoft.playwright.PlaywrightException;
import ru.kovbasa.playwright.TestConfig;
import ru.kovbasa.utils.UiActions;

public abstract class BasePage {

    protected final Page page;
    protected final TestConfig config;

    protected BasePage(Page page, TestConfig config) {
        this.page = page;
        this.config = config;
    }

    protected void openPath(String path) {
        String url = config.getBaseUrl() + path;
        Page.NavigateOptions options = new Page.NavigateOptions().setWaitUntil(
                        com.microsoft.playwright.options.WaitUntilState.DOMCONTENTLOADED)
                .setTimeout(config.getTimeoutMs());
        try {
            page.navigate(url, options);
        } catch (PlaywrightException ex) {
            String message = ex.getMessage() == null ? "" : ex.getMessage();
            if (message.contains("ERR_CONNECTION_TIMED_OUT")
                    || message.contains("ERR_NAME_NOT_RESOLVED")
                    || message.contains("net::ERR")) {
                page.waitForTimeout(2000);
                page.navigate(url, options);
            } else {
                throw ex;
            }
        }
        UiActions.closeCommonPopups(page);
    }
}
