package com.palacesun.masking.tools.sm4fpe;

import java.util.ArrayList;
import java.util.HashMap;

public class SM4FPEEntity {
   private int type;
   private ArrayList list;
   private HashMap charsMap;
   private int length;
   private SM4FPEBase.MatchType matchType;

   public SM4FPEEntity(int type, ArrayList list, HashMap charsMap) throws RuntimeException {
      this.type = type;
      this.list = list;
      this.charsMap = charsMap;
      this.matchType = SM4FPEBase.MatchType.LOOP;
      if (list.size() > 0 && list.size() > 0 && list.get(0) instanceof String) {
         this.length = ((String)list.get(0)).length();
      } else {
         this.length = 1;
      }

      printDuplicateData(list);
      if (this.list.size() != this.charsMap.size()) {
         throw new RuntimeException("array size not equals char map!");
      }
   }

   public static void printDuplicateData(ArrayList list) {
      for(int i = 0; i < list.size(); ++i) {
         for(int j = i + 1; j < list.size(); ++j) {
            if (list.get(i).equals(list.get(j))) {
               System.out.println(list.get(i));
            }
         }
      }

   }

   public SM4FPEEntity(int type, ArrayList list, HashMap charsMap, SM4FPEBase.MatchType matchType) throws RuntimeException {
      this(type, list, charsMap);
      this.matchType = matchType;
   }

   public Integer mapGet(String a, int index) {
      if (this.length > 1) {
         return a.length() >= index + this.length ? (Integer)this.charsMap.get(a.substring(index, index + this.length)) : null;
      } else {
         return (Integer)this.charsMap.get(a.charAt(index));
      }
   }

   public int getType() {
      return this.type;
   }

   public void setType(int type) {
      this.type = type;
   }

   public ArrayList getList() {
      return this.list;
   }

   public void setList(ArrayList list) {
      this.list = list;
   }

   public HashMap getCharsMap() {
      return this.charsMap;
   }

   public void setCharsMap(HashMap charsMap) {
      this.charsMap = charsMap;
   }

   public int getLength() {
      return this.length;
   }

   public void setLength(int length) {
      this.length = length;
   }

   public SM4FPEBase.MatchType getMatchType() {
      return this.matchType;
   }

   public void setMatchType(SM4FPEBase.MatchType matchType) {
      this.matchType = matchType;
   }
}
