package kz.cafe.admin.panel_extensions;

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

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

   private final Table table;
   
   private final Button btMenu = new Button("Меню"); 
   
   
   public ExtraPanelMenu(final AdminMain application, final DBListPanel<?> pnlParent){

      AbstractLayout panelLayout = (AbstractLayout) getContent();
      panelLayout.setMargin(false);      
      
      
      
      this.table = pnlParent.table;

      btMenu.addListener(new ClickListener() {

         private static final long serialVersionUID = 4542127344900596681L;

         @Override
         public void buttonClick(ClickEvent event){
            
            Window window = new Window("Меню");
            window.setModal(true);
            window.center();
            
            Dimension dim = Dimension.get80(table.getApplication());
            
            window.setSizeUndefined();
            window.setVisible(true);
            window.setWidth(dim.iWidth + "px");
            
            
            
            
            
            CustomDescriptor dscr = TabCourse.buildSelector();

            
            AdminMain app = (AdminMain) pnlParent.getApplication();
            
            
            Constraint constraint = new Constraint();
            
            
            int ID = (Integer) table.getValue();
            
            Constraint.Criterion criterion = new Constraint.Criterion("CID",  ID);
            constraint.criteria.add(criterion);
            
            if(application.isAdmin){
            }
            else{
               constraint.criteria.add(new Constraint.Criterion(MUser.F_OWNER_ID, application.UID));
            }
            
            
            DBListPanel<MCourse> panelCourse = new DBListPanel<MCourse>(app,
                                                      MCourse.class,
                                                      dscr,
                                                      Dimension.get80(app),
                                                      AccessConstants.full(),
                                                      constraint);
            
            panelCourse.reload();
            
            panelCourse.addPanel(new ExtraPanelImageUploader(panelCourse, (CustomImageField) dscr.mapDBFields.get("image")), false);
            
            panelCourse.addPanel(new ExtraPanelMany2Many(application,
                                                         "Ингредиенты ...",
                                                         MIngredient.class,
                                                         "ingredient",
                                                         AccessConstants.none(),
                                                         panelCourse), false);
            

            AbstractLayout panelLayout = (AbstractLayout) panelCourse.getContent();
            panelLayout.setMargin(false);
            
            panelCourse.setStyleName(Runo.PANEL_LIGHT);
            
            window.setContent(panelCourse);
            pnlParent.getApplication().getMainWindow().addWindow(window);
         }
      });
      
      btMenu.setEnabled(false);

      this.addComponent(btMenu);
   }
   

   



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