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

  rJS(window)
    /////////////////////////////////////////////////////////////////
    // Acquired methods
    /////////////////////////////////////////////////////////////////
    //.declareAcquiredMethod("updateHeader", "updateHeader")
    .declareAcquiredMethod("getUrlParameter", "getUrlParameter")
    .declareAcquiredMethod("getUrlFor", "getUrlFor")

    /////////////////////////////////////////////////////////////////
    // declared methods
    /////////////////////////////////////////////////////////////////
    /*.allowPublicAcquisition('updateHeader', function () {
      return;
    })*/
    .allowPublicAcquisition('getUrlFor', function (argument_list) {
      if (argument_list[0].command === 'index') {
        return this.getUrlFor({command: 'index', options: {jio_key: argument_list[0].options.jio_key, page: "data_set"}});
      }
      return this.getUrlFor.apply(this, argument_list);
    })
    .declareMethod("triggerSubmit", function () {
      var argument_list = arguments;
      return this.getDeclaredGadget('form_list')
        .push(function (gadget) {
          return gadget.triggerSubmit.apply(gadget, argument_list);
        });
    })
    .declareMethod("render", function () {
      var gadget = this;
      return gadget.getDeclaredGadget('form_list')
        .push(function (form_gadget) {
          var column_list = [
            ['title', 'Title'],
            ['reference', 'Reference'],
            ['version', 'Version '],
            ['description', 'Description']
          ];
          return form_gadget.render({
            erp5_document: {"_embedded": {"_view": {
              "listbox": {
                "column_list": column_list,
                "show_anchor": 0,
                "default_params": {},
                "editable": 1,
                "editable_column_list": [],
                "key": "field_listbox",
                "lines": 15,
                "list_method": "portal_catalog",
                "query": "urn:jio:allDocs?query=portal_type%3A%22Data+Set%22+AND+validation_state%3A%22validated%22",
                "portal_type": [],
                "search_column_list": column_list,
                "sort_column_list": column_list,
                "sort": [['modification_date', 'descending']],
                "title": "Data Sets",
                "type": "ListBox"
              }
            }},
              "_links": {
                "type": {
                  // form_list display portal_type in header
                  name: ""
                }
              }},
              form_definition: {
                group_list: [[
                  "bottom",
                  [["listbox"]]
                ]]
              }
            });
        })
        .push(function () {
          return gadget.getDeclaredGadget("download_access");
        })
        .push(function (my_gadget) {
          return my_gadget.render();
        });
    });
}(window, rJS));