// See Purple/license.txt for Google BSD license
// Copyright 2011 Google, Inc. johnjbarton@johnjbarton.com

define(['../browser/remoteByWebInspector'], function (remoteByWebInspector) {
  var thePurple = window.purple;
  var Assembly = thePurple.Assembly;

  var Resources = new thePurple.PurplePart('resources');
  
  Resources.resources = [];
  Resources.resourcesByURL = {}; // a Resource or an array of
  
  Assembly.addPartContainer(Resources);

  Resources.append = function(url, resource) {
    this.resourcesByURL[url] = resource;
    this.resources.push(resource);
    this.toEachPart('change', [{mutation: 'add', propertyName: url, value: resource}]);
    return resource;
  };

  Resources.get = function(url) {
    if ( this.resourcesByURL.hasOwnProperty(url) ) {
      return this.resourcesByURL[url];
    }
  };
  
  thePurple.registerPart(Resources);
  
  var jsEventHandler = new thePurple.PurplePart('jsEventHandler');
  
    
  //---------------------------------------------------------------------------------------------
  
  function JavaScriptResource(url, isContentScript) {
    this.url = url;
    this.isContentScript = isContentScript;
    this.scripts = {};
  }
  
  JavaScriptResource.prototype = {};
  
  JavaScriptResource.prototype.appendScript = function(scriptId, startLine, startColumn, endLine, endColumn) {
    this.scripts[scriptId] = [startLine, startColumn, endLine, endColumn];
  };
  
  //---------------------------------------------------------------------------------------------
  //
  jsEventHandler.startDebugger = function() {
    this.remote.Debugger.enable();
  };
  
  jsEventHandler.stopDebugger = function() {
    this.remote.Debugger.disable();
  };
  
  jsEventHandler.getOrCreateJavaScriptResource = function(url, isContentScript) {
    var resource = Resources.get(url);
    if (!resource) {
      resource = Resources.append(url, new JavaScriptResource(url, isContentScript));
    }
    return resource;
  };

  // Implement Remote.events
  jsEventHandler.responseHandlers = {
    Debugger: {
        breakpointResolved: function(breakpointId, location) {
          console.log("JavaScriptEventHandler", arguments);
        },
        paused: function(details) {
          console.log("JavaScriptEventHandler", arguments);
        },
        resumed: function() {
          console.log("JavaScriptEventHandler", arguments);
        },
        scriptFailedToParse: function(data, errorLine, errorMessage, firstLine, url) {
          console.log("JavaScriptEventHandler", arguments);
        },
        scriptParsed: function(endColumn, endLine, isContentScript, scriptId, startColumn, startLine, url) {
           var res = jsEventHandler.getOrCreateJavaScriptResource(url, isContentScript);
           res.appendScript(scriptId, startLine, startColumn, endLine, endColumn);
        }
      },
      Timeline: {
        eventRecorded: function(record) {
          console.log("JavaScriptEventHandler", arguments);
        },
        started: function() {
          console.log("JavaScriptEventHandler", arguments);
        },
        stopped: function() {
          console.log("JavaScriptEventHandler", arguments);
        },
      },
  };
  
   //---------------------------------------------------------------------------------------------
  // Implement PurplePart
  
  jsEventHandler.connect = function(channel) {
      this.remote = remoteByWebInspector.create('resourceCreationRemote', this.responseHandlers);
      this.remote.connect(channel);
	  this.startDebugger();
  };
  
  jsEventHandler.disconnect = function(channel) {
      this.stopDebugger();
      this.remote.disconnect(channel);
  };

  thePurple.registerPart(jsEventHandler);

  return jsEventHandler;
});
@@ -422,44 +422,221 @@
 %0D%0A  
-Assembly.addPartContainer(Resources)
+Resources.connect = function(eventSink) %7B%0D%0A    this.eventSink = eventSink;%0D%0A  %7D;%0D%0A%0D%0A  Resources.disconnect = function(eventSink) %7B%0D%0A    if (this.eventSink === eventSink) %7B%0D%0A      delete this.eventSink;%0D%0A    %7D%0D%0A  %7D
 ;%0D%0A%0D
@@ -726,16 +726,16 @@
 ource;%0D%0A
-
     this
@@ -758,24 +758,77 @@
 resource);%0D%0A
+    this.eventSink.apply(null, %5B%7Bdata: resource%7D%5D);%0D%0A
     this.toE
@@ -3838,16 +3838,104 @@
 nnel);%0D%0A
+      this.logger = channel.recv.bind(channel);%0D%0A      Resources.connect(this.logger);%0D%0A
 %09  this.
@@ -4077,24 +4077,66 @@
 (channel);%0D%0A
+      Resources.disconnect(this.logger);%0D%0A
   %7D;%0D%0A%0D%0A  th

Write a line into the log for each new Resource