/**
 * Klasa - kontener danych
 * @author Grzegorz Kęs <gkes@ispik.pl>
 * @version 1.0.0
 */
var MAATTableModel = Class.create();
MAATTableModel.prototype = {
  ident: null,
  _data: {},
  /**
   * Konstruktor
   */
  initialize: function(ident) {
    this._data = {};
    this._debug = true;
    this.ident = ident;
  },
  /**
   * Metoda zwraca model tabelki
   */
  get: function () {
      return this._data;
  },
  /**
   * Metoda zwraca model tabelki jako JSON
   */
  getAsJSON: function () {
      return Object.toJSON(this._data);
  },
  /**
   * Ustalenie modelu tabelki
   * @param data model tabeli zgodny z formatem JSON
   */
  set: function (data) {
      if (typeof data == 'string') {
          if(data.isJSON()) {
              this._data = data.evalJSON(false);
          } else {
              if (this._debug) alert('Model tabeli. Niepoprawny JSON!');
          }
      } else if (typeof data == 'object') {
        for (var i = 0; i < data.length; i++) {
          this._data[this.getModelIdent(i)] = data[i];
        }
      }
    },
  getModelIdent: function (ident) {
    return this.ident+'_body_'+ident;
  },
  /**
   * Usunięcie wiersza z modelu
   * @param rowIdx unikalny identyfikator wiersza
   */
  remove: function (rowIdx) {
      this._data[rowIdx] = null;
  },
  /**
   * Dodanie wiersza do modelu
   * @param rowIdx unikalny identyfikator wiersza
   * @param data model wiersza zgodny z JSON
   */
  add: function (rowIdx, data) {
      if(Object.isArray(data)) {
          this._data[rowIdx] = data;
      } else {
          if (this._debug) alert('Model tabeli. Niepoprawny JSON!');
      }
  },
  find: function (cellIdx, value) {
      var result = false;
        for(var i = 0; i < Object.keys(this._data).length; i++) {
            if(Object.values(this._data)[i][cellIdx].value == value) {
                result = true;
            }
        }
      return result;
  },
  complexfind: function (cellIdx1, value1, cellIdx2, value2) {
      var result = false;
        for(var i = 0; i < Object.keys(this._data).length; i++) {
            if(Object.values(this._data)[i][cellIdx1].value == value1 &&
               Object.values(this._data)[i][cellIdx2].value == value2) {
                result = true;
            }
        }
      return result;
  },
  /**
   * Aktualizacja komórki w danym wierszu
   * @param tableIdx unikalny identyfikator tabeli
   * @param rowIdx unikalny identyfikator wiersza
   * @param cellIdx unikalny identyfikator kolumny w ramach wiersza
   * @param data model wiersza zgodny z JSON
   */
  cellUpdate:function (tableIdx, rowIdx, cellIdx, data) {
      this._data[tableIdx+'model_body_'+rowIdx][cellIdx].value = data;
  },
  cellLinkUpdate:function (tableIdx, rowIdx, cellIdx, data) {
      this._data[tableIdx+'model_body_'+rowIdx][cellIdx].value = data;
  },
  getCellValue:function (tableIdx, rowIdx, cellIdx, data) {
      return this._data[tableIdx+'model_body_'+rowIdx][cellIdx].value;
  }
};