package kz.cafe.admin;

import java.util.*;
import com.vaadin.data.*;
import com.vaadin.ui.*;
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.constants.*;
import kz.cafe.db.*;
import kz.cafe.db.model.*;

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




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




   @Override
   public DBListPanel<MFeedback> 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 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.type = CustomFixedComboField.TYPE.IMAGE;
      ccf.visible = true;
      ccf.name = "Звезд";
      ccf.db = "rate";
      ccf.isEditable = true;
      ccf.style.width = 60;
      ccf.add(new CustomFixedComboField.Element(0, "нет", "<img src=\"" + ServerConstants.getImageURL() + "spacer.gif\"/>"));
      ccf.add(new CustomFixedComboField.Element(1, "1", "<img src=\"" + ServerConstants.getImageURL() + "star1.png\"/>"));
      ccf.add(new CustomFixedComboField.Element(2, "2", "<img src=\"" + ServerConstants.getImageURL() + "star2.png\"/>"));
      ccf.add(new CustomFixedComboField.Element(3, "3", "<img src=\"" + ServerConstants.getImageURL() + "star3.png\"/>"));
      ccf.add(new CustomFixedComboField.Element(4, "4", "<img src=\"" + ServerConstants.getImageURL() + "star4.png\"/>"));
      ccf.add(new CustomFixedComboField.Element(5, "5", "<img src=\"" + ServerConstants.getImageURL() + "star5.png\"/>"));
      ccf.isMust = false;
      dscr.addField(ccf);
      

      
      field = new CustomField();
      field.visible = true;
      field.name = "Заголовок";
      field.db = "title";
      field.isEditable = true;
      field.style.width = 300;
      field.isMust = false;
      field.search.mode = CustomField.Search.SEARCH_MODES.LOCALIZED_STRING;
      dscr.addField(field);
      
		
		
      field = new CustomField();
      field.visible = true;
      field.name = "Отзыв";
      field.db = "text";
      field.isEditable = true;
      field.style.width = 0;
      field.isMust = false;
      field.search.mode = CustomField.Search.SEARCH_MODES.LOCALIZED_STRING;
      dscr.addField(field);
      
      


      
      ccf = new CustomFixedComboField();
      ccf.visible = true;
      ccf.name = "Автор";
      ccf.db = "UID";
      ccf.isEditable = true;
      ccf.style.width = 120;
      ccf.isMust = true;
      dscr.addField(ccf);

      
      
      
      
      
      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 fieldPublished = new CustomField();
      fieldPublished.visible = true;
      fieldPublished.name = "Опубликован";
      fieldPublished.db = "published";
      fieldPublished.isEditable = true;
      fieldPublished.style.width = 80;
      fieldPublished.isSortable = true;
      fieldPublished.search.mode = CustomField.Search.SEARCH_MODES.BOOLEAN_ONLY_TRUE; 
      fieldPublished.search.row = 1;
      dscr.addField(fieldPublished);
      

      Constraint constraint = new Constraint();
      
      
      if(application.isAdmin){
      }
      else{
         constraint.criteria.add(new Constraint.Criterion(MUser.F_OWNER_ID, application.UID));
      }
      
      
      
      final DBListPanel<MFeedback> panel = new DBListPanel<MFeedback>(application,
                                                MFeedback.class,
                                                dscr,
                                                Dimension.getFull(application),
                                                setMode,
                                                constraint,
                                                new Order(Order.CREATER,
                                                          fieldPublished.db,
                                                          fieldPublished.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(fieldPublished.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;
	}
   
   
   
   
   
   
   
   
   
   
   
   
   
   public static CustomDescriptor buildDescriptor(AdminMain application){     
      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 = "UID";
      ccf.isEditable = true;
      ccf.style.width = 120;
      ccf.isMust = true;
      dscr.addField(ccf);

      
      
      
      
      

      
      ccf = new CustomFixedComboField();
      ccf.type = CustomFixedComboField.TYPE.IMAGE;
      ccf.visible = true;
      ccf.name = "Звезд";
      ccf.db = "rate";
      ccf.isEditable = true;
      ccf.style.width = 60;
      ccf.add(new CustomFixedComboField.Element(0, "1", "<img src=\"" + ServerConstants.getImageURL() + "star1.png\"/>")); 
      ccf.add(new CustomFixedComboField.Element(1, "2", "<img src=\"http://localhost:8080/kz_cafe_admin/VAADIN/themes/custom/star2.png\"/>")); 
      ccf.add(new CustomFixedComboField.Element(2, "3", "<img src=\"http://localhost:8080/kz_cafe_admin/VAADIN/themes/custom/star3.png\"/>")); 
      ccf.add(new CustomFixedComboField.Element(3, "4", "<img src=\"http://localhost:8080/kz_cafe_admin/VAADIN/themes/custom/star4.png\"/>")); 
      ccf.add(new CustomFixedComboField.Element(4, "5", "<img src=\"http://localhost:8080/kz_cafe_admin/VAADIN/themes/custom/star5.png\"/>")); 
      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.isMust = true;
      field.search.mode = CustomField.Search.SEARCH_MODES.LOCALIZED_STRING;
      dscr.addField(field);
      
      
      

      return dscr;
   }







   @Override
   public DBSingle<MFeedback> build(int ID){
      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 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.type = CustomFixedComboField.TYPE.IMAGE;
      ccf.visible = true;
      ccf.name = "Звезд";
      ccf.db = "rate";
      ccf.isEditable = true;
      ccf.style.width = 60;
      ccf.add(new CustomFixedComboField.Element(0, "нет", "<img src=\"" + ServerConstants.getImageURL() + "spacer.gif\"/>"));
      ccf.add(new CustomFixedComboField.Element(1, "1", "<img src=\"" + ServerConstants.getImageURL() + "star1.png\"/>"));
      ccf.add(new CustomFixedComboField.Element(2, "2", "<img src=\"" + ServerConstants.getImageURL() + "star2.png\"/>"));
      ccf.add(new CustomFixedComboField.Element(3, "3", "<img src=\"" + ServerConstants.getImageURL() + "star3.png\"/>"));
      ccf.add(new CustomFixedComboField.Element(4, "4", "<img src=\"" + ServerConstants.getImageURL() + "star4.png\"/>"));
      ccf.add(new CustomFixedComboField.Element(5, "5", "<img src=\"" + ServerConstants.getImageURL() + "star5.png\"/>"));
      ccf.isMust = false;
      dscr.addField(ccf);
      
      
      
      
      field = new CustomField();
      field.visible = true;
      field.name = "Отзыв";
      field.db = "text";
      field.isEditable = true;
      field.style.width = 0;
      field.isMust = false;
      field.search.mode = CustomField.Search.SEARCH_MODES.LOCALIZED_STRING;
      dscr.addField(field);
      
      


      
      ccf = new CustomFixedComboField();
      ccf.visible = true;
      ccf.name = "Автор";
      ccf.db = "UID";
      ccf.isEditable = true;
      ccf.style.width = 120;
      ccf.isMust = true;
      dscr.addField(ccf);

      
      
      
      
      
      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);
      

      
      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 = 1;
      dscr.addField(field);
      

      Constraint constraint = new Constraint();
      
      
      if(application.isAdmin){
      }
      else{
         constraint.criteria.add(new Constraint.Criterion(MUser.F_OWNER_ID, application.UID));
      }
      
      
      
      
      DBSingle<MFeedback> panel = new DBSingle<MFeedback>(application,
                                                          MFeedback.class,
                                                          ID,
                                                          dscr,
                                                          Dimension.get80(application),
                                                          setMode
                                                         );
      
      
      //panel.addPanel(new ExtraPanelImageUploader(panel, fieldImage), false);
      //panelCourse.addPanel(new ExtraPanelImageViewer(panelCourse, fieldImage, 50), true);
      
      
      return panel;
   }
}

