package com.ruoyi.biemo.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyObjects {

    public static Map<String,Integer> wordsMapping = new HashMap<>();
    public static Map<Integer,String> wordsMappingIndex = new HashMap<>();

    public static JSONArray labelCategories = new JSONArray();
    public static JSONArray connectionCategories = new JSONArray();


    static{
        wordsMapping.put("n",0);//名词
        wordsMapping.put("nr",1);// 人名
        wordsMapping.put("ns",2);// 地名
        wordsMapping.put("nt",3);// 机构团体
        wordsMapping.put("v",4);//动词
        wordsMapping.put("a",5);//形容词
        wordsMapping.put("d",6);//副词

        wordsMappingIndex.put(0,"n");//名词
        wordsMappingIndex.put(1,"nr");// 人名
        wordsMappingIndex.put(2,"ns");// 地名
        wordsMappingIndex.put(3,"nt");// 机构团体
        wordsMappingIndex.put(4,"v");//动词
        wordsMappingIndex.put(5,"a");//形容词
        wordsMappingIndex.put(6,"d");//副词

        JSONObject labelCategoriesMap1 = new JSONObject();
        labelCategoriesMap1.put("id",0);
        labelCategoriesMap1.put("text","名词");
        labelCategoriesMap1.put("color","#eac0a2");
        labelCategoriesMap1.put("border-color","#8c7361");
        labelCategories.add(labelCategoriesMap1);
        JSONObject labelCategoriesMap2 = new JSONObject();
        labelCategoriesMap2.put("id",1);
        labelCategoriesMap2.put("text","人名");
        labelCategoriesMap2.put("color","#eac0a2");
        labelCategoriesMap2.put("border-color","#8c7361");
        labelCategories.add(labelCategoriesMap2);
        JSONObject labelCategoriesMap3 = new JSONObject();
        labelCategoriesMap3.put("id",2);
        labelCategoriesMap3.put("text","地名");
        labelCategoriesMap3.put("color","#eac0a2");
        labelCategoriesMap3.put("border-color","#8c7361");
        labelCategories.add(labelCategoriesMap3);
        JSONObject labelCategoriesMap4 = new JSONObject();
        labelCategoriesMap4.put("id",3);
        labelCategoriesMap4.put("text","机构团体");
        labelCategoriesMap4.put("color","#eac0a2");
        labelCategoriesMap4.put("border-color","#8c7361");
        labelCategories.add(labelCategoriesMap4);
        JSONObject labelCategoriesMap5 = new JSONObject();
        labelCategoriesMap5.put("id",4);
        labelCategoriesMap5.put("text","动词");
        labelCategoriesMap5.put("color","#619dff");
        labelCategoriesMap5.put("border-color","#3c619d");
        labelCategories.add(labelCategoriesMap5);
        JSONObject labelCategoriesMap6 = new JSONObject();
        labelCategoriesMap6.put("id",5);
        labelCategoriesMap6.put("text","形容词");
        labelCategoriesMap6.put("color","#9d61ff");
        labelCategoriesMap6.put("border-color","#613C9D");
        labelCategories.add(labelCategoriesMap6);
        JSONObject labelCategoriesMap7 = new JSONObject();
        labelCategoriesMap7.put("id",6);
        labelCategoriesMap7.put("text","副词");
        labelCategoriesMap7.put("color","#ff9d61");
        labelCategoriesMap7.put("border-color","#995e3a");
        labelCategories.add(labelCategoriesMap7);


        JSONObject connectionCategoriesMap1 = new JSONObject();
        connectionCategoriesMap1.put("id",0);
        connectionCategoriesMap1.put("text","修饰");
        connectionCategories.add(connectionCategoriesMap1);
        JSONObject connectionCategoriesMap2 = new JSONObject();
        connectionCategoriesMap2.put("id",1);
        connectionCategoriesMap2.put("text","限定");
        connectionCategories.add(connectionCategoriesMap2);
        JSONObject connectionCategoriesMap3 = new JSONObject();
        connectionCategoriesMap3.put("id",2);
        connectionCategoriesMap3.put("text","是...的动作");
        connectionCategories.add(connectionCategoriesMap3);
    }

    /**
     * 判断对象是否为空，且对象的所有属性都为空
     * ps: boolean类型会有默认值false 判断结果不会为null 会影响判断结果
     *     序列化的默认值也会影响判断结果
     * @param object
     * @return
     */
    public static  boolean IsNull(Object object){
        Class clazz = object.getClass(); // 得到类对象
        Field fields[] = clazz.getDeclaredFields(); // 利用反射得到所有属性
        boolean flag = true; //定义标志flag
        /**
         * 循环遍历反射得到的属性数组，判断每个属性值是否为空
         */
        for(Field f : fields){
            f.setAccessible(true);//由于考虑到某些私有属性直接访问肯能访问不到，此属性设置为true确保可以访问到
            Object fieldValue = null;
            try {
                Type fieldType =f.getGenericType();//得到属性类型
                String fieldName = f.getName(); // 得到属性名
                if("serialVersionUID".equals(fieldName)){
                    continue;
                }
                fieldValue = f.get(object); //得到属性值

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if(fieldValue != null){  //只要有一个属性值不为null 就返回false 表示对象不为null
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static String delHTMLTag(String htmlStr){
        String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式

        Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
        Matcher m_script=p_script.matcher(htmlStr);
        htmlStr=m_script.replaceAll(""); //过滤

        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
        Matcher m_style=p_style.matcher(htmlStr);
        htmlStr=m_style.replaceAll(""); //过滤style标签

        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
        Matcher m_html=p_html.matcher(htmlStr);
        htmlStr=m_html.replaceAll(""); //过滤html标签

        return htmlStr.trim(); //返回文本字符串
    }
    public static String delSpace(String htmlStr){

        htmlStr = htmlStr.replaceAll("\t", "");
        htmlStr = htmlStr.replaceAll("\\u3000", "");
        htmlStr = htmlStr.replaceAll("(\\s{4})", "");
        htmlStr = htmlStr.replaceAll("\\s", "");
        htmlStr = StringUtils.deleteWhitespace(htmlStr);
        return htmlStr.trim(); //返回文本字符串
    }

   public static String getNo(long n) {
        if (n <= 9) {
            return "0000" + n;
        } else if (n > 9 && n <= 99) {
            return "000" + n;
        } else if (n > 99 && n <= 999) {
            return "00" + n;
        } else if (n > 999 && n <= 9999) {
            return "0" + n;
        } else {
            return n + "";
        }
    }

}
