/*global window, rJS, loopEventListener */
/*jslint nomen: true, indent: 2, maxerr: 3*/
(function (window, rJS, loopEventListener) {
  "use strict";

  var CODE_CONTENT_KEY = "text_content";

  function saveTextContent(gadget, event) {
    var content,
      url = gadget.props.element.querySelector("form.crib-editor-get .url").value,
      mimetype = gadget.props.element.querySelector("form.crib-editor-save .mimetype").value;
    return new RSVP.Queue()
    .push(function () {
       return gadget.getDeclaredGadget('codeeditor');      
     })
    .push(function (code_editor_gadget) {
       return code_editor_gadget.getContent();
    })
    .push(function (data) {
       content = data[CODE_CONTENT_KEY] || "";
       return gadget.crib_sw_put(url, {content:content, type:mimetype});
    })
    .push(function() {
      gadget.props.element.querySelector(".crib-editor-save-status").textContent = "Saved " + url + " files at "+ Date ()
    })
  }

  function getUrlTextContent(gadget, event, url) {
    return new RSVP.Queue()
    .push(function () {
      return RSVP.all([gadget.crib_sw_get(url),
        gadget.getDeclaredGadget('codeeditor')])
    })
    .push(function(data_list) {
      //gadget.props.element.querySelector("form.crib-editor-save .content").value = data_list[0].responseText;
      gadget.props.element.querySelector("form.crib-editor-save .mimetype").value = data_list[0].responseType;
      return data_list[1].render({
        key: CODE_CONTENT_KEY,
        value: data_list[0].responseText,
        mode: data_list[0].responseType
        });
    })
  }

    rJS(window)
    .ready(function (g) {
      g.props = {};
      return g.getElement()
        .push(function (element) {
          g.props.element = element;
          g.props.start_deferred = RSVP.defer();
         });
    })
    .allowPublicAcquisition("editor_saveContent", function () {
      return saveTextContent(this, undefined);
    })
    .declareAcquiredMethod("crib_sw_get", "crib_sw_get")
    .declareAcquiredMethod("crib_sw_put", "crib_sw_put")
    .declareAcquiredMethod("getUrlFor", "getUrlFor")
    .declareMethod('render', function (options) {
      var gadget = this;
      if (options === undefined) 
        options = {}
      gadget.props.options = options;
      return new RSVP.Queue()
        .push(function () {
          if (options.url !== undefined) {
            gadget.props.element.querySelector("form.crib-editor-get .url").value = options.url;
            return getUrlTextContent(gadget, undefined, options.url)
          }
        })
        .push(function () {
          return gadget.props.start_deferred.resolve();
        })  
    })
    .declareService(function () {
      var gadget = this;
      return new RSVP.Queue()
        .push(function () {
          return gadget.props.start_deferred.promise;
        })
        .push(function () {
          var promise_list = [];
          // promise to get content from URL
          promise_list.push(loopEventListener(
            gadget.props.element.querySelector(".crib-editor-get"),
            'submit',
            false,
            function (event) {
              var url = gadget.props.element.querySelector("form.crib-editor-get .url").value;
              return getUrlTextContent(gadget, event, url);
            }
          ));
          // promise to save content
          promise_list.push(loopEventListener(
            gadget.props.element.querySelector(".crib-editor-save"),
            'submit',
            false,
            function (event) {saveTextContent(gadget, event)}
          ));
          return RSVP.all(promise_list);
        });
    })  
}(window, rJS, loopEventListener));