package kz.cafe.admin.panels.model;

import java.util.*;
import kz.cafe.constants.*;

public class AbstractComboField extends CustomField implements java.io.Serializable{
   
   private static final long serialVersionUID = -6530651778025057732L;
   
   private final List<Element> listElements = new ArrayList<Element>();
   private final Map<Integer, Element> mapElementNumbers = new HashMap<Integer, Element>();
   private final Map<String, Element> mapElementText = new HashMap<String, Element>();
   
   
   public enum TYPE{TEXT, IMAGE}
   
   public TYPE type = TYPE.TEXT;
   
   public InterfaceConstants.LANGS lang;
   
   
   public void clear(){
      listElements.clear();
      mapElementNumbers.clear();
      mapElementText.clear();
   }
   
   
   
   public void add(Element element){
      if(mapElementNumbers.containsKey(element.id)){
         throw new Error("Double id=" + element.id);
      }
      
      listElements.add(element);
      mapElementNumbers.put(element.id, element);
      mapElementText.put(element.text, element);
      
      Collections.sort(listElements);
   }
   
   
   
   
   public List<Element> getElements(){
      return new ArrayList<Element>(listElements);
   }
   
   
   public Element get(int id){
      return mapElementNumbers.get(id);
   }
   public Element get(String text){
      return mapElementText.get(text);
   }
   

   
   
   
   
   public static class Element implements Comparable<Object>, java.io.Serializable{
      private static final long serialVersionUID = 2848022751799860900L;
      
      
      public final int id;
      public final String text;
      public final String image;

      
      public Element(int id, String text){
         this.id = id;
         this.text = text;
         this.image = null;
      }
      
      
      public Element(int id, String text, String image){
         this.id = id;
         this.text = text;
         this.image = image;
      }

      
      
      
      
      
      @Override
      public int hashCode(){
         final int prime = 31;
         int result = 1;
         result = prime * result + id;
         return result;
      }






      @Override
      public boolean equals(Object obj){
         if(this == obj)
            return true;
         if(obj == null)
            return false;
         if(getClass() != obj.getClass())
            return false;
         Element other = (Element) obj;
         if(id != other.id)
            return false;
         return true;
      }






      @Override
      public int compareTo(Object obj){
         return id - ((Element) obj).id;
      }
   }
}
