package kz.cafe.admin;

import java.util.*;
import kz.cafe.admin.constants.AccessConstants.EditMode;
import kz.cafe.admin.hooks.*;
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 TabMessageSystem extends AbstractTab{
   private static final long serialVersionUID = -7440565419856388384L;




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




   @Override
   public DBListPanel<MMessageSystem> 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 = 50;
		field.isSortable = true;
		field.search.row = 0;
      dscr.addField(field);
   
      
      
      
      CustomFixedComboField ccf = new CustomFixedComboField();
      ccf.visible = true;
      ccf.name = "Автор";
      ccf.db = "fromUID";
      ccf.isEditable = true;
      ccf.style.width = 200;
      ccf.isMust = true;
      dscr.addField(ccf);
      
		
		
      field = new CustomField();
      field.visible = true;
      field.name = "Отзыв";
      field.db = "text";
      field.isEditable = true;
      field.style.width = 0;
      field.style.height = 100;
      field.isMust = false;
      field.search.mode = CustomField.Search.SEARCH_MODES.LOCALIZED_STRING;
      dscr.addField(field);
      
      


      

      
      
      
      
      
      CustomDateField 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 fieldRead = new CustomField();
      fieldRead.visible = true;
      fieldRead.name = "Прочитано";
      fieldRead.db = "read";
      fieldRead.isEditable = true;
      fieldRead.style.width = 80;
      fieldRead.isSortable = true;
      fieldRead.search.mode = CustomField.Search.SEARCH_MODES.BOOLEAN_ONLY_TRUE; 
      fieldRead.search.row = 1;
      dscr.addField(fieldRead);
      

      Constraint constraint = new Constraint();
      
      
      if(application.isAdmin){
      }
      else{
         constraint.criteria.add(new Constraint.Criterion(MUser.F_OWNER_ID, application.UID));
      }
      
      
      
      final DBListPanel<MMessageSystem> panel = new DBListPanel<MMessageSystem>(application,
                                                                                MMessageSystem.class,
                                                                                dscr,
                                                                                Dimension.getFull(application),
                                                                                setMode,
                                                                                constraint,
                                                                                new Order(Order.CREATER,
                                                                                          fieldRead.db,
                                                                                          fieldRead.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 = -6586615471330221828L;

         @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(fieldRead.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;
            }
         }
      });      
      
      
      panel.hookDelete(new FeedbackHook());
      panel.hookAdd(new FeedbackHook());
      panel.hookEdit(new FeedbackHook());
      panel.hookUndelete(new FeedbackHook());
		
		return panel;
	}
   
   







   @Override
   public DBSingle<MMessageSystem> build(int ID){
      
      return null;
   }
}

