package kz.cafe.admin.panels;

import org.apache.commons.logging.*;
import kz.cafe.admin.*;
import kz.cafe.admin.panels.model.*;
import kz.cafe.constants.*;
import kz.cafe.db.*;
import kz.cafe.db.model.*;
import com.vaadin.ui.*;

public class AbstractDBPanel<T> extends AbstractPanel{
   private static final long serialVersionUID = 4693209044763912578L;

   
   private static Log log = LogFactory.getLog(MongoService.class);
   private static final String LOG = "[UI PNL] ";
   

   public volatile String MENU_TITLE;


   public Class<T> classDB;



   public NativeSelect CURRENT_LANG;





   public AbstractDBPanel(){
      // Langs
      CURRENT_LANG = new NativeSelect();
      for(InterfaceConstants.LANGS lang : InterfaceConstants.LANGS.values()){
         CURRENT_LANG.addItem(lang);
      }
      CURRENT_LANG.setNullSelectionAllowed(false);
      CURRENT_LANG.setWidth("50px");

      CURRENT_LANG.select(CURRENT_LANG.getItemIds().iterator().next());

      CURRENT_LANG.setImmediate(true);
   }




   protected java.lang.reflect.Field getField(String sName){
      java.lang.reflect.Field field;
      try{
         field = classDB.getField(sName);
      }
      catch (Throwable e){
         throw new Error("Field access name=" + sName + " failed.", e);
      }

      return field;
   }



   protected Object getFieldValue(java.lang.reflect.Field field, Object obj) {
      try{
         return field.get(obj);
      }
      catch (Exception e){
         throw new Error("Field access value=" + field + " failed.", e);
      }
   }



   @SuppressWarnings({ "rawtypes"})
   public void localizeCombos(AbstractComboField cdf, Class classForeign){
      log.trace(LOG + "Loading combo " + classForeign.getName());
      
      InterfaceConstants.LANGS lang = InterfaceConstants.LANGS.valueOf("" + CURRENT_LANG.getValue());

      if(cdf.lang == null || cdf.lang != lang || cdf.getElements().size() == 0) {
         cdf.clear();
         
         for(Object obj : Caching.getList(classForeign)){
            MAbstractDataElement mde = (MAbstractDataElement) obj;
   
            String sLocalizedText = mde.humanReadable(lang);
            cdf.add(new CustomFixedComboField.Element(mde.ID, sLocalizedText));
         }
         
         cdf.lang = lang;
      }
      
      log.trace(LOG + "gadina");
   }

   
   
   
   @SuppressWarnings({ "rawtypes", "unchecked"})
   public void localizeCombos(CustomDatabaseComboField cdf, Class classForeign){
//      log.trace(LOG + "suka");
      
      InterfaceConstants.LANGS lang = InterfaceConstants.LANGS.valueOf("" + CURRENT_LANG.getValue());

      if(cdf.lang == null || cdf.lang != CURRENT_LANG.getValue() || cdf.getElements().size() == 0) {
         cdf.clear();
         
         for(Object obj : MongoService.getList(classForeign, cdf.constraint)){
            MAbstractDataElement mde = (MAbstractDataElement) obj;
   
            String sLocalizedText = mde.humanReadable(lang);
            cdf.add(new CustomFixedComboField.Element(mde.ID, sLocalizedText));
         }
         
         cdf.lang = lang;
      }
      
//      log.trace(LOG + "gadina");
   }
   
   
   
   
   public static Object formatPrice(int iPrice){
      return new Float(iPrice) / 100;
   }
   public static Object formatPrice(Object price){
      return Float.parseFloat("" + price) / 100;
   }
}
