package com.example.utils;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;


public class DriverManager {

    public WebDriver driver;

    public WebDriver WebDriverManager() throws IOException {
        FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "//src//test//resources//global.properties");
        Properties prop = new Properties();
        prop.load(fis);


        String urlPk = prop.getProperty("UrlPk");
        String browser_properties = prop.getProperty("browser");
        String browser_maven = System.getProperty("browser");
        String browser = browser_maven != null ? browser_maven : browser_properties;
        if (driver == null) {
            //Todo: For chrome browser any other browser can add their
            if (browser.equalsIgnoreCase("chrome")) {
                System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver");
                DesiredCapabilities caps = new DesiredCapabilities();
                LoggingPreferences logPrefs = new LoggingPreferences();
                logPrefs.enable(LogType.BROWSER, Level.ALL);
                caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
                driver = new ChromeDriver(caps);
                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                driver.manage().window().maximize();
            }
            //Todo: Fire fox work will be included
//            if(browser.equalsIgnoreCase("firefox"))
//          {
//              System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver");
//              driver= new FirefoxDriver();
//              driver.manage().window().maximize();
//           }
            driver.get(urlPk);
        }
        return driver;
    }

    public WebDriver WebDriverManagerUsa() throws IOException {
        FileInputStream fis = new FileInputStream(System.getProperty("user.dir") + "//src//test//resources//global.properties");
        Properties prop = new Properties();
        prop.load(fis);


        String urlPk = prop.getProperty("UrlUsa");
        String browser_properties = prop.getProperty("browser");
        String browser_maven = System.getProperty("browser");
        String browser = browser_maven != null ? browser_maven : browser_properties;
        if (driver == null) {
            if (browser.equalsIgnoreCase("chrome")) {                //For chrome browser any other browser can add their
                System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver");
                DesiredCapabilities caps = new DesiredCapabilities();
                LoggingPreferences logPrefs = new LoggingPreferences();
                logPrefs.enable(LogType.BROWSER, Level.ALL);
                caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
                driver = new ChromeDriver(caps);
                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                driver.manage().window().maximize();
            }
            //Todo: Fire fox work will be included
//            if(browser.equalsIgnoreCase("firefox"))
//          {
//              System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver");
//              driver= new FirefoxDriver();
//              driver.manage().window().maximize();
//           }
            driver.get(urlPk);
        }
        return driver;
    }
}
