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

  rJS(window)
    /////////////////////////////////////////////////////////////////
    // Acquired methods
    /////////////////////////////////////////////////////////////////
    .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: "file_fif"}});
      }
      return this.getUrlFor.apply(this, argument_list);
    })
    /*.allowPublicAcquisition('getUrlFor', function (argument_list) {
      if (argument_list[0].command === 'index') {
        return this.getUrlFor({command: 'display_stored_state', options: {jio_key: argument_list[0].options.jio_key}});
      }
      return this.getUrlFor.apply(this, argument_list);
    })
    .allowPublicAcquisition('getUrlParameter', function (argument_list) {
      return this.getUrlParameter(argument_list)
        .push(function (result) {
          if ((result === undefined) && (argument_list[0] === 'field_listbox_sort_list:json')) {
            return [['title', 'ascending']];
          }
          return result;
        });
    })*/
    .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 (options) {
      var gadget = this,
        dataset = (options) ? options.reference : "",
        header_dict = {
          page_title: 'Files',
          filter_action: true
        };
      return gadget.getDeclaredGadget('form_list')
      .push(function (form_gadget) {
        var column_list = [
          ['title', 'Title'],
          ['reference', 'Reference'],
          //['version', 'version'],
          ['validation_state', 'State']
        ];
        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+Array%22+AND+validation_state%3A%22validated%22+AND+reference%3A%22" + dataset + "%2F%25%22",
              "portal_type": [],
              "search_column_list": column_list,
              "sort_column_list": column_list,
              "title": "Files",
              "sort": [['modification_date', 'descending']],
              "type": "ListBox"
            }
          }},
            "_links": {
              "type": {
                name: ""
              }
            }},
            form_definition: {
              group_list: [[
                "bottom",
                [["listbox"]]
              ]]
            }
          });
      });
    });
}(window, rJS));