package com.dpsolution.Tests;
import org.apache.http.HttpConnection;
import org.junit.*;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
import java.net.URL;
import org.apache.commons.lang3.SystemUtils;
import java.util.LinkedList;

public class TestBase {

    WebDriver driver = null;

    @Before
    public void setUp() throws Exception {
// Depend upon OS check for Chromedriver
        if (SystemUtils.IS_OS_LINUX){
            System.setProperty("webdriver.chrome.driver", "src/test/java/com/dpsolution/Tests/resource/chromedriver_linux64");

        } else{
            System.setProperty("webdriver.chrome.driver", "src/test/java/com/dpsolution/Tests/resource/chromedriver_mac");

        }


        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.manage().window().maximize();

    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        this.userCleanUp();
    }

    private void userCleanUp(){

        URL url = null;
        try {
            url = new URL("http://85.93.17.135:9000/users/all");
        } catch (MalformedURLException exception) {
            exception.printStackTrace();
        }
        HttpURLConnection httpURLConnection = null;
        try {
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            httpURLConnection.setRequestMethod("DELETE");
        } catch (IOException exception) {
            exception.printStackTrace();
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }

    }

}