package com.agile611.testng.webdriver;

import java.net.URL;
import java.time.Duration;

import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class BaseRemoteWebDriverTest {
    public RemoteWebDriver driver;
    @BeforeClass(alwaysRun = true) //Inicialización del navegador
    public void setUp() throws Exception {
        String browser = System.getProperty("browser");
        // Extraemos la URL para mantener el código DRY (Don't Repeat Yourself)
        URL gridUrl = new URL("http://0.0.0.0:4444/wd/hub");

        if (browser != null && browser.equalsIgnoreCase("chrome")) {
            ChromeOptions options = new ChromeOptions();
            driver = new RemoteWebDriver(gridUrl, options);
            
        } else if (browser != null && browser.equalsIgnoreCase("safari")) {
            SafariOptions options = new SafariOptions();
            // Aquí podrías añadir configuraciones específicas si lo necesitas
            // options.setUseTechnologyPreview(true); 
            driver = new RemoteWebDriver(gridUrl, options);
            
        } else {
            // Firefox se mantiene como el navegador por defecto (fallback)
            FirefoxOptions options = new FirefoxOptions();
            driver = new RemoteWebDriver(gridUrl, options);
        }
        
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
    }

    @AfterClass(alwaysRun = true) //El cierre del navegador
    public void tearDown() throws Exception {
        driver.quit();
    }
}
