package com.jsc.dsp.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsc.dsp.model.WordBank;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

    private static final Logger logger = Logger.getLogger("com.jsc.dsp.utils.StringUtils");

    public static String Html2Text(String inputString) {
        String htmlStr = inputString; // 含html标签的字符串
        String textStr = "";
        Pattern p_html;
        Matcher m_html;
        try {
            String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); // 过滤html标签
            textStr = htmlStr;
        } catch (Exception e) {
            System.err.println("Html2Text: " + e.getMessage());
        }
        return textStr;// 返回文本字符串
    }

    public static String DateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static Date StringToDate(String dateString) {
        String pattern = "yyyy-MM-dd HH:mm:ss";

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String TimestampToStringDate(Long timestamp) {
        return DateToString(new Date(timestamp));
    }

    public static boolean checkContentWithFilterWords(String content, List<String> exWords, List<String> inWords) {
        boolean checkPass = true;
        if (inWords != null && inWords.size() > 0) {
            checkPass = false;
            for (String word : inWords) {
                if (word != null && word.length() > 0 && content.contains(word)) {
                    checkPass = true;
                    break;
                }
            }
        }
        if (exWords != null && exWords.size() > 0) {
            for (String word : exWords) {
                if (word != null && word.length() > 0 && content.contains(word)) {
                    logger.warning("Find filter word '" + word + "'");
                    checkPass = false;
                    break;
                }
            }
        }
        return checkPass;
    }

    public static Map<String, List<String>> initFilterMap(String queryAPI) {
        Map<String, List<String>> resultMap = new HashMap<>();
        JSONObject dataObject = new JSONObject();
        dataObject.put("category", Arrays.asList("0", "1", "2", "3", "4", "5"));
        String response = HttpUtils.post(queryAPI, dataObject);
        JSONObject responseObject = JSON.parseObject(response);
        JSONArray content = responseObject.getJSONArray("content");
        for (Object object : content) {
            WordBank wordRecord = JSONObject.parseObject(object.toString(), WordBank.class);
            String cate = wordRecord.getCategory();
            String word = wordRecord.getWord();
            if (!resultMap.containsKey(cate)) {
                resultMap.put(cate, new ArrayList<>());
            }
            List<String> wordList = resultMap.get(cate);
            wordList.add(word);
            resultMap.put(cate, wordList);
        }
        return resultMap;
    }

    public static List<String> getFilterWordList(String customWords, Map<String, List<String>> cateMap, JSONArray cateList) {
        List<String> wordList = new ArrayList<>();
        if (customWords != null && customWords.length() > 0) {
            for (String c : customWords.split(";")) {
                if (c.length() > 0) {
                    wordList.add(c);
                }
            }
        }
        if (cateMap != null) {
            for (Object cate : cateList) {
                List<String> cateWords = cateMap.get(cate.toString());
                wordList.addAll(cateWords);
            }
        }
        return wordList;
    }

    public static void main(String[] args) {
        initFilterMap("http://39.98.151.140:28081/api/open/wordBank/queryAll");
    }

}
