// Generated by CoffeeScript 1.7.1
(function() {
  var createGuest, destroyGuest, getNextInstanceId, guestInstances, ipc, nextInstanceId, supportedWebViewEvents, webContents, webViewManager,
    __slice = [].slice;

  ipc = require('ipc');

  webContents = require('web-contents');

  webViewManager = null;

  supportedWebViewEvents = ['did-finish-load', 'did-fail-load', 'did-frame-finish-load', 'did-start-loading', 'did-stop-loading', 'did-get-redirect-request', 'console-message', 'new-window', 'close', 'crashed', 'destroyed'];

  nextInstanceId = 0;

  guestInstances = {};

  getNextInstanceId = function(webContents) {
    return ++nextInstanceId;
  };

  createGuest = function(embedder, params) {
    var event, guest, id, preload, _fn, _i, _len, _ref;
    if (webViewManager == null) {
      webViewManager = process.atomBinding('web_view_manager');
    }
    id = getNextInstanceId(embedder);
    guest = webContents.create({
      isGuest: true,
      guestInstanceId: id,
      storagePartitionId: params.storagePartitionId
    });
    guestInstances[id] = {
      guest: guest,
      embedder: embedder
    };
    preload = (_ref = params.preload) != null ? _ref : '';
    webViewManager.addGuest(id, embedder, guest, params.nodeIntegration, params.plugins, preload);
    embedder.once('render-view-deleted', function() {
      if (guestInstances[id] != null) {
        return destroyGuest(id);
      }
    });
    guest.once('did-attach', function(event, params) {
      var max, min;
      this.viewInstanceId = params.instanceId;
      min = {
        width: params.minwidth,
        height: params.minheight
      };
      max = {
        width: params.maxwidth,
        height: params.maxheight
      };
      this.setAutoSize(params.autosize, min, max);
      if (params.src) {
        if (params.httpreferrer) {
          this.loadUrl(params.src, {
            httpreferrer: params.httpreferrer
          });
        } else {
          this.loadUrl(params.src);
        }
      }
      if (params.allowtransparency != null) {
        return this.setAllowTransparency(params.allowtransparency);
      }
    });
    _fn = function(event) {
      return guest.on(event, function() {
        var args, _;
        _ = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
        return embedder.send.apply(embedder, ["ATOM_SHELL_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-" + guest.viewInstanceId, event].concat(__slice.call(args)));
      });
    };
    for (_i = 0, _len = supportedWebViewEvents.length; _i < _len; _i++) {
      event = supportedWebViewEvents[_i];
      _fn(event);
    }
    guest.on('size-changed', function() {
      var args, _;
      _ = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
      return embedder.send.apply(embedder, ["ATOM_SHELL_GUEST_VIEW_INTERNAL_SIZE_CHANGED"].concat(__slice.call(args)));
    });
    return id;
  };

  destroyGuest = function(id) {
    webViewManager.removeGuest(id);
    guestInstances[id].guest.destroy();
    return delete guestInstances[id];
  };

  ipc.on('ATOM_SHELL_GUEST_VIEW_MANAGER_CREATE_GUEST', function(event, type, params, requestId) {
    return event.sender.send("ATOM_SHELL_RESPONSE_" + requestId, createGuest(event.sender, params));
  });

  ipc.on('ATOM_SHELL_GUEST_VIEW_MANAGER_DESTROY_GUEST', function(event, id) {
    return destroyGuest(id);
  });

  ipc.on('ATOM_SHELL_GUEST_VIEW_MANAGER_SET_AUTO_SIZE', function(event, id, params) {
    var _ref;
    return (_ref = guestInstances[id]) != null ? _ref.guest.setAutoSize(params.enableAutoSize, params.min, params.max) : void 0;
  });

  ipc.on('ATOM_SHELL_GUEST_VIEW_MANAGER_SET_ALLOW_TRANSPARENCY', function(event, id, allowtransparency) {
    var _ref;
    return (_ref = guestInstances[id]) != null ? _ref.guest.setAllowTransparency(allowtransparency) : void 0;
  });

  exports.getGuest = function(id) {
    var _ref;
    return (_ref = guestInstances[id]) != null ? _ref.guest : void 0;
  };

  exports.getEmbedder = function(id) {
    var _ref;
    return (_ref = guestInstances[id]) != null ? _ref.embedder : void 0;
  };

}).call(this);
