package com.dbs.dbsec.util;

public class ConvertToEnglish {
  /**
   * 한글을 초성, 중성, 종성으로 분리하여
   * 해당 키보드에 맞는 영어로 변환하는 클래스
   */

  final static char[] choSung = {
    //  ㄱ      ㄲ      ㄴ       ㄷ      ㄸ       ㄹ      ㅁ      ㅂ      ㅃ      ㅅ
    0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139, 0x3141, 0x3142, 0x3143, 0x3145,
    //  ㅆ      ㅇ       ㅈ      ㅉ      ㅊ       ㅋ      ㅌ      ㅍ      ㅎ
    0x3146, 0x3147, 0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e
  };

  final static String[] choSungEng = {
    "r", "R", "s", "e", "E", "f", "a", "q", "Q", "t", "T", "d", "w", "W", "c", "z", "x", "v"," g"
  };

  final static char[] jungSung = {
    //  ㅏ       ㅐ      ㅑ      ㅒ      ㅓ      ㅔ      ㅕ       ㅖ      ㅗ      ㅘ       ㅙ
    0x314f, 0x3150, 0x3151, 0x3152, 0x3153, 0x3154, 0x3155, 0x3156, 0x3157, 0x3158, 0x3159,
    //  ㅚ       ㅛ      ㅜ      ㅝ      ㅞ      ㅟ       ㅠ      ㅡ      ㅢ      ㅣ
    0x315a, 0x315b, 0x315c, 0x315d, 0x315e, 0x315f, 0x3160, 0x3161, 0x3162, 0x3163
  };

  final static String[] jungSungEng = {
    "k", "o", "i", "O", "j", "p", "u", "P", "h", "hk", "ho", "hl", "y", "n", "nj", "np", "nl", "b", "m", "ml", "l"
  };

  final static char[] jongSung = {
    //" "   ㄱ      ㄲ      ㄳ      ㄴ      ㄵ       ㄶ     ㄷ       ㄹ      ㄺ      ㄻ       ㄼ      ㄽ
    0, 0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, 0x3139, 0x313a, 0x313b, 0x313c, 0x313d,
    //    ㄾ      ㄿ      ㅀ     ㅁ       ㅂ       ㅄ      ㅅ      ㅆ       ㅇ     ㅈ        ㅊ      ㅋ     ㅌ      ㅍ       ㅎ
    0x313e, 0x313f, 0x3140, 0x3141, 0x3142, 0x3144, 0x3145, 0x3146, 0x3147, 0x3148, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e
  };

  final static String[] jongSungEng ={
    "", "r", "R", "rt", "s", "sw", "sg", "e", "f", "fr", "fa", "fq", "ft", "fx", "fv", "fg", "a", "q", "qt", "t", "T", "d", "w", "c", "z", "x", "v", "g"
  };

  public static String convertToJaso(String pwd) {
    int a, b, c;
    String result = "";

    for (int i = 0; i < pwd.length(); i++) {
      char ch = pwd.charAt(i);

      if (ch >= 0xAC00 && ch <= 0xD7A3) {
        c = ch - 0xAC00;
        a = c / (21 * 28);
        c = c % (21 * 28);
        b = c / 28;
        c = c % 28;

        result = result + choSung[a] + jungSung[b];

        if (c != 0) {
          result = result + jongSung[c];
        }
      } else {
        result = result + ch;
      }
    }
    return result;
  }

  public static String convertToEnglish(String pwd) {
    int a, b, c;
    String result = "";

    for (int i = 0; i < pwd.length(); i++) {
      char ch = pwd.charAt(i);

      if (ch >= 0xAC00 && ch <= 0xD7A3) {
        c = ch - 0xAC00;
        a = c / (21 * 28);
        c = c % (21 * 28);
        b = c / 28;
        c = c % 28;

        result = result + choSungEng[a] + jungSungEng[b];

        if (c != 0) {
          result = result + jongSungEng[c];
        }
      } else if (ch >= 0x3131 && ch <= 0x3163) {
        String temp = null;

        temp = findChoSung(ch);
        if (temp != null) {
          result = result + temp;
        } else {
          temp = findJungSung(ch);
          if (temp != null) {
            result = result + temp;
          } else {
            temp = findJongSung(ch);
            if (temp != null) {
              result = result + temp;
            } else {
              result = result + ch;
            }
          }
        }
      } else {
        result = result + ch;
      }
    }
    return result;
  }

//  public static String convertToSingleChar(String pwd) {
//    String result = "";
//    String temp = null;
//
//    for (int i = 0; i < pwd.length(); i++) {
//      char ch = pwd.charAt(i);
//
//      if (ch >= 0x3131 && ch <= 0x3163) {
//        temp = findChoSung(ch);
//        if (temp != null) {
//          result = result + temp;
//        } else {
//          temp = findJungSung(ch);
//          if (temp != null) {
//            result = result + temp;
//          } else {
//            temp = findJongSung(ch);
//            if (temp != null) {
//              result = result + temp;
//            } else {
//              result = result + ch;
//            }
//          }
//        }
//      } else {
//        result = result + ch;
//      }
//    }
//    return result;
//  }

  private static String findJongSung(char ch) {
    String result = null;

    for (int i = 0; i < jongSung.length; i++) {
      if (jongSung[i] == ch) {
        result = jongSungEng[i];
        break;
      }
    }
    return result;
  }

  private static String findJungSung(char ch) {
    String result = null;

    for (int i = 0; i < jungSung.length; i++) {
      if (jungSung[i] == ch) {
        result = jungSungEng[i];
        break;
      }
    }
    return result;
  }

  public static String findChoSung(char ch) {
    String result = null;

    for (int i = 0; i < choSung.length; i++) {
      if (choSung[i] == ch) {
        result = choSungEng[i];
        break;
      }
    }
    return result;
  }

}