dojo.provide("parus.editableDictionaryDialog");

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.gridData");
dojo.require("parus.gridActions");
dojo.require("parus.catalogActions");
dojo.require("parus.dictionaryDialog");

dojo.require("parus.editDictionaryDialog");

dojo.declare("parus.editableDictionaryDialog", [parus.dictionaryDialog], {

  parentDialog: null,

  editByDoubleClick: false,
  //edit selected or return selected
  editDialogClass: null,
  //override with non-abstract class inherited of parus.editDictionaryDialog
  editDetailDialogClass: null,
  //override with non-abstract class inherited of parus.editDictionaryDialog
  allowEditCatalog: true,
  allowMoveCatalog: true,
  moveCatalogQueryID: "moveCatalog",

  moveDocumentQueryID: null,

  postMixInProperties: function() {
    this.templateString = dojo.cache(dojo.moduleUrl("parus", "dictionaryDialog.html"));
    this.inherited(arguments);
  },

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

    if (this.editDialogClass) this.menu = this.createMenu();
    if (this.detailQueryID && this.editDetailDialogClass) this.detailMenu = this.createDetailMenu();
    if (this.catalogsQueryID && (this.allowEditCatalog || this.allowMoveCatalog)) this.catalogMenu = this.createCatalogMenu();
  },

  //to override
  createMenu: function() {
    return this._createCustomMenu(this.menu, this.grid, this.editDialogClass, this.editByDoubleClick, this.moveDocumentQueryID, false);
  },

  //to override
  createDetailMenu: function() {
    return this._createCustomMenu(this.detailMenu, this.detailGrid, this.editDetailDialogClass, true, null, true);
  },

  //to override
  createCatalogMenu: function() {
    var menu = this.catalogMenu;

    var hasMenu = !! menu;
    if (!hasMenu) menu = new dijit.Menu({
      "class": "balanceMenu"
    });

    var index = 0;

    if (this.allowEditCatalog) {
      menu.addChild(new dijit.MenuItem({
        label: "Добавить...",
        itemAction: false,
        actionCode: "CATALOG_INSERT",
        defaultIcon: "PCHardwareadaltCustomIcon",
        onClick: dojo.hitch(this, function() {
          parus.catalogActions.insertCatalog(this.catalogTree);
        })
      }), index++);

      menu.addChild(new dijit.MenuItem({
        label: "Исправить...",
        itemAction: true,
        actionCode: "CATALOG_UPDATE",
        defaultIcon: "PCdocumentCustomIcon",
        onClick: dojo.hitch(this, function() {
          parus.catalogActions.updateCatalog(this.catalogTree);
        })
      }), index++);
    }

    if (this.allowMoveCatalog) menu.addChild(new dijit.MenuItem({
      label: "Переместить...",
      itemAction: true,
      actionCode: "CATALOG_MOVE",
      onClick: dojo.hitch(this, function() {
        parus.catalogActions.moveCatalog(this.catalogTree, this.moveCatalogQueryID);
      })
    }), index++);

    if (this.allowEditCatalog) menu.addChild(new dijit.MenuItem({
      label: "Удалить",
      itemAction: true,
      actionCode: "CATALOG_DELETE",
      defaultIcon: "PCreduceCustomIcon",
      onClick: dojo.hitch(this, function() {
        parus.catalogActions.deleteCatalog(this.catalogTree);
      })
    }), index++);

    if (hasMenu) menu.addChild(new dijit.MenuSeparator(), index++);

    menu.startup();

    return menu;
  },

  _createCustomMenu: function(menu, grid, editDialogClass, editByDoubleClick, moveDocumentQueryID, isDetail) {
    var hasMenu = !! menu;
    if (!hasMenu) menu = new dijit.Menu({
      "class": "balanceMenu"
    });

    var index = 0;

    menu.addChild(new dijit.MenuItem({
      label: "Добавить...",
      itemAction: false,
      detailAction: isDetail,
      defaultIcon: "PCHardwareadaltCustomIcon",
      onClick: dojo.hitch(this, function() {
        this.insertAction(this, grid, editDialogClass);
      })
    }), index++);

    menu.addChild(new dijit.MenuItem({
      label: "Размножить...",
      itemAction: true,
      defaultIcon: "PCcopyCustomIcon",
      onClick: dojo.hitch(this, function() {
        this.duplicateAction(this, grid, editDialogClass);
      })
    }), index++);

    menu.addChild(new dijit.MenuItem({
      label: "Исправить...",
      itemAction: true,
      "default": editByDoubleClick,
      defaultIcon: "PCdocumentCustomIcon",
      onClick: dojo.hitch(this, function() {
        this.updateAction(this, grid, editDialogClass);
      })
    }), index++);

    if (this.catalogTree && moveDocumentQueryID) menu.addChild(new dijit.MenuItem({
      label: "Переместить...",
      itemAction: true,
      onClick: dojo.hitch(this, function() {
        parus.gridActions.moveDocumentToCatalog(grid, this.catalogTree, this.unitCode, moveDocumentQueryID);
      })
    }), index++);

    menu.addChild(new dijit.MenuItem({
      label: "Удалить",
      itemAction: true,
      defaultIcon: "PCreduceCustomIcon",
      onClick: dojo.hitch(this, function() {
        this.deleteAction(this, grid, editDialogClass);
      })
    }), index++);

    if (hasMenu) menu.addChild(new dijit.MenuSeparator(), index++);

    menu.startup();

    return menu;
  },

  //static
  insertAction: function(dictionaryDialog, grid, editDialogClass) {
    var store = grid.store;

    new editDialogClass({
      titlePostfix: "Добавление",
      parentDialog: dictionaryDialog,
      store: store,
      onApply: dojo.hitch(dictionaryDialog, function(dialog) {
        var rn = dialog.executeInsert(false);
        if (rn) parus.balance.refreshGrid(grid, "RN", rn);
        return !!rn;
      })
    }).show();
  },

  //static
  duplicateAction: function(dictionaryDialog, grid, editDialogClass) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    new editDialogClass({
      titlePostfix: "Размножение",
      parentDialog: dictionaryDialog,
      store: store,
      item: item,
      onApply: dojo.hitch(dictionaryDialog, function(dialog) {
        var rn = dialog.executeInsert(true);
        if (rn) parus.balance.refreshGrid(grid, "RN", rn);
        return !!rn;
      })
    }).show();
  },

  //static
  updateAction: function(dictionaryDialog, grid, editDialogClass) {
    var store = grid.store;
    var index = grid.selection.getSelectedIndex();
    var item = grid.getItem(index);

    new editDialogClass({
      titlePostfix: "Исправление",
      parentDialog: dictionaryDialog,
      store: store,
      item: item,
      editMode: true,
      onApply: dojo.hitch(dictionaryDialog, function(dialog) {
        var updated = dialog.executeUpdate();
        if (updated) parus.balance.refreshGrid(grid, "RN", dialog.rn);
        return updated;
      })
    }).show();
  },

  //static
  deleteAction: function(dictionaryDialog, grid, editDialogClass) {
    parus.gridActions.deleteSelected(grid, editDialogClass.prototype.executeDelete);
  },

  showDictionaryAction: function(dictionaryInfo, dialogClass, editDialogClass, editDetailDialogClass) {
    dojo.mixin(dictionaryInfo, {
      dictionary: dictionaryInfo,
      unitCode: dictionaryInfo.unitCode,
      editDialogClass: editDialogClass,
      editDetailDialogClass: editDetailDialogClass
    });

    if (dictionaryInfo.menu) dictionaryInfo.menu.startup();

    if (!dialogClass) dialogClass = parus.editableDictionaryDialog;

    var dialog = new dialogClass(dictionaryInfo);
    dialog.parentDialog = this;
    dialog.show();

    return dialog;
  },

  getGridCondition: function(field) {
    if (this.gridConditions && this.gridConditions.hasOwnProperty(field)) return this.gridConditions[field];
  },

  getParentDialogCondition: function(field) {
    //get parentDialog PRN (not master grid for detail) - PRN of master grid!
    if (this.parentDialog && this.parentDialog.gridConditions && this.parentDialog.gridConditions.hasOwnProperty(field)) return this.parentDialog.gridConditions[field];
  },

  getParentDialogMasterGridItem: function(fieldName) {
    if (this.parentDialog && this.parentDialog.grid) {
      var grid = this.parentDialog.grid;
      var store = grid.store;
      var item = grid.selection.getFirstSelected();

      return store.getValue(item, fieldName);
    }
  },

  getParentDialogDetailGridItem: function(fieldName) {
    if (this.parentDialog && this.parentDialog.detailGrid) {
      var grid = this.parentDialog.detailGrid;
      var store = grid.store;
      var item = grid.selection.getFirstSelected();

      return store.getValue(item, fieldName);
    }
  }

});