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

  function getExtension(url) {
    var extension = url.split('.').pop();
    if (extension.endsWith('/')) {
      return ".html";
    }
    return "." + extension;
  }  

  function createJio(gadget, jio_config) {
    var jio_gadget;
    return gadget.getDeclaredGadget("jio_gadget")
      .push(function (jio_g) {
        jio_gadget = jio_g;
        if (jio_config === undefined)
          jio_config = {type: "indexeddb", database: "cribjs"};
        return jio_gadget.createJio(jio_config);
    });
  }

  function createDAVJio(gadget, event) {
    return createJio(gadget, {
      type:"dav",
      url: gadget.props.element.querySelector('.form-use-jio-dav .url').value,
      basic_login: btoa(gadget.props.element.querySelector(".form-use-jio-dav .credential").value)
    });
  }  

  function saveContentToJIO(gadget, event) {
    var path_to_save, path_to_save_length, application_id, crib_sw_gadget,
      jio_gadget, url_list = [], saved_number = 0;
    path_to_save = gadget.props.element.querySelector('form.crib-save-to-jio .save-path').value
    application_id = gadget.props.element.querySelector('form.crib-save-to-jio .save-id').value
    path_to_save_length = path_to_save.length;
    return new RSVP.Queue()
    .push(function () {
      return RSVP.all([
        gadget.getDeclaredGadget('jio_gadget')]);
    })
    .push(function (gadget_list) {
      jio_gadget = gadget_list[0];
      return gadget.crib_sw_allDocs({cached_only: true});
    })
    .push(function(data) {
      url_list = data.urls;
      // This is buggy, it fails if the document already exists with dav storage
      return RSVP.all(
        [jio_gadget.put("/" + application_id + ".attachment/", {
                 // url: path_to_save
                  }),
         jio_gadget.putAttachment("/", application_id, new Blob([JSON.stringify({url: path_to_save})],{type: "application/json"}))  
      ]);      
    })
    .push(function() {
      var promise_list = [],
        i, i_len, url;
      for (i = 0, i_len = url_list.length; i < i_len; i += 1) {
        url = new String(url_list[i]);
        if (url.indexOf(path_to_save) === 0) {
          saved_number += 1;
          promise_list.push(gadget.crib_sw_get(url));
        };
      };
      return RSVP.all(promise_list);
    })
    .push(function(response_list) {
      var promise_list = [],
        i, i_len, url, response, extension;
      for (i = 0, i_len = response_list.length; i < i_len; i += 1) {
        response = response_list[i];
        url = response.responseURL.substr(path_to_save_length)
        extension = getExtension(url)
        promise_list.push(
          jio_gadget.putAttachment("/" + application_id + ".attachment/", btoa(url) + extension,
            // XXX Not sure it would work with images
            new Blob([response.responseText], {type : response.responseType})
          )
        );
      };
      return RSVP.all(promise_list)
    })
    .push(function() {
      gadget.props.element.querySelector(".crib-save-to-jio-status").textContent = "Saved " + saved_number + " files at "+ Date ()
    })
  }


  function loadContentFromJIO(gadget, event) {
    var path_to_load, path_to_load_length, application_id, crib_sw_gadget,
      jio_gadget, url_list = [];
    path_to_load = gadget.props.element.querySelector('form.crib-load-from-jio .load-path').value
    application_id = gadget.props.element.querySelector('form.crib-load-from-jio .load-id').value
    path_to_load_length = path_to_load.length;
    return new RSVP.Queue()
    .push(function () {
      return RSVP.all([
        gadget.getDeclaredGadget('jio_gadget')]);
    })
    .push(function (gadget_list) {
      jio_gadget = gadget_list[0];
      return jio_gadget.allAttachments("/" + application_id + ".attachment/");
    })
      .push(function(response) {
        var promise_list = [],
          key, extension;
        for (key in response) {
          if (response.hasOwnProperty(key)) {
            extension = getExtension(key);
            url_list.push(atob(key.substr(0, key.length - extension.length)))
            promise_list.push(jio_gadget.getAttachment("/" + application_id + ".attachment/", key));
          }
        };
        return RSVP.all(promise_list)
      })
      .push(function(response_list) {
        var promise_list = [],
          i, i_len, url, index, response, location, location_len;
        location = document.location.origin;
        location_len = location.length
        for (i = 0, i_len = response_list.length; i < i_len; i += 1) {
          url = url_list[i]
          index = url.indexOf(location);
          if (index != -1)
            url = url.substr(index + location_len);
          promise_list.push( 
            gadget.crib_sw_put(path_to_load + url, {blob: response_list[i]})
          )
        }
      })
      .push(function() {
        gadget.props.element.querySelector(".crib-load-from-jio-status").textContent = "Loaded " + url_list.length + " files at "+ Date ()
      });
  }
  
  
  rJS(window)
    .ready(function (g) {
      g.props = {};
      return g.getElement()
        .push(function (element) {
          g.props.element = element;
          g.props.start_deferred = RSVP.defer();
         });
    })
    .declareAcquiredMethod("crib_sw_allDocs", "crib_sw_allDocs")
    .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;
      gadget.props.element.querySelector('form.crib-save-to-jio .save-path').value = document.location.origin;
      gadget.props.element.querySelector('form.crib-load-from-jio .load-path').value = document.location.origin;
      return new RSVP.Queue()
        .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 save content to jIO
          promise_list.push(loopEventListener(
            gadget.props.element.querySelector(".crib-save-to-jio"),
            'submit',
            false,
            function (event) {saveContentToJIO(gadget, event)}
          ));
          // promise to load content from jIO
          promise_list.push(loopEventListener(
            gadget.props.element.querySelector(".crib-load-from-jio"),
            'submit',
            false,
            function (event) {loadContentFromJIO(gadget, event)}
          ));
          // promise to use dav storage for jIO
          promise_list.push(loopEventListener(
            gadget.props.element.querySelector(".form-use-jio-dav"),
            'submit',
            false,
            function (event) {createDAVJio(gadget, event)}
          ));
          // promise to use local storage for jIO
          promise_list.push(loopEventListener(
            gadget.props.element.querySelector(".form-use-jio-local"),
            'submit',
            false,
            function (event) {createJio(gadget, {type: "indexeddb", database: "cribjs"})}
          ));
          return RSVP.all(promise_list);
        });
    })  
}(window, rJS, loopEventListener));