/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2017 wcm.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.
 * #L%
 */
package io.wcm.qa.galenium.webdriver;

import java.text.MessageFormat;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.wcm.qa.galenium.reporting.GaleniumReportUtil;
import io.wcm.qa.galenium.util.GaleniumConfiguration;
import io.wcm.qa.galenium.util.TestDevice;

class HeadlessChromeCapabilityProvider extends ChromeCapabilityProvider {

  private static final String[] ARGUMENTS_HEADLESS = new String[] {
      // main headless arg
      "headless",
      // workaround (https://developers.google.com/web/updates/2017/04/headless-chrome#cli)
      "disable-gpu" };
  private static final String[] ARGUMENTS_HEADLESS_WINDOWS_WORKAROUND = new String[] {
      // workaround for windows: there is still a window opened, so put it somewhere offscreen
      "window-position=10000,0"
  };
  private TestDevice device;


  public HeadlessChromeCapabilityProvider(TestDevice device) {
    setDevice(device);
  }

  @Override
  protected DesiredCapabilities getBrowserSpecificCapabilities() {
    GaleniumReportUtil.getLogger().debug("setting up headless chrome.");
    DesiredCapabilities capabilities = super.getBrowserSpecificCapabilities();
    addChromeOption(capabilities, OPTIONS_KEY_ARGS, ARGUMENTS_HEADLESS);
    if (GaleniumConfiguration.isChromeHeadlessWindowsWorkaround()) {
      addChromeOption(capabilities, OPTIONS_KEY_ARGS, ARGUMENTS_HEADLESS_WINDOWS_WORKAROUND);
    }
    addChromeOption(capabilities, OPTIONS_KEY_ARGS, getBrowserSizeArgument());
    return capabilities;
  }

  private String[] getBrowserSizeArgument() {
    Dimension screenSize = getDevice().getScreenSize();
    String windowSizeArgument = MessageFormat.format("--window-size={0}x{1}", screenSize.getWidth(), screenSize.getHeight());
    String[] argumentsBrowserWindowSize = new String[] { windowSizeArgument };
    return argumentsBrowserWindowSize;
  }

  public TestDevice getDevice() {
    return device;
  }

  public void setDevice(TestDevice device) {
    this.device = device;
  }

}

@@ -664,41 +664,8 @@
 r;%0A%0A
-import java.text.MessageFormat;%0A%0A
 impo
@@ -2033,314 +2033,161 @@
     
-addChromeOption(capabilities, OPTIONS_KEY_ARGS, getBrowserSizeArgument());%0A    return capabilities;%0A  %7D%0A%0A  private String%5B%5D getBrowserSizeArgument() %7B%0A    Dimension screenSize = getDevice().getScreenSize();%0A    String windowSizeArgument = MessageFormat.format(%22--window-size=%7B0%7Dx%7B1%7D%22, screenSize.getWidth()
+Dimension screenSize = getDevice().getScreenSize();%0A    String width = String.format(%22%25d%22, screenSize.getWidth());%0A    String height = String.format(%22%25d%22
 , sc
@@ -2270,40 +2270,126 @@
 %5D %7B 
+%22--
 window
-SizeArgument %7D;%0A    return
+-size=%22%0A        + width%0A        + %22x%22%0A        + height %7D;%0A    addChromeOption(capabilities, OPTIONS_KEY_ARGS,
  arg
@@ -2411,16 +2411,42 @@
 ndowSize
+);%0A    return capabilities
 ;%0A  %7D%0A%0A 

format size without thousand separators