package com.bonus.canteen.core.utils;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PinyinUtil {
   private static final Logger log = LoggerFactory.getLogger(PinyinUtil.class);

   public static String getFirstLetterFromChinese(String chinese) {
      chinese = delSignBlank(chinese);
      StringBuilder firstLetter = new StringBuilder();
      char[] chars = chinese.toCharArray();
      HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
      format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
      format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
      char[] var4 = chars;
      int var5 = chars.length;

      for(int var6 = 0; var6 < var5; ++var6) {
         char aChar = var4[var6];
         if (aChar > 128) {
            try {
               firstLetter.append(PinyinHelper.toHanyuPinyinStringArray(aChar, format)[0].charAt(0));
            } catch (BadHanyuPinyinOutputFormatCombination var9) {
               log.error("getFirstLetterFromChinese error", var9);
            }
         } else {
            firstLetter.append(aChar);
         }
      }

      return firstLetter.toString();
   }

   public static String convertChineseToPinyin(String chinese) {
      chinese = delSignBlank(chinese);
      StringBuilder pinyin = new StringBuilder();
      char[] chars = chinese.toCharArray();
      HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
      format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
      format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
      char[] var4 = chars;
      int var5 = chars.length;

      for(int var6 = 0; var6 < var5; ++var6) {
         char aChar = var4[var6];
         if (aChar > 128) {
            try {
               pinyin.append(PinyinHelper.toHanyuPinyinStringArray(aChar, format)[0]);
            } catch (BadHanyuPinyinOutputFormatCombination var9) {
               log.error("convertChineseToPinyin error", var9);
            }
         } else {
            pinyin.append(aChar);
         }
      }

      return pinyin.toString();
   }

   public static String convertChineseToPinyinWithBlank(String chinese) {
      chinese = delSign(chinese);
      StringBuilder pinyin = new StringBuilder();
      char[] chars = chinese.toCharArray();
      HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
      format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
      format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
      char[] var4 = chars;
      int var5 = chars.length;

      for(int var6 = 0; var6 < var5; ++var6) {
         char aChar = var4[var6];
         if (aChar > 128) {
            try {
               if (!CharSequenceUtil.endWith(pinyin, " ")) {
                  pinyin.append(" ");
               }

               String[] hanyuPinyinStringArray = PinyinHelper.toHanyuPinyinStringArray(aChar, format);
               String convertedChars = (String)ArrayUtil.get(hanyuPinyinStringArray, 0);
               if (CharSequenceUtil.isNotBlank(convertedChars)) {
                  pinyin.append(convertedChars);
               }

               if (pinyin.length() > 0 && !CharSequenceUtil.endWith(pinyin, " ")) {
                  pinyin.append(" ");
               }
            } catch (BadHanyuPinyinOutputFormatCombination var10) {
               log.error("convertChineseToPinyinWithBlank error", var10);
            }
         } else if (!CharUtil.isBlankChar(aChar) || !CharSequenceUtil.endWith(pinyin, " ")) {
            pinyin.append(aChar);
         }
      }

      return CharSequenceUtil.trim(pinyin.toString());
   }

   public static String filterChinese(String str) {
      String regEx = "[。|？！，、；：“”‘’（）《》〈〉【】『』「」﹃﹄〔〕…—～﹏￥－＿-]?";
      Pattern p = Pattern.compile(regEx);
      Matcher matcher = p.matcher(str);
      return matcher.replaceAll("").trim();
   }

   public static String delSignBlank(String str) {
      str = str.replaceAll("(?i)[^a-zA-Z0-9一-龥]", "");
      str = str.replace('-', ' ').replace(':', ' ').replaceAll(" ", "");
      return str;
   }

   public static String delSign(String str) {
      str = str.replaceAll("(?i)[^a-zA-Z0-9. 一-龥]", " ");
      str = str.replace("-", " ").replace(":", " ").replace("  ", " ");
      return str;
   }
}
