package com.storlead.framework.common.util;

import org.apache.commons.lang3.StringUtils;

import java.beans.PropertyEditorSupport;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * 类描述：时间操作定义类
 *
 * @Author:
 * @Date:2012-12-8 12:15:03
 * @Version 1.0
 */
public class HtmlUtils extends PropertyEditorSupport {


	/**
	 * content
	 *
	 */
//	public static String userContent(String content) {
//		// <p>段落替换为换行
//		content = content.replaceAll("<p .*?>", "\r\n");
//// <br><br/>替换为换行
//		content = content.replaceAll("<br\\s*/?>", "\r\n");
//// 去掉其它的<>之间的东西
//		content = content.replaceAll("\\<.*?>", "");
//		content = content.replaceAll("&nbsp;", " ");
//// 还原HTML
//// content = HTMLDecoder.decode(content);
//		return content;
//	}


	 /***
	  * @Description: 不处理HTML标签
	  * @Param:
	  * @return:
	  * @Author: YPZ
	  * @Date: 2022/11/29 14:32
	  */
	 public static String userContent(Object var) {
		 if (Objects.isNull(var)){
			 return "";
		 }
	 	 String inputString = var.toString();
		 if (StringUtils.isNumeric(inputString)) {
			 return inputString;
		 }
		 if(StringUtils.isBlank(inputString)){
			 return inputString;
		 }
		 if (inputString.indexOf("http") >= 0 || inputString.indexOf("<img") >= 0) {
			 return inputString;
		 }
		 return clearContent(inputString);
	 }
	/**
	 * 去除inputString中的HTML/CSS/JS标签
	 */
	public static String clearContent(String inputString) {
		if(StringUtils.isBlank(inputString)){
			return inputString;
		}

		String htmlStr = inputString;
		String textStr = "";
		Pattern p_script;
		Matcher m_script;
		Pattern p_style;
		Matcher m_style;
		Pattern p_html;
		Matcher m_html;
		Pattern p_html1;
		Matcher m_html1;

		try {
			String regEx_script = "<[//s]*?script[^>]*?>[//s//S]*?<[//s]*?///[//s]*?script[//s]*?>";
			String regEx_style = "<[//s]*?style[^>]*?>[//s//S]*?<[//s]*?///[//s]*?style[//s]*?>";
			String regEx_html = "<[^>]+>";
			String regEx_html1 = "<[^>]+";

			p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);

			m_script = p_script.matcher(htmlStr);
			htmlStr = m_script.replaceAll("");

			p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);

			m_style = p_style.matcher(htmlStr);

			htmlStr = m_style.replaceAll("");

			p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);

			m_html = p_html.matcher(htmlStr);

			htmlStr = m_html.replaceAll("");

			p_html1 = Pattern.compile(regEx_html1, Pattern.CASE_INSENSITIVE);

			m_html1 = p_html1.matcher(htmlStr);

			htmlStr = m_html1.replaceAll("");

			htmlStr = htmlStr.replaceAll("&nbsp;", " ");
			textStr = htmlStr;

		} catch (Exception e) {
			System.err.println("Html2Text: " + e.getMessage());
		}
		return textStr;
	}



	/**
	 * @Description:  统计有效字符长度
	 * @Param:
	 * @return:
	 * @Author: YPZ
	 * @Date: 2022-7-4
	 */

	public static int userContentSize(String content) {
		// <p>段落替换为换行
		content = content.replaceAll("<p .*?>", "");
// <br><br/>替换为换行
		content = content.replaceAll("<br\\s*/?>", "");
// 去掉其它的<>之间的东西
		content = content.replaceAll("\\<.*?>", "");
		content = content.replaceAll("&nbsp;", "");
// 还原HTML
// content = HTMLDecoder.decode(content);
		return content.length();
	}

	public static void main(String[] args) {
//		String content = "<p>一、SP21014.01,&nbsp; 网表与客户光纤那部分代码合成编译后测试仍有问题，时钟域太多，得去掉一部分；</p><p>二、SP22001.00，DDR3多通道读功能还有问题；</p><p>三、SP19021.01,&nbsp; 30000个文件情况下列表性能测试；</p>";
		String content = "<p><span style=\"color: rgb(96, 98, 102); background-color: rgb(245, 247, 250); font-size: 13px;\">年度目标3</span></p>";
		System.out.println(userContent(content));
		System.out.println(userContent(content).length());
		System.out.println(userContentSize(content));
	}

}
