package com.dbnt.kcscbackend.config.util;

import javax.servlet.http.HttpServletRequest;

public class ClientUtils {
  public static String getRemoteIP(HttpServletRequest request){
    String ip = request.getHeader("X-FORWARDED-FOR");

    //proxy 환경일 경우
    if (ip == null || ip.isEmpty()) {
      ip = request.getHeader("Proxy-Client-IP");
    }
    //웹로직 서버일 경우
    if (ip == null || ip.isEmpty()) {
      ip = request.getHeader("WL-Proxy-Client-IP");
    }
    if (ip == null || ip.isEmpty()) {
      ip = request.getRemoteAddr() ;
    }
    if (ip == null || ip.isEmpty()) {
      ip = request.getHeader("HTTP_CLIENT_IP");
    }
    if (ip == null || ip.isEmpty()) {
      ip = request.getHeader("HTTP_X_FORWARDED_FOR");
    }
    if (ip == null || ip.isEmpty()) {
      ip = request.getRemoteAddr();
    }

    return ip;
  }

  public static String getWebType(HttpServletRequest request) {
    String filter = "iphone|ipod|android|windows ce|blackberry|symbian|windows phone|webos|opera mini|opera mobi|polaris|iemobile|lgtelecom|nokia|sonyericsson|lg|samsung";
    String filters[] = filter.split("\\|");
    String webType = "";

    for(String tmp : filters){
      if (request.getHeader("User-Agent").toLowerCase().contains(tmp)) {
        webType = "MOBILE";
        break;
      } else {
        webType = "PC";
      }
    }
    return webType;
  }
}
