package kz.cafe.admin.panel_extensions;

import java.util.*;
import kz.cafe.admin.*;
import kz.cafe.admin.AdminMain.MenuElement;
import kz.cafe.admin.constants.AccessConstants.EditMode;
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;

public class ExtraPanelFeedbacks extends AbstractExtendedPanel implements java.io.Serializable{
   private static final long serialVersionUID = 711754256190346567L;
   
   private final Table table;
   
   private final Button btMenu = new Button("Отзывы"); 
   
   
   public ExtraPanelFeedbacks(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.setWidth(dim.iWidth + "px");
            window.setHeight(dim.iHeight + "px");
            window.setVisible(true);
            
            
            
            
            

            
            AdminMain app = (AdminMain) pnlParent.getApplication();
            CustomDescriptor dscr = TabFeedback.buildDescriptor(app);
            
            
            Constraint constraint = new Constraint();
            
            
            int ID = (Integer) table.getValue();
            
            Constraint.Criterion criterion = new Constraint.Criterion("CID",  ID);
            constraint.criteria.add(criterion);
            
//            MCafe mCafe = MongoService.get(MCafe.class, ID);
//            
//            criterion = new Constraint.Criterion(false, "cafe_type",  mCafe.type);
//            constraint.criteria.add(criterion);
//            
//            
//            criterion = new Constraint.Criterion(false, "cafe_name",  mCafe.name);
//            constraint.criteria.add(criterion);
            
            
            if(application.isAdmin){
            }
            else{
               constraint.criteria.add(new Constraint.Criterion(MUser.F_OWNER_ID, application.UID));
            }
            
            
            MenuElement menuElement = application.mapMenuClass.get(MFeedback.class);
            Set<EditMode> setEditMode = new HashSet<EditMode>();
            if(application.screenAdd.contains(menuElement.ID)){
               setEditMode.add(EditMode.ADD);
            }
            if(application.screenDelete.contains(menuElement.ID)){
               setEditMode.add(EditMode.DELETE);
            }
            if(application.screenEdit.contains(menuElement.ID)){
               setEditMode.add(EditMode.CHANGE);
            }
            
            DBListPanel<MFeedback> panel =
                     new DBListPanel<MFeedback>(app,
                                     MFeedback.class,
                                     dscr,
                                     Dimension.get80(app),
                                     setEditMode,
                                     constraint);
//            
            panel.addPanel(new ExtraPanelImageUploader(panel, (CustomImageField) dscr.mapDBFields.get("image")), false);
            
            panel.reload();
            
            AbstractLayout panelLayout = (AbstractLayout) panel.getContent();
            panelLayout.setMargin(false);
            window.setContent(panel);
            
            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);
      }
   }
}
