/**
 * Copyright (c) 2013, Groupon, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * Neither the name of GROUPON nor the names of its contributors may be
 * used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 * Created with IntelliJ IDEA.
 * User: Dima Kovalenko (@dimacus) && Darko Marinov
 * Date: 5/10/13
 * Time: 4:06 PM
 */


package com.groupon.seleniumgridextras.tasks;


import java.util.Map;

import com.google.gson.JsonObject;
import com.groupon.seleniumgridextras.ExecuteCommand;

public class StopGrid extends ExecuteOSTask {

  public StopGrid() {
    setEndpoint("/stop_grid");
    setDescription("Stops grid or node process");
    JsonObject params = new JsonObject();
    params.addProperty("port", "(Required) Port on which the node/hub is running.");
    setAcceptedParams(params);
    setRequestType("GET");
    setResponseType("json");
    setClassname(this.getClass().getCanonicalName().toString());
    setCssClass("btn-info");
    setButtonText("Stop Grid");
    setEnabledInGui(false);
  }

  @Override
  public JsonObject getAcceptedParams() {
    JsonObject params = new JsonObject();
    params.addProperty("port", "(Required) Port on which the node/hub is running");
    return params;
  }

  @Override
  public JsonObject execute() {
    getJsonResponse().addKeyValues("error", "Port parameter is required");
    return getJsonResponse().getJson();
  }

//  @Override
//  public JsonObject execute(String port) {
//	  String stopSeleniumUrl = "http://";
//	  // add host
//	  stopSeleniumUrl += RuntimeConfig.getConfig().getHub().getHost();
//	  // add port
//	  stopSeleniumUrl += ":" + RuntimeConfig.getConfig().getHub().getPort();
//	  // add command
//	  stopSeleniumUrl += "/selenium-server/driver/?cmd=shutDownSeleniumServer";
//	    try {
//	    	URL url = new URL(stopSeleniumUrl);
//	    	HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//	    	;
//	    	JsonObject result = new JsonObject();
//	    	result.add("httpResponse", new JsonPrimitive(connection.getResponseCode()));
//	    	return result;
//    } catch (Exception error) {
//      getJsonResponse().addKeyValues("error", error.toString());
//      return getJsonResponse().getJson();
//    }
//  }
  
  public JsonObject execute(String port) {
	  return ExecuteCommand.execRuntime(getWindowsCommand(port), true);
  }
  
	/**
	 * Get all the tasks currently running with verbose description. Search in
	 * the task description for execution of the start_&lt;port>.bat and kill
	 * the corresponding process.
	 */
  public String getWindowsCommand(String port) {
	  return "FOR /F \"usebackq tokens=2\" %i IN (`tasklist /V ^| findstr \"" + port + ".bat\"`) DO taskkill /PID %i";
  }

  @Override
  public JsonObject execute(Map<String, String> parameter) {
    if (parameter.isEmpty() || !parameter.containsKey("port")) {
      return execute();
    } else {
      return execute(parameter.get("port").toString());
    }
  }

}

@@ -2719,849 +2719,8 @@
 %0A  %7D
-%0A%0A//  @Override%0A//  public JsonObject execute(String port) %7B%0A//%09  String stopSeleniumUrl = %22http://%22;%0A//%09  // add host%0A//%09  stopSeleniumUrl += RuntimeConfig.getConfig().getHub().getHost();%0A//%09  // add port%0A//%09  stopSeleniumUrl += %22:%22 + RuntimeConfig.getConfig().getHub().getPort();%0A//%09  // add command%0A//%09  stopSeleniumUrl += %22/selenium-server/driver/?cmd=shutDownSeleniumServer%22;%0A//%09    try %7B%0A//%09    %09URL url = new URL(stopSeleniumUrl);%0A//%09    %09HttpURLConnection connection = (HttpURLConnection) url.openConnection();%0A//%09    %09;%0A//%09    %09JsonObject result = new JsonObject();%0A//%09    %09result.add(%22httpResponse%22, new JsonPrimitive(connection.getResponseCode()));%0A//%09    %09return result;%0A//    %7D catch (Exception error) %7B%0A//      getJsonResponse().addKeyValues(%22error%22, error.toString());%0A//      return getJsonResponse().getJson();%0A//    %7D%0A//  %7D
 %0A  %0A

clean up unused code.