dojo.provide("parus.signDialog");

dojo.require("dijit.Dialog");
dojo.require("dijit.form.Button");

dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileReadStore");

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

dojo.require("parus.digitalSignature");

dojo.declare("parus.signDialog", [parus.Dialog], {

  rn: null,

  unitCode: null,

  showNote: true,
  allowSimpleSign: true,

  checkPluginVersion: false,

  requiredVersion: {
    major: 0,
    minor: 0,
    build: 0
  },

  isUserCertificatesUsed: false,

  postCreate: function() {
    this.inherited(arguments);

    this.noteEdit.attr("value", null);
    if (!this.showNote) this.notePanel.domNode.style.display = "none";
    if (!this.allowSimpleSign) this.simpleSignPane.domNode.style.display = "none";

    parus.balance._createActionsButton(this.popupMenu, this.headerPane);
    this.popupMenu.bindDomNode(this.certificateGrid.viewsNode);

    var settings = parus.userdata.get(this.declaredClass);
    if (settings) {
      parus.gridSettingsDialog.mergeGridSettings(this.certificateGrid, settings.gridSettings);
      parus.gridSettingsDialog.mergeColorConditions(this.certificateGrid, settings.colorConditions);
    }
  },

  show: function() {
    var dialog = this;
    this.inherited(arguments).then(
    dojo.hitch(this, function() {
      parus.digitalSignature.checkPlugin().then(
      dojo.hitch(this, function() {
        if (!this.checkPluginVersion || (this.checkPluginVersion && parus.digitalSignature.checkPluginVersion(this.requiredVersion))) {
          parus.digitalSignature.certificatesStore(this.getAllowedUserCerts()).then(
          dojo.hitch(this, function(store) {
            this.certificateGrid.attr("store", store);
            this.certificateGrid.setQuery(this.certificateGrid.query);
            this.certificateGrid.selection.select(0);
          }), function(message) {
            parus.parus8.errorMessage(message);
          });
        }
      }), function(message) {
        dialog.close();
        parus.parus8.errorMessage(message);
      })
    }));
  },

  onSimpleSignCheckBoxClicked: function() {
    this.certificateGrid.domNode.style.display = this.simpleSign.checked ? "none" : "";
  },

  getAllowedUserCerts: function() {
    this.isUserCertificatesUsed = false;

    var response = parus.database.execute("GetContextUserCertificates", {
      RN: this.rn.join(";"),
      UNITCODE: this.unitCode
    });

    if (!response) return [];

    this.isUserCertificatesUsed = parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "UseUserCertificates") == "1";
    if (this.isUserCertificatesUsed) return (parus.parus8.nodeField(parus.parus8.rowNodes(response)[0], "sTHUMBPRINTS") || "").split(",");
    else return null;
  },

  getNote: function() {
    return this.noteEdit.value;
  },

  getIsSimpleSign: function() {
    return this.simpleSign.checked;
  },

  getCertificateAttribute: function(name) {
    if (this.simpleSign.checked) return null;

    var grid = this.certificateGrid;
    return grid.store.getValue(grid.getItem(grid.selection.getSelectedIndex()), name);
  },

  getSerialNumber: function() {
    return this.getCertificateAttribute("SERIALNUMBER");
  },

  getThumbprint: function() {
    return this.getCertificateAttribute("THUMBPRINT");
  },

  getIssuedTo: function() {
    return this.getCertificateAttribute("ISSUEDTO");
  },

  getIssuedBy: function() {
    return this.getCertificateAttribute("ISSUER");
  },

  getValidFrom: function() {
    return this.getCertificateAttribute("VALIDFROMDATE");
  },

  getValidTo: function() {
    return this.getCertificateAttribute("VALIDTODATE");
  },

  getGOSTVersion: function() {
    return this.getCertificateAttribute("GOST_VERSION");
  },

  getGOSTKeySize: function() {
    return this.getCertificateAttribute("GOST_KEYSIZE");
  }
});