/**
 * 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.grid.proxies;

import com.groupon.seleniumgridextras.ExecuteCommand;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.internal.TestSession;
import org.openqa.grid.internal.listeners.TestSessionListener;
import org.openqa.grid.selenium.proxy.DefaultRemoteProxy;
import org.openqa.selenium.remote.CapabilityType;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class SetupTeardownProxy extends DefaultRemoteProxy implements TestSessionListener {

  private Pattern urlPattern = Pattern.compile("http://([^:/]+)");

  public SetupTeardownProxy(RegistrationRequest request, Registry registry) {
    super(request, registry);
    System.out.println("Attaching a node with " + getClass().getSimpleName());
  }

  @Override
  public void beforeSession(TestSession session) {
    super.beforeSession(session);
    Map<String,Object> cap =session.getRequestedCapabilities();
    if(cap.get(CapabilityType.BROWSER_NAME).equals("internet explorer"))
        callAction(session,"kill_ie"); //kill IE if starting IE
    callAction(session, "setup");
  }

  @Override
  public void afterSession(TestSession session) {
    super.afterSession(session);
    Map<String,Object> cap =session.getRequestedCapabilities();
    if(cap.get(CapabilityType.BROWSER_NAME).equals("internet explorer"))
        callAction(session,"kill_ie"); //kill IE if starting IE
    callAction(session, "teardown");
  }

  synchronized private void callAction(TestSession session, String action) {
    try {
      String nodeAddress = session.getSlot().getProxy().getRemoteHost().toString();
      Matcher matcher = urlPattern.matcher(nodeAddress);
      String cleanAddress = "";
      while (matcher.find()) {
        cleanAddress = matcher.group();
      }
      URL url = new URL(cleanAddress + ":3000/" + action);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      System.out.println(ExecuteCommand.inputStreamToString(conn.getInputStream()));
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (ProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }


}

@@ -2787,339 +2787,336 @@
     
-Map%3CString,Object%3E cap =session.getRequestedCapabilities();%0A    if(cap.get(CapabilityType.BROWSER_NAME).equals(%22internet explorer%22))%0A        callAction(session,%22kill_ie%22); //kill IE if starting IE%0A    callAction(session, %22setup%22);%0A  %7D%0A%0A  @Override%0A  public void afterSession(TestSession session) %7B%0A    super.afterSession(session);%0A
+killBrowserForCurrentSession(session);%0A    callAction(session, %22setup%22);%0A  %7D%0A%0A  @Override%0A  public void afterSession(TestSession session) %7B%0A    super.afterSession(session);%0A    killBrowserForCurrentSession(session);%0A    callAction(session, %22teardown%22);%0A  %7D%0A%0A  private void killBrowserForCurrentSession(TestSession session) %7B%0A   
     
@@ -3126,16 +3126,17 @@
 %3CString,
+ 
 Object%3E 
@@ -3140,16 +3140,17 @@
 t%3E cap =
+ 
 session.
@@ -3185,11 +3185,37 @@
     
+   
-if(
+String browser = (String) 
 cap.
@@ -3246,16 +3246,37 @@
 ER_NAME)
+;%0A%0A       if (browser
 .equals(
@@ -3296,16 +3296,18 @@
 lorer%22))
+ %7B
 %0A       
@@ -3303,24 +3303,25 @@
 ) %7B%0A        
+ 
 callAction(s
@@ -3327,16 +3327,17 @@
 session,
+ 
 %22kill_ie
@@ -3343,71 +3343,204 @@
 e%22);
- //kill IE if starting IE%0A    callAction(session, %22teardown%22);%0A
+%0A       %7D else if (browser.equals(%22chrome%22)) %7B%0A         callAction(session, %22kill_chrome%22);%0A       %7D else if (browser.equals(%22firefox%22))%7B%0A         callAction(session, %22kill_firefox%22);%0A       %7D%0A   
   %7D%0A

Send kill command for each supported browser