package com.madeu.util;

import org.apache.commons.lang3.StringEscapeUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class ConvertStringUtil {

	public static String escapeXmlWeb(String content){
	//	return StringEscapeUtils.escapeXml(content.trim().replace("\n","\\n").replace("\r","\\r"));
		String newContent="";
		if(null==content||("").equals(content)){
		
		}
			else{
			newContent = StringEscapeUtils.escapeXml(content.trim()).replace("\r\n","<br>");
		}
		return newContent;
	}
	
	public static String escapeXmlApp(String content){
	//	return StringEscapeUtils.escapeXml(content.trim().replace("\\n","\n").replace("\\r","\r").replace("\r\n","<br>"));
		String newContent="";
		if(null==content||("").equals(content)){
			
		}
			else{
			newContent =StringEscapeUtils.escapeXml(content.trim().replace("\\n","\n").replace("\\r","\r"));
		}
		return newContent;
	}
	
	
	public static String unescapeXml(String content){
		String newContent="";
		if(null==content||("").equals(content)){
			
		}
			else{
			newContent = StringEscapeUtils.unescapeXml(content);
		}
		return newContent;
	}

	public static String ifNullToString(Object content){
		String newContent="";
		if(null==content||("").equals(content)||("null").equals(content)){
		
		}
			else{
			newContent = String.valueOf(content);
		}
		return newContent;
	}
	
	public static String escapeXmlNtsoftWeb(String content){
		String newContent="";
		if(null==content||("").equals(content)){
		
		}
			else{
			newContent = content.trim().replace("<","&#60;")
					.replace(">","&#62;").replace("\"","&#34;").replace("\r\n","<br>");
			//newContent = StringEscapeUtils.escapeXml(content.trim()).replace("\r\n","<br>").replace("<script>","&lt;script&gt;");
		}
		return newContent;
	}
		

	public static int ifNullToInteger(Object content){
		int newContent=0;
		if(null==content||("").equals(content)||("null").equals(content)){
		
		}
			else{
			newContent = Integer.parseInt(String.valueOf(content));
		}
		return newContent;
	}
	
	public static String numberComma(String value){
		DecimalFormat comma = new DecimalFormat("###,###,###,###");
		BigDecimal changeValue = new BigDecimal(value);
		return comma.format(changeValue);
	}
	
	public static String numberComma(Object value){
		DecimalFormat comma = new DecimalFormat("###,###,###,###");
		BigDecimal changeValue = new BigDecimal(String.valueOf(value));
		return comma.format(changeValue);
	}
	
	public static int byteLength(String value){
		int strLength=0;
		char tempChar[] = new char[value.length()];
		
		for(int i=0; i<tempChar.length;i++){
			tempChar[i] = value.charAt(i);
			
			if(tempChar[i]<128){
				strLength++;
			}
			else{
				strLength+=2;
			}
		}
		return strLength;
	}
	
	public static int byteLength(Object value){
		int strLength=0;
		char tempChar[] = new char[String.valueOf(value).length()];
		
		for(int i=0; i<tempChar.length;i++){
			tempChar[i] = String.valueOf(value).charAt(i);
			
			if(tempChar[i]<128){
				strLength++;
			}
			else{
				strLength+=2;
			}
		}
		return strLength;
	}
	
}
