package kz.cafe.admin;

import java.util.*;
import kz.cafe.admin.constants.AccessConstants.EditMode;
import kz.cafe.admin.hooks.*;
import kz.cafe.admin.panel_extensions.*;
import kz.cafe.admin.panels.*;
import kz.cafe.admin.panels.model.*;
import kz.cafe.constants.*;
import kz.cafe.db.*;
import kz.cafe.db.model.*;
import com.vaadin.data.*;
import com.vaadin.ui.*;



public class TabOrders extends AbstractTab{
   private static final long serialVersionUID = -8572183427270759965L;




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

   @Override
   public DBListPanel<MOrder> 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 = "CID";
      ccf.isEditable = true;
      ccf.style.width = 110;
      ccf.isMust = true;
      ccf.search.row = 1;
      dscr.addField(ccf);
      
      
      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.visible = true;
      ccf.name = "Акция";
      ccf.db = "AID";
      ccf.isEditable = true;
      ccf.style.width = 150;
      ccf.isMust = false;
      dscr.addField(ccf);
      
      


      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);
      
      
      
      field = new CustomField();
      field.visible = true;
      field.name = "Цена";
      field.db = "price";
      field.isEditable = true;
      field.style.width = 80;
      field.isMust = false;
      field.search.mode = CustomField.Search.SEARCH_MODES.INTEGER;
      dscr.addField(field);
      
      
      
      
      
      ccf = new CustomFixedComboField();
      ccf.visible = true;
      ccf.name = "Город";
      ccf.db = "deliveryCity";
      ccf.isEditable = true;
      ccf.style.width = 100;
      ccf.isMust = true;
      ccf.search.row = 0;
      dscr.addField(ccf);
      

      
      field = new CustomField();
      field.visible = true;
      field.name = "Улица";
      field.db = "deliveryStreet";
      field.isEditable = true;
      field.style.width = 200;
      field.isMust = false;
      field.search.mode = CustomField.Search.SEARCH_MODES.LOCALIZED_STRING;
      field.style.alignment = CustomField.Alignment.LEFT;
      dscr.addField(field);
      
      
      
      
      field = new CustomField();
      field.visible = true;
      field.name = "Дом";
      field.db = "deliveryBuilding";
      field.isEditable = true;
      field.style.width = 50;
      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 = "deliveryApartment";
      field.isEditable = true;
      field.style.width = 50;
      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 = "deliveryPhone";
      field.isEditable = true;
      field.style.width = 150;
      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 CustomFixedComboField ccfStatus = new CustomFixedComboField();
      ccfStatus.type = CustomFixedComboField.TYPE.TEXT;
      ccfStatus.visible = true;
      ccfStatus.name = "Статус";
      ccfStatus.db = "status";
      ccfStatus.isEditable = true;
      ccfStatus.style.width = 100;
      ccfStatus.add(new CustomFixedComboField.Element(OrderConstants.STATUS.NEW.ordinal(),         "Новый"));
      ccfStatus.add(new CustomFixedComboField.Element(OrderConstants.STATUS.IN_PROGRESS.ordinal(), "В работе"));
      ccfStatus.add(new CustomFixedComboField.Element(OrderConstants.STATUS.PAID.ordinal(),        "Оплачен"));
      ccfStatus.add(new CustomFixedComboField.Element(OrderConstants.STATUS.CANCELLED.ordinal(),   "Отменен"));
      ccfStatus.isMust = true;
      ccfStatus.search.row = 1;
      dscr.addField(ccfStatus);
      
      
      
      
      
      
      
      
//      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 = 1;
//      dscr.addField(fieldConfirmed);
//      
//
//      
//      
//      
//      final CustomField fieldClosed = new CustomField();
//      fieldClosed.visible = true;
//      fieldClosed.name = "Закрыт";
//      fieldClosed.db = "closed";
//      fieldClosed.isEditable = true;
//      fieldClosed.style.width = 80;
//      fieldClosed.isSortable = true;
//      fieldClosed.search.mode = CustomField.Search.SEARCH_MODES.BOOLEAN_ONLY_TRUE; 
//      fieldClosed.search.row = 1;
//      dscr.addField(fieldClosed);
      
      
      Constraint constraint = new Constraint();
      
      if(application.isAdmin){
      }
      else{
         constraint.criteria.add(new Constraint.Criterion(MUser.F_OWNER_ID, application.UID));
      }

      
      final DBListPanel<MOrder> panel = new DBListPanel<MOrder>(application,
                                                                MOrder.class,
                                                                dscr,
                                                                Dimension.getFull(application),
                                                                setMode,
                                                                constraint,
                                                                new Order(Order.CREATER,
                                                                          ccfStatus.db,
                                                                          ccfStatus.name));
      
      
      panel.addPanel(new ExtraPanelOrderItems(application, panel), false);
      
      
      
      
      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);
               
               int iStatus;
               try{
                  String sStatus = "" +  item.getItemProperty(ccfStatus.name).getValue();
                  iStatus = ccfStatus.get(sStatus).id;
               }
               catch (Throwable e){
                  iStatus = 0;
               }
               
               OrderConstants.STATUS status = OrderConstants.STATUS.values()[iStatus];
               
               switch (status) {
                  case NEW:{
                     return "highlight-stressed";
                  }
                  case PAID:{
                     return "highlight-gray";
                  }
                  case CANCELLED:{
                     return "highlight-gray";
                  }
               }
               
               
               
//               boolean isClosed = (Boolean) item.getItemProperty(ccfStatus.name).getValue();
//               if(isClosed){
//                  return "highlight-gray";
//               }
//               
//               
//               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;
            }
         }
      });      
      
      
      
      panel.hookDelete(new OrderHook());
      panel.hookAdd(new OrderHook());
      panel.hookEdit(new OrderHook());
      panel.hookUndelete(new OrderHook());
      
      
      
		return panel;
	}




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

