package kz.cafe.admin.panel_extensions;

import java.util.*;
import kz.cafe.admin.*;
import kz.cafe.admin.constants.*;
import kz.cafe.admin.panels.*;
import kz.cafe.db.*;
import kz.cafe.db.model.*;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.*;

public class ExtraPanelMany2Many extends AbstractExtendedPanel implements java.io.Serializable{
   private static final long serialVersionUID = 711754256190346567L;

   private final DBListPanel<?> pnlParent;
   private final Button btSelector;
   
   
   public ExtraPanelMany2Many(final AdminMain application,
                              final String sButtonName,
                              final Class<?> classSelector,
                              final String sField,
                              final Set<AccessConstants.EditMode> setMode,
                              final DBListPanel<?> pnlParent){
      
      AbstractLayout panelLayout = (AbstractLayout) getContent();
      panelLayout.setMargin(false);      
      
      
      this.pnlParent = pnlParent;
      this.btSelector = new Button(sButtonName);
      this.btSelector.setEnabled(false);

      
      btSelector.addListener(new Button.ClickListener(){
         private static final long serialVersionUID = -2751879007281094866L;
         @SuppressWarnings("unchecked")
         @Override
         public void buttonClick(ClickEvent event){
            
            MAbstractEditableDataElement recordToEdit = pnlParent.oldValueMongoObject;
            
            if(recordToEdit == null){
               //we are in RearMode
               
               recordToEdit = (MAbstractEditableDataElement) MongoService.get(pnlParent.classDB, (int) pnlParent.table.getValue());
            }
            
            Set<Integer> setValue;
            java.lang.reflect.Field field; 
            try{
               field = pnlParent.classDB.getField(sField); 
               setValue = (Set<Integer>) field.get(recordToEdit);
            }
            catch (Throwable e){
               throw new Error(e);
            }  
            
            
            Window window = new Window(sButtonName);
            
            DBListSelectorMultiPanel pnlSelector = new DBListSelectorMultiPanel(application,
                                                                                application.mapMenuClass.get(classSelector).abstractTab,
                                                                                window,
                                                                                recordToEdit,
                                                                                field);

            //Let's mark the things to highlight
            pnlSelector.panel.setSelectedIDes.addAll(setValue);
            
            //Reload
            pnlSelector.panel.reload();
            
            
            
            pnlSelector.setStyleName(Runo.PANEL_LIGHT);
            

            window.setContent(pnlSelector);
            window.setSizeUndefined();
            window.setModal(true);
            window.center();
            
            window.setWidth(Dimension.get80(application).iWidth + "px");
            
            
            application.getMainWindow().addWindow(window);
         }
      });
      
      this.addComponent(btSelector);
   }

   



   @Override
   public void changeFocus(){
      if(pnlParent.table.getValue() == null){
         btSelector.setEnabled(false);
      }
      else{
         btSelector.setEnabled(true);
      }
   }
}
