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

  function setDropboxConfiguration(gadget, token) {
    return new RSVP.Queue()
      .push(function () {
        return RSVP.all([
          gadget.getSetting("portal_type"),
          gadget.getSetting("erp5_attachment_synchro", "")
        ]);
      })
      .push(function (setting) {
        var configuration = {},
          attachment_synchro = setting[1] !== "";
        configuration = {
          type: "replicate",
          query: {
            query: 'portal_type:"' + setting[0] + '" ',
            limit: [0, 100],
            sort_on: [["modification_date", "descending"]]
          },
          use_remote_post: false,
          conflict_handling: 2,
          check_local_attachment_modification: attachment_synchro,
          check_local_attachment_creation: attachment_synchro,
          check_remote_attachment_modification: attachment_synchro,
          check_remote_attachment_creation: attachment_synchro,
          check_remote_attachment_deletion: attachment_synchro,
          check_local_modification: true,
          check_local_creation: true,
          check_local_deletion: false,
          check_remote_modification: true,
          check_remote_creation: true,
          check_remote_deletion: true,
          signature_sub_storage: {
            type: "query",
            sub_storage: {
              type: "uuid",
              sub_storage: {
                type: "indexeddb",
                database: "officejs-dropbox-hash"
              }
            }
          },
          local_sub_storage: {
            type: "mapping",
            attachment: {
              'data': {
                get: {uri_template: 'enclosure'},
                put: {uri_template: 'enclosure'}
              }
            },
            sub_storage: {
              type: "query",
              sub_storage: {
                type: "uuid",
                sub_storage: {
                  type: "indexeddb",
                  database: "officejs-dropbox"
                }
              }
            }
          },
          remote_sub_storage: {
            type: "mapping",
            attachment: {
              'data': {
                get: {uri_template: 'enclosure'},
                put: {uri_template: 'enclosure'}
              }
            },
            mapping_dict: {
              "portal_type": ["equalSubProperty", "type"]
            },
            sub_storage: {
              type: "query",
              sub_storage: {
                type: "drivetojiomapping",
                sub_storage: {
                  type: "dropbox",
                  access_token: token,
                  root: "sandbox"
                }
              }
            }
          }
        };
        return gadget.setSetting('jio_storage_description', configuration);
      })
      .push(function () {
        return gadget.setSetting('jio_storage_name', "DROPBOX");
      })
      .push(function () {
        return gadget.setSetting('sync_reload', true);
      })
      .push(function () {
        // XXX Please note this is a work-around to have a usable application
        // waiting for a bug in IndexedDB or jIO to be fixed
        // this bug can been seen on 'fast' devices where
        // data stored in settings is not accessible right after save.
        // it was shown that after redirecting to sync page, the
        // "sync_reload" value got in the sync gadget was "undefined"
        // Hence triggering synchronization and not setting the storage first
        // and redirecting once again on the jio configuration page
        return gadget.getSetting('sync_reload');
      })
      .push(function () {
        return gadget.redirect({
          command: "display",
          options: {page: 'ojs_sync', auto_repair: 'true'}
        });
      });
  }

  var gadget_klass = rJS(window);

  gadget_klass
    .declareAcquiredMethod("redirect", "redirect")
    .declareAcquiredMethod("getSetting", "getSetting")
    .declareAcquiredMethod("setSetting", "setSetting")
    .declareMethod("render", function (options) {
      return setDropboxConfiguration(this, options.access_token);
    });

}(window, rJS, RSVP, console));