package com.example.stepdefinitions;

import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;

import java.net.MalformedURLException;
import java.net.URL;

public class LoginSteps {
    private WebDriver driver;

    @Before
    public void setUp() throws MalformedURLException {
        // Get the execution type and browser from system properties
        String executionType = System.getProperty("execution.type", "local");
        String browser = System.getProperty("browser", "chrome");

        if (executionType.equalsIgnoreCase("grid")) {
            // For grid execution, create a RemoteWebDriver
            ChromeOptions chromeOptions = new ChromeOptions();
            // The Selenium Grid hub URL
            URL gridUrl = new URL("http://localhost:4444/wd/hub");
            driver = new RemoteWebDriver(gridUrl, chromeOptions);
        } else if (executionType.equalsIgnoreCase("saucelabs")) {
            // For Sauce Labs execution
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("browserName", "chrome");
            capabilities.setCapability("platform", "Windows 10");
            capabilities.setCapability("version", "latest");
            // Replace with your Sauce Labs username and access key
            String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");
            String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
            URL sauceUrl = new URL("https://" + SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com/wd/hub");
            driver = new RemoteWebDriver(sauceUrl, capabilities);
        } else if (executionType.equalsIgnoreCase("browserstack")) {
            // For BrowserStack execution
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("browser", "chrome");
            capabilities.setCapability("browser_version", "latest");
            capabilities.setCapability("os", "Windows");
            capabilities.setCapability("os_version", "10");
            capabilities.setCapability("project", "BDD Cucumber Project");
            capabilities.setCapability("build", "1.0");
            capabilities.setCapability("name", "Login Test");
            // Replace with your BrowserStack username and access key
            String BROWSERSTACK_USERNAME = System.getenv("BROWSERSTACK_USERNAME");
            String BROWSERSTACK_ACCESS_KEY = System.getenv("BROWSERSTACK_ACCESS_KEY");
            URL browserstackUrl = new URL("https://" + BROWSERSTACK_USERNAME + ":" + BROWSERSTACK_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub");
            driver = new RemoteWebDriver(browserstackUrl, capabilities);
        } else {
            // For local execution, use WebDriverManager
            if (browser.equalsIgnoreCase("chrome")) {
                WebDriverManager.chromedriver().setup();
                driver = new ChromeDriver();
            } else {
                // You can add support for other browsers here
                throw new IllegalArgumentException("Unsupported browser: " + browser);
            }
        }
    }

    @Given("I am on the login page")
    public void i_am_on_the_login_page() {
        // Get the absolute path of the index.html file
        String filePath = "C:\\Users\\mahes\\OneDrive\\Desktop\\mahesh\\Automation Frameworks\\Java\\frontend\\index.html";
        driver.get("file:///" + filePath);
    }

    @When("I enter {string} and {string}")
    public void i_enter_and(String username, String password) {
        // Find the username and password fields and enter the credentials
        driver.findElement(By.id("textbox1")).sendKeys(username);
        driver.findElement(By.id("textbox2")).sendKeys(password);
    }

    @When("I click the login button")
    public void i_click_the_login_button() {
        // Find and click the login button
        driver.findElement(By.id("button1")).click();
    }

    @Then("I should be on the home page")
    public void i_should_be_on_the_home_page() {
        // For this example, we'll just verify the title of the page.
        // In a real application, you would verify something more specific to the home page.
        String title = driver.getTitle();
        Assert.assertEquals(title, "Test Page");
    }

    @After
    public void tearDown() {
        // Quit the driver after each scenario
        if (driver != null) {
            driver.quit();
        }
    }
}
