package com.chinaweal.aiccs.common.util;

import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;

/**
 * Created by IntelliJ IDEA.
 * Class: �ַ��������Լ��ж��ࣨ�����������ϴ����ܣ�
 * User: zhouls
 * Date: 14-4-23
 * Time: ����1:51
 * To change this template use File | Settings | File Templates.
 */
public class WebUtil {

    /**
     * ʵ���ַ����п�
     *
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj) {
        if (obj != null) {
            if (obj.toString().trim().equals(""))
                return true;
            else
                return false;
        }
        return true;
    }


    /**
     * ʵ��ȥ���ַ����пո�
     *
     * @param str
     * @return
     */
    public static String removeSpaceOfString(String str) {
        if (str == null || "".equals(str.trim())) return "";
        char[] chr = str.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < chr.length; i++) {
            if (chr[i] != ' ' && chr[i] != '\n' && chr[i] != '\r') {
                sb.append(chr[i]);
            }
        }
        return sb.toString();
    }

    /**
     * ʵ��ȥ���ַ����е����ҿո�
     *
     * @param obj
     * @return
     */
    public static String trim(String obj) {
        if (isEmpty(obj))
            return "";
        return obj.trim();
    }

    public static String outHTML(Object obj) {
        if (isEmpty(obj))
            return "&nbsp;";
        return obj.toString();
    }

    public static String outNull(Object obj) {
        if (isEmpty(obj))
            return "";
        if (obj instanceof BigDecimal) {
            obj = ((BigDecimal) obj).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        return obj.toString();
    }

    public static String outSYNull(Object obj) {
        if (isEmpty(obj))
            return "";
        if (obj instanceof BigDecimal) {
            obj = ((BigDecimal) obj).setScale(4, BigDecimal.ROUND_HALF_UP);
        }
        return obj.toString();
    }

    public static String outDateNull(String value) {
        if (isEmpty(value))
            return "";
        return value.substring(0, 10);
    }

    public static String outIntNull(Object obj) {
        if (isEmpty(obj))
            return "";
        return obj.toString();
    }

    public static boolean isExists(String src,String tag){
        int i=-1;
        i=outNull(src).indexOf(outNull(tag));
        if (i>-1) return true;
        return false;
    }


    /**
     * ����1�ǡ�2���select��ǩ
     *
     * @param fieldName
     * @param
     * @param
     * @param isNeed
     * @return
     */
    public static String getBooleanHTML(String fieldName, String cName, String checkValue,
                                        int boardIndex, boolean isNeed, String width) {
        String[] id = new String[]{"1", "2"};
        String[] name = new String[]{"��", "��"};
        return getSelectHTML(fieldName, id, name, cName, checkValue, boardIndex, isNeed, width, "");
    }

    /**
     * ������������select��ǩ
     *
     * @param fieldName
     * @param
     * @param
     * @param isNeed
     * @return
     */
    public static String getSelectHTML(String fieldName, String[] id, String[] name,
                                       String cName, String checkValue,
                                       int boardIndex, boolean isNeed, String width) {

        return getSelectHTML(fieldName, id, name, cName, checkValue, boardIndex, isNeed, width, "");
    }

    public static String getSelectHTML(String fieldName, String[] id, String[] name,
                                       String cName, String checkValue,
                                       int boardIndex, boolean isNeed, String width, String method) {
        StringBuffer html = new StringBuffer("<select name='" + fieldName + "' isNeed='" + (isNeed ? "y" : "n")
                + "' size='1' cName='" + cName + "' " + method + " " +
                "boardIndex=" + boardIndex + " style='width:" + width + "'" + ">");
        html.append("<option value=''></option>");
        for (int i = 0; i < id.length; i++) {
            html.append("\n<option value='" + id[i] + "' "
                    + (id[i].equals(checkValue) ? "selected" : "") + ">"
                    + name[i] + "</option>");
        }
        html.append("\n</select>\n");
        return html.toString();
    }

    public static String getIntView(Object value) {
        if (null == value) {
            return "";
        }
        String strValue = value.toString();
        int idx = strValue.indexOf('.');
        return strValue.substring(0, idx);
    }

    public static boolean isAjax(HttpServletRequest request) {
        String accept = request.getHeader("accept");
        String X_Requested_With = request.getHeader("X-Requested-With");
        return (accept != null && accept.contains("application/json"))
                || (X_Requested_With != null && X_Requested_With.contains("XMLHttpRequest"));
    }

    public static boolean isNotAjax(HttpServletRequest request) {
        return !isAjax(request);
    }

    public static boolean isMSIE(String userAgent) {
        return org.apache.commons.lang.StringUtils.contains(userAgent, "MSIE") || StringUtils.contains(userAgent, "Trident");
    }

    public static String encodeDownloadFileName(boolean isMSIE, String fileName) throws UnsupportedEncodingException {
        return isMSIE ? URLEncoder.encode(fileName, "UTF-8") : new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
    }
}
