package kz.cafe.admin;

import java.util.*;
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.data.*;
import com.vaadin.ui.*;

/**
 * Main application class.
 */
public class TabReservation extends AbstractTab{
   private static final long serialVersionUID = -3210275047163076036L;





   public TabReservation(AdminMain application, Set<EditMode> setMode){
      super(application, setMode);
   }
   
   
   

   @Override
   public DBListPanel<MReservation> build(){
		CustomDescriptor dscr = new CustomDescriptor();
		
		CustomField field = new CustomField();
		field.visible = true;
		field.search.mode = CustomField.Search.SEARCH_MODES.INTEGER;
		field.name = "ID";
      field.db = "ID";
      field.isEditable = false;
      field.style.width = 30;
		field.isSortable = true;
		field.search.row = 0;
      dscr.addField(field);
      
      
      
      
      CustomDateField dateField = new CustomDateField();
      dateField.visible = true;
      dateField.name = "Время";
      dateField.db = "date";
      dateField.isEditable = true;
      dateField.style.width = 120;
      dateField.isSortable = true;
      dateField.search.mode = CustomField.Search.SEARCH_MODES.DATE_RANGE;
      dateField.search.row = 1;
      dscr.addField(dateField);
      
      
      
      
      CustomFixedComboField crf = new CustomFixedComboField();
      crf.visible = true;
      crf.name = "Объект";
      crf.db = "CID";
      crf.isEditable = true;
      crf.style.width = 110;
      crf.isMust = true;
      crf.search.row = 1;
      dscr.addField(crf);
   
      
      
      
      CustomFixedComboField ccf = new CustomFixedComboField();
      ccf.visible = true;
      ccf.name = "Клиент";
      ccf.db = "UID";
      ccf.isEditable = true;
      ccf.style.width = 120;
      ccf.isMust = true;
      dscr.addField(ccf);

      
      
      
      field = new CustomField();
      field.visible = true;
      field.search.mode = CustomField.Search.SEARCH_MODES.INTEGER;
      field.name = "Мест";
      field.db = "sits";
      field.isEditable = true;
      field.style.width = 50;
      field.isSortable = true;
      field.search.row = 0;
      field.style.expandable = false;
      dscr.addField(field);
      

      
      
      
      field = new CustomField();
      field.visible = true;
      field.name = "Комментарий";
      field.db = "comment";
      field.isEditable = true;
      field.style.width = 0;
      field.isMust = false;
      field.search.mode = CustomField.Search.SEARCH_MODES.LOCALIZED_STRING;
      dscr.addField(field);
      
      
      

      if(application.isAdmin){
         ccf = new CustomFixedComboField();
         ccf.visible = true;
         ccf.name = "Владелец";
         ccf.db = MAbstractDataElement.F_OWNER_ID;
         ccf.isEditable = true;
         ccf.style.width = 120;
         ccf.isMust = true;
         //ccf.search.mode = CustomField.Search.SEARCH_MODES.NONE;
         ccf.search.row = 2;
         dscr.addField(ccf);
      }
      
      
      
      
      
      dateField = new CustomDateField();
      dateField.visible = true;
      dateField.name = "Создан";
      dateField.db = "created";
      dateField.isEditable = false;
      dateField.style.width = 120;
      dateField.isSortable = true;
      dateField.search.mode = CustomField.Search.SEARCH_MODES.DATE_RANGE; 
      dateField.search.row = 1;
      dscr.addField(dateField);
      

      
      
      
      
      dateField = new CustomDateField();
      dateField.visible = true;
      dateField.name = "Обновлен";
      dateField.db = "updated";
      dateField.isEditable = false;
      dateField.style.width = 120;
      dateField.isSortable = true;
      dateField.search.mode = CustomField.Search.SEARCH_MODES.DATE_RANGE;
      dateField.search.row = 1;
      dscr.addField(dateField);

      
      
      
      final CustomField fieldConfirmed = new CustomField();
      fieldConfirmed.visible = true;
      fieldConfirmed.name = "Обработан";
      fieldConfirmed.db = "confirmed";
      fieldConfirmed.isEditable = true;
      fieldConfirmed.style.width = 80;
      fieldConfirmed.isSortable = true;
      fieldConfirmed.search.mode = CustomField.Search.SEARCH_MODES.BOOLEAN_ONLY_TRUE; 
      fieldConfirmed.search.row = 2;
      dscr.addField(fieldConfirmed);
      
      

      
//      field = new CustomField();
//      field.visible = true;
//      field.name = "Опубликован";
//      field.db = "published";
//      field.isEditable = true;
//      field.style.width = 80;
//      field.isSortable = true;
//      field.search.mode = CustomField.Search.SEARCH_MODES.BOOLEAN_ONLY_TRUE; 
//      field.search.row = 2;
//      dscr.addField(field);
      

      Constraint constraint = new Constraint();
      
      
      if(application.isAdmin){
      }
      else{
         constraint.criteria.add(new Constraint.Criterion(MUser.F_OWNER_ID, application.UID));
      }
      
      
      
      final DBListPanel<MReservation> panel = new DBListPanel<MReservation>(application,
                                                MReservation.class,
                                                dscr,
                                                Dimension.getFull(application),
                                                setMode,
                                                constraint,
                                                new Order(Order.CREATER,
                                                         fieldConfirmed.db,
                                                         fieldConfirmed.name));
      //panel.addPanel(new ExtraPanelImageUploader(panel, fieldImage), false);
      //panelCourse.addPanel(new ExtraPanelImageViewer(panelCourse, fieldImage, 50), true);
      
      
      
      
      panel.table.setCellStyleGenerator(new Table.CellStyleGenerator(){
         private static final long serialVersionUID = -3776734826403357164L;

         @Override
         public String getStyle(Object itemId, Object propertyId){
            if(propertyId == null){
               // Styling for row

               
               Item item = panel.table.getItem(itemId);
               boolean isProcessed = (Boolean) item.getItemProperty(fieldConfirmed.name).getValue();
               
               if(isProcessed){
               }
               else{
                  return "highlight-stressed";
               }
//               if(firstName.toLowerCase().startsWith("g")){
//                  return "highlight-green";
//               }
//               else if(firstName.contains("o")){
//                  return "highlight-red";
//               }
//               else{
//                  return null;
//               }               
               
               
               if(panel.setBlockedRecords.contains(itemId)){
                  return "highlight-blocked";
               }
               else{
                  return null;
               }

            }
            else{
               // styling for column propertyId
               return null;
            }
         }
      });      
      
		
		return panel;
	}



   

   @Override
   public DBSingle build(int ID){
      // TODO Auto-generated method stub
      return null;
   }
}

