dojo.provide("parus.designerEdit");

dojo.require("dijit.form.DateTextBox");
dojo.require("dijit.form.NumberTextBox");
dojo.require("dijit.form.ValidationTextBox");
dojo.require("dijit.form.TimeTextBox");
dojo.require("dijit.form.CheckBox");

dojo.require("parus.parus8");
dojo.require("parus.dictEdit");


dojo.declare("parus.designerDictEdit", [parus.dictEdit], {

  unitCode: null,

  methodCode: null,

  inCode: null,

  outCode: null,

  dictionaryInfoText: null,

  dictionaryInfoPending: false,

  autoComplete: false,

  postMixInProperties: function() {
    this._checkInfo();
    this.inherited(arguments);
  },

  _dictionaryInfoLoaded: function() {

    if (this.autoComplete) {

      var dictionary = dojo.fromJson(this.dictionaryInfoText);

      this.searchAttr = dictionary.labelField;

      var key = this.unitCode + "#" + this.methodCode + "#" + this.inCode + "#" + this.outCode;

      var store = parus.dictionaries._stores ? parus.dictionaries._stores[key] : null;

      if (!store) {

        var sortField = dictionary.labelAttr;
        dojo.forEach(dictionary.columns, function(column) {
          if (column.fieldName == dictionary.labelAttr) sortField = column.sortField || dictionary.labelAttr;
        });
        var sortObj = {
          attribute: dictionary.labelAttr,
          descending: false,
          field: sortField
        };

        store = new parus.gridData({
          asJson: true,
          persistent: true,
          __lastSort: dojo.clone(sortObj),
          __lastConditions: {
            __sort: dojo.clone(sortObj)
          },
          _data: parus.database.queryObject(dictionary.queryID, this.onGetGridConditions() || {})
        });

        if (!parus.dictionaries._stores) parus.dictionaries._stores = {};
        parus.dictionaries._stores[key] = store;

      }

      this.store = store;

      this.store.fetch({
        start: 0,
        query: {},
        queryOptions: {
          ignoreCase: true,
          deep: true
        },
        onBegin: dojo.hitch(this, function(cnt) {
          this._itemCount = cnt;
          if (parus.dictionaries._autoCompleteLimit != null) {
            if (this._itemCount >= parus.dictionaries._autoCompleteLimit) {
              this.autoComplete = false;
              this.searchAttr = "";
            }
          }
        })
      });

      this._setStoreAttr(this.store);
    }

  },

  _checkInfo: function() {


    if (this.unitCode && this.methodCode && this.inCode && this.outCode && !this.dictionaryInfoText && !this.dictionaryInfoPending) {
      this.dictionaryInfoPending = true;

      parus.database.execute("designerUnitInfo", {
        "UNITCODE": this.unitCode,
        "METHOD_CODE": this.methodCode,
        "INPARAM_CODE": this.inCode,
        "OUTPARAM_CODE": this.outCode
      }, dojo.hitch(this, function(response) {

        this.dictionaryInfoText = parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "clobDEFINITION");
        this.dictionaryInfoPending = false;
        this._dictionaryInfoLoaded();
      }),

      dojo.hitch(this, function(e) {
        dojo.query(".dijitArrowButtonInner", this.domNode).style({
          display: "none"
        });
        this.dictionaryInfoText = "{}";
        this.dictionaryInfoPending = false;
      }));

    };


  },

  onGetGridConditions: function() {

    var obj = this.inherited(arguments) || {};
    dojo.mixin(obj, {
      "UNITCODE": this.unitCode,
      "METHOD_CODE": this.methodCode,
      "INPARAM_CODE": this.inCode,
      "OUTPARAM_CODE": this.outCode
    });
    return obj;
  },

  dictButtonClick: function() {

    this._checkInfo();

    if (this.showingDictionary) return;

    if (this.dictionaryInfoPending) {
      setTimeout(dojo.hitch(this, arguments.callee), 1000);
      return;
    };

    var dictionaryInfo = dojo.fromJson(this.dictionaryInfoText);

    dojo.mixin(dictionaryInfo, {
      multiselect: this.multiselect,
      label: this.value,
      dictionary: dictionaryInfo,
      unitCode: this.unitCode || dictionaryInfo.unitCode,
      gridConditions: this.onGetGridConditions(),
      userDataSection: this.unitCode || dictionaryInfo.unitCode,
      menu: this.menu,

      catalogConditions: {
        "METHOD_CODE": this.methodCode,
        "INPARAM_CODE": this.inCode,
        "OUTPARAM_CODE": this.outCode
      },

      onApply: dojo.hitch(this, function(dialog) {
        this.setValue(dialog.getLabel());
        this.onApplyDictionary(dialog);
        return true;
      }),

      onClose: dojo.hitch(this, function(dialog) {
        this.showingDictionary = false;
        dijit.focus(this.focusNode);
      })
    });

    this.showingDictionary = true;
    dijit.hideTooltip(this.domNode);
    setTimeout(function() {
      new parus.dictionaryDialog(dictionaryInfo).show();
    }, 100);
  }

});