package com.ssj.kmt.utils;

import org.apache.commons.lang3.StringUtils;

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

public class HTMLSpirit {
	
	
	 public static String filterHTMLSeparate(String htmlStr){ 
		 if(StringUtils.isEmpty(htmlStr)){
			 return "";
		 }
		 htmlStr=htmlStr.trim();
		 
		 //删除不合法的img
		 List<String[]> imgs=filterImgSrcList(htmlStr);
		 for (String[] srcs : imgs) {
			 if(srcs[0].contains("default/images/spacer.gif")){
				 htmlStr=htmlStr.replace(srcs[1], "");
			 }
		}
		 
		 
		//删除以多个<br/>结局的数据
		while (true) {
			 if(htmlStr.endsWith("<br/>")){
				 htmlStr=filterHTMLEndBr(htmlStr);
			 }else{
				 break;
			 }
		 }
		
		//删除以多个<p><br/></p>结局的数据
		while (true) {
			 if(htmlStr.endsWith("<p><br/></p>")){
				 htmlStr=filterHTMLEndP(htmlStr);
			 }else{
				 break;
			 }
		 }
		
		
	
		 
		 //替换不规则的text-decoration:underline;为____
		 htmlStr=filterUnderlineStr(htmlStr);
		
		 //过滤不规则的横线
		 htmlStr=filterStrleStr(htmlStr);
		 
		 //定义固定的横线
		 htmlStr=geLineStr(htmlStr);
		 
		 //过滤自定义标识
		 return htmlStr.replaceAll("#N", "<br/>").replaceAll("#&", "&nbsp;&nbsp;&nbsp;").replaceAll("<p></p>","")
				 .replaceAll("<p>&nbsp;&nbsp;</p>", "").replaceAll("<p>&nbsp;</p>", "").replaceAll("<p>  </p>", "")
				 .replaceAll("margin: 0px;","").replaceAll("<img>", "").replaceAll("<img/>", "");
	 }
	 
	 
	 //过滤不规则的横线
	 private static String filterStrleStr(String htmlStr){    
		 Pattern pattern = Pattern.compile("<span style=\"font-size: 16px;\">_*</span>");
		 Matcher matcher = pattern.matcher(htmlStr);
		 while(matcher.find()){
			 String replacement=matcher.group();
			 String newReplacement=replacement.replaceAll("<span style=\"font-size: 16px;\">", "").replaceAll("</span>", "");
			 htmlStr=htmlStr.replaceAll(replacement, newReplacement);
		 }
		 return htmlStr;
	 }
	 
	 
	//	替换不规则的text-decoration:underline;为____
	 private static String filterUnderlineStr(String htmlStr){
		 Pattern pattern = Pattern.compile("<span style=\"text-decoration:underline;\">(&nbsp;|&nbsp; )*</span>");
		 Matcher matcher = pattern.matcher(htmlStr);
		 while(matcher.find()){
			 String replacement=matcher.group();
			 String newReplacement=replacement.replaceAll("<span style=\"text-decoration:underline;\">", "").replaceAll("</span>", "").replaceAll("&nbsp;", "_").replaceAll(" ", "");
			 htmlStr=htmlStr.replaceAll(replacement, newReplacement);
		 }
		 return htmlStr;
	 }
	 
	
	 
	 //定义这么长的横线，超出的不要了
	 private static String lineStr="_____________________________________________________________________";
	 private static String splitStr="#1#1#";
	 private static String geLineStr(String htmlStr){
		 StringBuffer bb=new StringBuffer();
		 if(htmlStr.contains(lineStr)){
			htmlStr=htmlStr.replaceAll(lineStr, lineStr+splitStr);
			String[] htmls=htmlStr.split(lineStr);
			for (int i = 0; i < htmls.length; i++) {
				String html=htmls[i]; 
				String newHtml=""; 
				if(i>=1){//从下标1开始
					if(html.startsWith("_")){
						for (int j = 0; j < html.length(); j++) {
							char lstr=html.charAt(j);
							if(lstr!='_'){
								break;
							}
							newHtml=html.substring(j+1,html.length());
						}
						html=newHtml;
					}else if(html.startsWith(splitStr+"_")){
						for (int j = 5; j < html.length(); j++) {
							char lstr=html.charAt(j);
							if(lstr!='_'){
								break;
							} 
							newHtml=splitStr+html.substring(j+1,html.length());
						}
						html=newHtml;
					}
				}
				bb.append(html.replaceAll(splitStr, lineStr));
			}
			htmlStr=bb.toString();
		 }
		 return htmlStr;
	 }
	 
	 
	 private static String filterHTMLEndP(String htmlStr){ 
		 htmlStr=htmlStr.substring(0,htmlStr.lastIndexOf("<p><br/></p>"));
		 return htmlStr;
	 }
	 
	 private static String filterHTMLEndBr(String htmlStr){ 
		 htmlStr=htmlStr.substring(0,htmlStr.lastIndexOf("<br/>"));
		 return htmlStr;
	 }
	 
	
	 public static String delHTMLTag(String htmlStr){ 
		    if(StringUtils.isEmpty(htmlStr)){
		    	return "";
		    }
	        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("<w:p></w:p>"); //过滤script标签 
	         
	        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
	        Matcher m_style=p_style.matcher(htmlStr); 
	        htmlStr=m_style.replaceAll("<w:p></w:p>"); //过滤style标签 
	         
	        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
	        Matcher m_html=p_html.matcher(htmlStr); 
	        htmlStr=m_html.replaceAll("<w:p></w:p>"); //过滤html标签 

	        return htmlStr.trim().replaceAll("&nbsp;"," ").replaceAll("	", "  "); //返回文本字符串 
	    } 
	 
	 public static String delHTMLTagAll(String htmlStr){ 
		 	if(StringUtils.isEmpty(htmlStr)){
		 		return "";
		 	}
	        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(""); //过滤script标签 
	         
	        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().replace("<div>", "").replace("<p>", "").replaceAll("&nbsp;", "").replace("</div>", "").replace("</p>", "").replace("<br>", ""); //返回文本字符串 
	    } 
	 
	 
	 public static String getOtype(String type){
			if("1".equals(type)){
	    		return "单选项";
	    	}else if("2".equals(type)){
	    		return "多选项";
	    	}else if("3".equals(type)){
	    		return "填空题";
	    	}else if("4".equals(type)){
	    		return "简答题";
	    	}else if("5".equals(type)){
	    		return "解答题";
	    	}else if("6".equals(type)){
	    		return "计算题";
	    	}else if("7".equals(type)){
	    		return "作文题";
	    	}else if("8".equals(type)){
	    		return "判断题";
	    	}else if("9".equals(type)){
	    		return "连线题";
	    	}else if("10".equals(type)){
	    		return "翻译题";
	    	}else if("11".equals(type)){
	    		return "匹配题";
	    	}else if("12".equals(type)){
	    		return "阅读题";
	    	}else if("13".equals(type)){
	    		return "语音题";
	    	}else if("14".equals(type)){
	    		return "操作题";
	    	}else if("15".equals(type)){
	    		return "应用题";
	    	}else if("16".equals(type)){
	    		return "课外阅读题";
	    	}else if("17".equals(type)){
	    		return "解决问题";
	    	}else if("18".equals(type)){
	    		return "看图列式";
	    	}else if("19".equals(type)){
	    		return "情景交际";
	    	}else{
	    		return "";
	    	}
		}
	 
	 
	 public static List<String[]> filterImgSrcList(String content){
		 List<String[]> list = new ArrayList<String[]>();
		 //目前img标签标示有3种表达式
		 //<img alt="" src="1.jpg"/> <img alt="" src="1.jpg"></img> <img alt="" src="1.jpg">
		 //开始匹配content中的<img />标签
		 Pattern p_img = Pattern.compile("<(img|IMG)(.*?)(/>|></img>|>)");
		 Matcher m_img = p_img.matcher(content);
		 boolean result_img = m_img.find();
		 if (result_img) {
			 while (result_img) {
				 //获取到匹配的<img />标签中的内容
				 String str_img = m_img.group(2);
				 //开始匹配<img />标签中的src
				 Pattern p_src = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
				 Matcher m_src = p_src.matcher(str_img);
				 if (m_src.find()) {
					 String str_src = m_src.group(3);
					 list.add(new String[]{str_src,str_img});
				 }
				 //匹配content中是否存在下一个<img />标签，有则继续以上步骤匹配<img />标签中的src
				 result_img = m_img.find();
			 }
		 }
		 return list;
	 }
	 
	

}
