package kz.cafe.admin.panels.model;

import java.util.*;

public final class CustomDescriptor implements java.io.Serializable{
   
   private static final long serialVersionUID = -5815322192169028956L;
   
   
   
   private final List<CustomField> _listFields = new ArrayList<CustomField>();
   private final Map<String, CustomField> _mapDBFields = new HashMap<String, CustomField>();  
   private final Map<String, CustomField> _mapNames = new HashMap<String, CustomField>();  
   
   public final List<CustomField> listFields = Collections.unmodifiableList(_listFields);
   public final Map<String, CustomField> mapDBFields = Collections.unmodifiableMap(_mapDBFields);
   public final Map<String, CustomField> mapNames = Collections.unmodifiableMap(_mapNames);
   
   public final LinkedHashMap<Integer, List<CustomField>> mapSearchOrder = new LinkedHashMap<Integer, List<CustomField>>(); 
   
   
   public void addField(CustomField cstField){
      if(_mapDBFields.containsKey(cstField.db) || _mapNames.containsKey(cstField.name)){
         throw new Error("duplicate key '" + cstField.db + "' '" + cstField.name + "'");
      }
      
      _mapDBFields.put(cstField.db, cstField);
      _mapNames.put(cstField.name, cstField);
      _listFields.add(cstField);
      
      List<CustomField> list = mapSearchOrder.get(cstField.search.row);
      
      if(list == null){
         list = new ArrayList<CustomField>();
      }
      
      list.add(cstField);
      
      
      mapSearchOrder.put(cstField.search.row, list);
   }
   
   
   
   
   public void removeFieldByName(String sName){
      CustomField cstField = _mapNames.get(sName);
      
      _mapDBFields.remove(cstField.db);
      _mapNames.remove(cstField.name);
      _listFields.remove(cstField);
      mapDBFields.remove(cstField.db);
      mapNames.remove(cstField.name);
      listFields.remove(cstField);
   }

}
