import PageComponents.HomePagePF;
import PageComponents.LoginModalPF;
import PageComponents.RightSideBarPF;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class SafewayLoginTest {

    String OS;
    String browser;
    Properties safewayLoginCreds;

    @Test
    public void testSafewayLogin() throws InterruptedException {

        setup();

        WebDriver driver = new ChromeDriver();

        HomePagePF homePage = new HomePagePF(driver);
        RightSideBarPF rightSideBar = new RightSideBarPF(driver);
        LoginModalPF loginModal = new LoginModalPF(driver);

        homePage.visitSafewayHomepage();
        homePage.revealRightSidebar();
        rightSideBar.revealLoginModal();
        loginModal.fillUserInfo(safewayLoginCreds.getProperty("username"), safewayLoginCreds.getProperty("password"));
        loginModal.sendCredentials();

        Thread.sleep(10000);
        driver.quit();

    }

    public void setup() {
        /////////////////////
        // Get environment information
        /////////////////////
        this.OS = System.getProperty("os.name");
        this.browser = "Chrome";

        switch (this.OS) {
            case "Mac OS X":
                System.setProperty("webdriver.chrome.driver",
                        "/Users/kmuldoon/Dev/practice/testing/Selenium/Selenium-POM-Safeway/src/test/resources/chromedriver");
                break;

            case "Windows":
                ;
                break;

            case "Linux":
                ;
                break;

            default:
                System.out.println("Unsupported OS");
        }


        System.out.printf("Running login test using %s on %s\n", browser, OS);


        /////////////////////
        // Get Login Credentials
        /////////////////////
        try (InputStream input = new FileInputStream("src/test/resources/credentials.properties")) {

            this.safewayLoginCreds = new Properties();

            // load a properties file
            this.safewayLoginCreds.load(input);

            // get the property value and print it out
            System.out.println("username:\t" + this.safewayLoginCreds.getProperty("username"));
            System.out.println("password:\t" + this.safewayLoginCreds.getProperty("password"));

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}