package insynctive.utils.saucelabs;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import com.saucelabs.saucerest.SauceREST;

import insynctive.utils.ExternalTestRunner;
import insynctive.utils.data.TestEnvironment;

public class SauceLabsUtil implements ExternalTestRunner {

	private String username = "Insynctive2";
	private String password = "d2f371f8-fc76-4b7c-9b60-e069c996c879";
	private SauceREST rest;
	
	private final int COMMAND_TIMEOUT = 420;
	private final int IDLE_COMMAND_TIMEOUT = 300;
	
	public SauceLabsUtil() {
		rest = new SauceREST(username, password);
	}
	
	@Override
	public String getRemoteTestingUsername() {
		return username;
	}

	@Override
	public void setRemoteTestingUsername(String username) {
		this.username = username;
	}

	@Override
	public String getRemoteTestingPassword() {
		return password;
	}

	@Override
	public void setRemoteTestingPassword(String password) {
		this.password = password;
	}

	@Override
	public RemoteWebDriver getRemoteWebDriver(String sessionName, TestEnvironment testEnvironment) throws MalformedURLException {
		DesiredCapabilities capabilities = new DesiredCapabilities();
		
		capabilities.setCapability("command-timeout", COMMAND_TIMEOUT);
		capabilities.setCapability("commandTimeout", IDLE_COMMAND_TIMEOUT);
		capabilities.setCapability("name", sessionName+" ["+testEnvironment+"]");
		//Set BROWSER > VERSION > PLATAFORM
		capabilities.setCapability(CapabilityType.BROWSER_NAME, testEnvironment.browserSauceLabs);
		if (testEnvironment.versionSauceLabs != null) { capabilities.setCapability(CapabilityType.VERSION, testEnvironment.versionSauceLabs);	}
		capabilities.setCapability(CapabilityType.PLATFORM, testEnvironment.osSauceLabs);
		
		if(!testEnvironment.isDesktop) {capabilities.setCapability("deviceOrientation", "portrait");}
		
		
		return new RemoteWebDriver(new URL("http://" + username + ":" + password + "@ondemand.saucelabs.com:80/wd/hub"), capabilities);
	}

	@Override
	public void changeStatusOfJob(String jobID, Boolean generalStatus) throws MalformedURLException, IOException, JSONException {
		if(generalStatus) { rest.jobPassed(jobID); } else { rest.jobFailed(jobID);}
	}

	@Override
	public String getPublicVideoLinkOfJob(String jobID) throws IOException, JSONException {
		return "https://saucelabs.com/beta/tests/" + jobID + "/watch";
	}
}

@@ -539,17 +539,17 @@
 synctive
-2
+3
 %22;%0A%09priv
@@ -575,46 +575,48 @@
  = %22
-d2f371f8-fc76-4b7c-9b60-e069c996c879%22;
+0c0ad8aa-4e69-49c5-9f5a-dbc2bd06bc8a%22;%0A%09
 %0A%09pr

change saucelabs account