package com.gmail.inverseconduit.chat;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.logging.Logger;

import com.gistlabs.mechanize.impl.MechanizeAgent;
import com.gmail.inverseconduit.SESite;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class JSONChatConnection {

    private static final String  EVENT_URL_FORMAT = "http://chat.%s.com/chats/%d/events";

    private static final Logger  logger           = Logger.getLogger(JSONChatConnection.class.getName());

    private static final Gson    gson             = new Gson();

    private final MechanizeAgent agent;

    private boolean              isEnabled        = false;

    private final MessageRelay   seBrowser;

    public JSONChatConnection(MechanizeAgent agent, MessageRelay relay) {
        this.agent = agent;
        seBrowser = relay;
    }

    public void queryEventsFor(final SESite site, final int chatId, final String fkey) {
        logger.finest(String.format("Querying events for %s 's room %d", site, chatId));
        if ( !isEnabled)
            throw new IllegalStateException();

        HashMap<String, String> params = new HashMap<>();
        params.put("mode", "Messages");
        params.put("fkey", fkey);

        String rString;
        try {
            rString = agent.post(String.format(EVENT_URL_FORMAT, site.getDomain(), chatId), params).asString();
        } catch(UnsupportedEncodingException e) {
            logger.severe("Unsupported Encoding: " + e.toString());
            throw new RuntimeException(e);
        }

        try {
            JSONChatEvents events = gson.fromJson(rString, JSONChatEvents.class);
            seBrowser.handleChatEvents(events);
            events.site = site;
        } catch(JsonSyntaxException ex) {
            logger.severe(ex.getClass() + ": " + ex.getMessage());
            logger.severe("The JSON object was: " + rString);
        }
    }

    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
    }
}

@@ -44,155 +44,210 @@
 ort 
-java.io.UnsupportedEncodingException;%0Aimport java.util.HashMap;%0Aimport java.util.logging.Logger;%0A%0Aimport com.gistlabs.mechanize.impl.MechanizeAgent
+com.gargoylesoftware.htmlunit.WebClient;%0Aimport com.gargoylesoftware.htmlunit.WebRequest;%0Aimport com.gargoylesoftware.htmlunit.WebResponse;%0Aimport com.gargoylesoftware.htmlunit.util.WebConnectionWrapper
 ;%0Aim
@@ -317,41 +317,26 @@
 on;%0A
+%0A
 import 
-com.google.gson.JsonSyntax
+java.io.IO
 Exce
@@ -379,520 +379,294 @@
 ion 
-%7B%0A%0A    private static final String  EVENT_URL_FORMAT = %22http://chat.%25s.com/chats/%25d/events%22;%0A%0A    private static final Logger  logger           = Logger.getLogger(JSONChatConnection.class.getName());%0A%0A    private static final Gson    gson             = new Gson();%0A%0A    private final MechanizeAgent agent;%0A%0A    private boolean              isEnabled        = false;%0A%0A    private final MessageRelay   seBrowser;%0A%0A    public JSONChatConnection(MechanizeAgent agent, MessageRelay relay) %7B%0A        this.agent = ag
+extends WebConnectionWrapper %7B%0A    private boolean isEnabled = false;%0A    private StackExchangeChat seBrowser;%0A    private Gson gson = new Gson();%0A%0A    public JSONChatConnection(WebClient webClient, StackExchangeChat seBrowser) throws IllegalArgumentException %7B%0A        super(webCli
 ent
+)
 ;%0A  
@@ -663,32 +663,37 @@
 lient);%0A        
+this.
 seBrowser = rela
@@ -692,13 +692,17 @@
 r = 
-relay
+seBrowser
 ;%0A  
@@ -714,241 +714,82 @@
     
-public void queryEventsFor(final SESite site, final int chatId, final String fkey) %7B%0A        logger.finest(String.format(%22Querying events for %25s 's room %25d%22, site, chatId));%0A        if ( !isEnabled)%0A            throw new IllegalState
+@Override%0A    public WebResponse getResponse(WebRequest request) throws IO
 Exce
@@ -793,20 +793,18 @@
 xception
-();%0A
+ %7B
 %0A       
@@ -808,141 +808,67 @@
     
-HashMap%3CString, String%3E params = new HashMap%3C%3E();%0A        params.put(%22mode%22, %22Messages%22);%0A        params.put(%22fkey%22, fkey
+WebResponse response = super.getResponse(request
 );%0A
-%0A
         
 Stri
@@ -867,31 +867,21 @@
     
-String rString;%0A       
+if(isEnabled)
  try
@@ -895,16 +895,23 @@
         
+String 
 rString 
@@ -916,283 +916,141 @@
 g = 
-agent.post(String.format(EVENT_URL_FORMAT, site.getDomain(), chatId), params).asString();%0A        %7D catch(UnsupportedEncodingException e) %7B%0A            logger.severe(%22Unsupported Encoding: %22 + e.toString());%0A            throw new RuntimeException(e);%0A        %7D%0A%0A        try %7B
+response.getContentAsString();%0A            String jsonString = rString.substring(rString.indexOf(%22:%22) + 1, rString.lastIndexOf(%22%7D%22));
 %0A   
@@ -1096,17 +1096,20 @@
 romJson(
-r
+json
 String, 
@@ -1147,102 +1147,121 @@
     
-seBrowser.handleChatEvents(events);%0A            events.site = site;%0A        %7D catch(JsonSyntax
+events.site = SESite.fromUrl(request.getUrl());%0A            seBrowser.handleChatEvents(events);%0A        %7D catch (
 Exce
@@ -1266,18 +1266,23 @@
 ception 
-ex
+ignored
 ) %7B%0A    
@@ -1289,138 +1289,34 @@
     
-    logger.severe(ex.getClass() + %22: %22 + ex.getMessage());%0A            logger.severe(%22The JSON object was: %22 + rString);%0A        %7D
+%7D%0A        return response;
 %0A   

Revert partial of a548723 which I missed.