package com.chinacreator.video.restful.util;

import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;

public class RequestUtil
{
    private static final String ANDROID = ".*(?i)Android.*";
    private static final String IPHONE = ".*(?i)iPhone.*";
    
    public static String getQueryString(final HttpServletRequest request) {
        if ("GET".equalsIgnoreCase(request.getMethod())) {
            return request.getQueryString();
        }
        final Enumeration<String> enumeration = (Enumeration<String>)request.getParameterNames();
        final StringBuffer buffer = new StringBuffer();
        while (enumeration.hasMoreElements()) {
            final String key = enumeration.nextElement();
            String value = request.getParameter(key);
            if ("userid".equals(key)) {
                try {
                    value = URLEncoder.encode(value, "UTF-8");
                }
                catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            buffer.append(key).append("=").append(value);
            if (enumeration.hasMoreElements()) {
                buffer.append("&");
            }
        }
        return buffer.toString();
    }
    
    public static String getIpAddr(final HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (inValidateIpAddr(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (inValidateIpAddr(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (inValidateIpAddr(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (inValidateIpAddr(ip)) {
            ip = request.getHeader("clientip");
        }
        if (inValidateIpAddr(ip)) {
            ip = request.getRemoteAddr();
        }
        final int index = ip.indexOf(",");
        if (index != -1) {
            ip = ip.substring(0, index);
        }
        return ip;
    }
    
    private static boolean inValidateIpAddr(final String ip) {
        return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || ip.startsWith("10.") || ip.startsWith("172.") || ip.startsWith("192.");
    }
    
    public static String getHostIp() {
        try {
            final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                final NetworkInterface intf = en.nextElement();
                final Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    final InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        }
        catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static String getUserAgent(final HttpServletRequest request) {
        return request.getHeader("user-agent");
    }
    
    public static String getMobType(final HttpServletRequest request) {
        final String userAgent = getUserAgent(request);
        String appType = null;
        if (userAgent.matches(".*(?i)Android.*")) {
            appType = "Android";
        }
        else if (userAgent.matches(".*(?i)iPhone.*")) {
            appType = "iPhone";
        }
        return appType;
    }
}
