/*
 * (C) Copyright 2017-2019 OpenVidu (https://openvidu.io/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.openvidu.test.e2e.browser;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;

public class ChromeUser extends BrowserUser {

	public ChromeUser(String userName, int timeOfWaitInSeconds, boolean runningAsRoot) {
		this(userName, timeOfWaitInSeconds, generateDefaultScreenChromeOptions(runningAsRoot));
	}

	public ChromeUser(String userName, int timeOfWaitInSeconds, String screenToCapture, boolean runningAsRoot) {
		this(userName, timeOfWaitInSeconds, generateCustomScreenChromeOptions(screenToCapture, runningAsRoot));
	}

	public ChromeUser(String userName, int timeOfWaitInSeconds, Path fakeVideoLocation) {
		this(userName, timeOfWaitInSeconds, generateFakeVideoChromeOptions(fakeVideoLocation));
	}

	private ChromeUser(String userName, int timeOfWaitInSeconds, ChromeOptions options) {
		super(userName, timeOfWaitInSeconds);
		options.setAcceptInsecureCerts(true);

		Map<String, Object> prefs = new HashMap<String, Object>();
		prefs.put("profile.default_content_setting_values.media_stream_mic", 1);
		prefs.put("profile.default_content_setting_values.media_stream_camera", 1);
		options.setExperimentalOption("prefs", prefs);

		String REMOTE_URL = System.getProperty("REMOTE_URL_CHROME");
		if (REMOTE_URL != null) {
			log.info("Using URL {} to connect to remote web driver", REMOTE_URL);
			try {
				this.driver = new RemoteWebDriver(new URL(REMOTE_URL), options);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		} else {
			log.info("Using local web driver");
			this.driver = new ChromeDriver(options);
		}

		this.driver.manage().timeouts().setScriptTimeout(this.timeOfWaitInSeconds, TimeUnit.SECONDS);
		this.configureDriver();
	}

	private static ChromeOptions generateDefaultScreenChromeOptions(boolean runningAsRoot) {
		ChromeOptions options = new ChromeOptions();
		// This flag avoids to grant the user media
		options.addArguments("--use-fake-ui-for-media-stream");
		// This flag fakes user media with synthetic video
		options.addArguments("--use-fake-device-for-media-stream");
		// This flag selects the entire screen as video source when screen sharing
		options.addArguments("--auto-select-desktop-capture-source=Entire screen");

		if (runningAsRoot) {
			options.addArguments("--no-sandbox");
		}

		return options;
	}

	private static ChromeOptions generateCustomScreenChromeOptions(String screenToCapture, boolean runningAsRoot) {
		ChromeOptions options = new ChromeOptions();
		// This flag selects the entire screen as video source when screen sharing
		options.addArguments("--auto-select-desktop-capture-source=" + screenToCapture);

		if (runningAsRoot) {
			options.addArguments("--no-sandbox");
		}

		return options;
	}

	private static ChromeOptions generateFakeVideoChromeOptions(Path videoFileLocation) {
		ChromeOptions options = new ChromeOptions();
		// This flag avoids to grant the user media
		options.addArguments("--use-fake-ui-for-media-stream");
		// This flag fakes user media with synthetic video
		options.addArguments("--use-fake-device-for-media-stream");
		// This flag sets the video input as
		options.addArguments("--use-file-for-fake-video-capture=" + videoFileLocation.toString());
		return options;
	}

}
@@ -745,16 +745,44 @@
 e.Path;%0A
+import java.nio.file.Paths;%0A
 import j
@@ -1440,32 +1440,78 @@
 e, runningAsRoot
+,%0A%09%09%09%09Paths.get(%22/opt/openvidu/fakeaudio.wav%22)
 ));%0A%09%7D%0A%0A%09public 
@@ -1849,16 +1849,65 @@
 s(true);
+%0A%09%09%0A%09%09options.addArguments(%22--disable-infobars%22);
 %0A%0A%09%09Map%3C
@@ -3404,32 +3404,59 @@
 an runningAsRoot
+,%0A%09%09%09Path audioFileLocation
 ) %7B%0A%09%09ChromeOpti
@@ -3649,16 +3649,171 @@
 apture);
+%0A%09%09options.addArguments(%22--use-fake-device-for-media-stream%22);%0A%09%09options.addArguments(%22--use-file-for-fake-audio-capture=%22 + audioFileLocation.toString());
 %0A%0A%09%09if (

configure fake audio in ChromeUser