/*
 * Copyright (c) 2013, TRIOLOGY GmbH
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * https://www.scm-manager.com
 *
 */

Triology.groupmanager.Panel = Ext.extend(Sonia.rest.Panel, {

  titleText : Triology.groupmanager.I18n.groupManagerFormTitleText,
  emptyText : Triology.groupmanager.I18n.emptyText,
  removeTitleText : Triology.groupmanager.I18n.removeTitleText,
  removeMsgText : Triology.groupmanager.I18n.removeMsgText,
  errorTitleText : Triology.groupmanager.I18n.errorTitleText,
  errorMsgText : Triology.groupmanager.I18n.errorMsgText,

  // grid
  groupmanagerGrid : null,

  initComponent : function() {
    var config = {
      tbar : [ {
        xtype : 'tbbutton',
        text : this.reloadText,
        icon : this.reloadIcon,
        scope : this,
        handler : this.reload
      } ],
      items : [ {
        id : 'groupmanagerGrid',
        xtype : 'groupmanagerGrid',
        region : 'center',
        parentPanel : this
      }, {
        id : 'groupmanagerEditPanel',
        xtype : 'tabpanel',
        activeTab : 0,
        height : 250,
        split : true,
        border : true,
        region : 'south',
        items : [ {
          bodyCssClass : 'x-panel-mc',
          title : this.titleText,
          padding : 5,
          html : this.emptyText
        } ]
      } ]
    };

    Ext.apply(this, Ext.apply(this.initialConfig, config));
    Sonia.group.Panel.superclass.initComponent.apply(this, arguments);
  },

  getGrid : function() {
    if (!this.groupmanagerGrid) {
      if (debug) {
        console.debug('could not find groupmanager grid, fetch by id');
      }
      this.groupmanagerGrid = Ext.getCmp('groupmanagerGrid');
    }
    return this.groupmanagerGrid;
  },

  updateHistory : function(group) {
    var token = Sonia.History.createToken('groupmanagerPanel', group.name);
    Sonia.History.add(token);
  },

  resetPanel : function() {
    Sonia.group.setEditPanel(Sonia.group.DefaultPanel);
  },

  reload : function() {
    this.getGrid().reload();
  }

});

// register xtype
Ext.reg('groupmanagerPanel', Triology.groupmanager.Panel);

// register history handler
Sonia.History.register('groupmanagerPanel', {

  onActivate : function(panel) {
    var token = null;
    var rec = panel.getGrid().getSelectionModel().getSelected();
    if (rec) {
      token = Sonia.History.createToken('groupmanagerPanel', rec.get('name'));
    } else {
      token = Sonia.History.createToken('groupmanagerPanel');
    }
    return token;
  },

  onChange : function(groupId) {
    var panel = Ext.getCmp('groupmanagerPanel');
    if (!panel) {
      Triology.groupmanager.addGroupManagerTabPanel();
      panel = Ext.getCmp('groupmanagerPanel');
      if (groupId) {
        var selected = false;
        panel.getGrid().getStore().addListener('load', function() {
          if (!selected) {
            panel.getGrid().selectById(groupId);
            selected = true;
          }
        });
      }
    } else {
      main.addTab(panel);
      if (groupId) {
        panel.getGrid().selectById(groupId);
      }
    }
  }
});