package cmm.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MessageAlertUtil
{
	private static final Logger LOGGER = LoggerFactory.getLogger(MessageAlertUtil.class);

	public static void alert(String msg, HttpServletResponse response){

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("alert(\'" + msg + "\');");
			out.println("</script>");
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alert()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alert()", e);
		}
		finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertBack(String msg, HttpServletResponse response)
	{
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try{
			out = response.getWriter();
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("alert(\'" + msg + "\');");
			out.println("history.back(-1);");
			out.println("</script>");
		}
		catch (IOException ex){
			LOGGER.error("Error in alertBack()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertBack()", e);
		}finally{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertBackExceptionMessage(String msg, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();

			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("alert(\'" + msg + "\');");
			out.println("history.back();");
			out.println("</script>");
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertBackExceptionMessage()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertBackExceptionMessage()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertBack(String msg, Object[] args, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();

			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("alert(\'" + msg + "\');");
			out.println("history.back();");
			out.println("</script>");
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertBack()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertBack()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertClose(String msg, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("alert(\'" + msg + "\');");
			out.println("self.close();");
			out.println("</script>");
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertClose()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertClose()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertCloseOpenerReload(String msg, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();

			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("alert(\'" + msg + "\');");
			out.println("self.close();");
			out.println("opener.location.reload();");
			out.println("</script>");
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertCloseOpenerReload()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertCloseOpenerReload()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertRedirect(String msg, String url, HttpServletResponse response)
	{
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "0");

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			out.println("<meta http-equiv=\"Expires\" content=\"-1\">");
			out.println("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
			out.println("<meta http-equiv=\"Cache-Control\" content=\"No-Cache\">");
			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("var isDoubleAction = fasle;");
			out.println("try {");
			out.println("var preHLInput = document.frm.currentHistoryLength;");
			out.println("var preHLValue = preHLInput.value;");
			out.println("if(preHLInput && preHLValue && ((window.history.length - preHLValue) <= 1)) {");
			out.println("alert(\'" + msg + "\');");
			out.println("}else{");
			out.println("isDoubleAction = true;");
			out.println("alert('" + msg + "');");
			out.println("return;");
			out.println("}");
			out.println("}catch(e){");
			out.println("alert(\'" + msg + "\');");
			out.println("try{console.log(e);}catch(e1){var output = '줄번호:192\\n';for (var i in e) {output += i + ' : ' + e[i] + '\\n';}alert(output);}");
			out.println("}");
			out.println("if(isDoubleAction == fasle){");
			out.println("document.target='_blank';");
			out.println("document.location.href='" + url + "';");
			out.println("}");
			out.println("</script>");
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertRedirect()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertRedirect()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertRedirect(String msg, String url, String hiddens, HttpServletResponse response)
	{
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "0");

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			StringBuffer sb = new StringBuffer();

			sb.append("<meta http-equiv=\"Expires\" content=\"-1\">");
			sb.append("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
			sb.append("<meta http-equiv=\"Cache-Control\" content=\"No-Cache\">");
			sb.append("<meta name=\"decorator\" content=\"iframe\">");
			sb.append("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			sb.append("fn_onload = function() {");
			sb.append("try {");
			sb.append("var preHLInput = document.frm.currentHistoryLength;");
			sb.append("var preHLValue = preHLInput.value;");
			sb.append("if(preHLInput && preHLValue && ((window.history.length - preHLValue) <= 1)) {");
			sb.append("alert(\'" + msg + "\');");
			sb.append("}else{");
			sb.append("alert('" + msg + "');");
			sb.append("return;");
			sb.append("}");
			sb.append("}catch(e){");
			sb.append("alert(\'" + msg + "\');");
			sb.append("try{console.log(e);}catch(e1){var output = '줄번호:251\\n';for (var i in e) {output += i + ' : ' + e[i] + '\\n';}alert(output);}");
			sb.append("}");
			sb.append("document.frm.action = \"_self\";");
			sb.append("document.frm.action = \"" + url + "\";");
			sb.append("document.frm.submit();");
			sb.append("}");
			sb.append("</script>");
			sb.append("<body onload=\"fn_onload()\">");
			sb.append("<form name=\"frm\" method=\"POST\">");
			sb.append(hiddens);
			sb.append("</form>");
			sb.append("</body>");

			out.println(sb.toString());

		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertRedirect()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertRedirect()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertReload(String msg, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("alert(\'" + msg + "\');");
			out.println("location.reload();");
			out.println("</script>");
			out.close();

		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertReload()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertReload()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertOpenerReload(String msg, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			// out.println("document.domain='daum.net';");
			out.println("opener.location.reload();");
			out.println("alert(\'" + msg + "\');");
			out.println("this.close();");
			out.println("</script>");
			out.close();

		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertOpenerReload()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertOpenerReload()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertCloseOpenerClose(String msg, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			// out.println("document.domain='daum.net';");
			out.println("opener.close();");
			out.println("alert(\'" + msg + "\');");
			out.println("this.close();");
			out.println("</script>");
			out.close();

		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertCloseOpenerClose()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertCloseOpenerClose()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void println(Object obj, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			out.println(obj);
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in println()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in println()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void println(String msg, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();

			out.println(msg);
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in println()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in println()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void script(String script, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();

			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println(script);
			out.println("</script>");
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in script()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in script()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertRedirectTop(String msg, String url, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();

			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("alert(\'" + msg + "\');");
			out.println("document.target='_top';");
			out.println("document.location.href='" + url + "';");
			out.println("</script>");
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertRedirectTop()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertRedirectTop()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertRedirectClose(String msg, String url, HttpServletResponse response)
	{

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();

			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("alert(\'" + msg + "\');");
			out.println("window.close();");
			out.println("</script>");

			/*out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("alert(\'" + msg + "\');");
			out.println("document.target='_top';");
			out.println("document.close();");
			out.println("</script>");*/
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertRedirectClose()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertRedirectClose()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertRedirect(String msg, String url, HttpServletRequest request, HttpServletResponse response)
	{
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "0");

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();

			out.println("<meta http-equiv=\"Expires\" content=\"-1\">");
			out.println("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
			out.println("<meta http-equiv=\"Cache-Control\" content=\"No-Cache\">");
			out.println("<meta name=\"decorator\" content=\"iframe\">");
			out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			out.println("var isDoubleAction = fasle;");
			out.println("try {");
			out.println("var preHLInput = document.frm.currentHistoryLength;");
			out.println("var preHLValue = preHLInput.value;");
			out.println("if(preHLInput && preHLValue && ((window.history.length - preHLValue) <= 1)) {");
			out.println("alert(\'" + msg + "\');");
			out.println("}else{");
			out.println("isDoubleAction = true;");
			out.println("alert('" + msg + "');");
			out.println("return;");
			out.println("}");
			out.println("}catch(e){");
			out.println("alert(\'" + msg + "\');");
			out.println("try{console.log(e);}catch(e1){var output = '줄번호:603\\n';for (var i in e) {output += i + ' : ' + e[i] + '\\n';}alert(output);}");
			out.println("}");
			out.println("if(isDoubleAction == fasle){");
			out.println("document.target='_blank';");
			out.println("document.location.href='" + request.getContextPath() + url + "';");
			out.println("}");
			out.println("</script>");
		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertRedirect()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertRedirect()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertRedirect(String msg, String url, String hiddens, HttpServletRequest request, HttpServletResponse response)
	{
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "0");

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			StringBuffer sb = new StringBuffer();

			sb.append("<meta http-equiv=\"Expires\" content=\"-1\">");
			sb.append("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
			sb.append("<meta http-equiv=\"Cache-Control\" content=\"no-cache\">");
			sb.append("<meta name=\"decorator\" content=\"iframe\">");
			sb.append("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			sb.append("fn_onload = function() {");
			sb.append("try {");
			sb.append("var preHLInput = document.frm.currentHistoryLength;");
			sb.append("var preHLValue = preHLInput.value;");
			sb.append("if(preHLInput && preHLValue && ((window.history.length - preHLValue) <= 1)) {");
			sb.append("alert(\'" + msg + "\');");
			sb.append("}else{");
			sb.append("alert('" + msg + "');");
			sb.append("return;");
			sb.append("}");
			sb.append("}catch(e){");
			sb.append("alert(\'" + msg + "\');");
			//sb.append("try{console.log(e);}catch(e1){var output = '줄번호:664\\n';for (var i in e) {output += i + ' : ' + e[i] + '\\n';}alert(output);}");
			sb.append("}");
			sb.append("document.frm.action = \"_self\";");
			sb.append("document.frm.action = \"" + request.getContextPath() + url + "\";");
			sb.append("document.frm.submit();");
			sb.append("}");
			sb.append("</script>");
			sb.append("<body onload=\"fn_onload()\">");
			sb.append("<form name=\"frm\" method=\"POST\">");
			sb.append(hiddens);
			sb.append("</form>");
			sb.append("</body>");

			out.println(sb.toString());

		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertRedirect()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertRedirect()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}

	public static void alertDirectMsgRedirect(String msg, String url, String hiddens, HttpServletRequest request, HttpServletResponse response)
	{
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "0");

		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			StringBuffer sb = new StringBuffer();

			sb.append("<meta http-equiv=\"Expires\" content=\"-1\">");
			sb.append("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
			sb.append("<meta http-equiv=\"Cache-Control\" content=\"No-Cache\">");
			sb.append("<meta name=\"decorator\" content=\"iframe\">");
			sb.append("<script language=\"JavaScript\" type=\"text/JavaScript\">");
			sb.append("fn_onload = function() {");
			if (msg != null && !"".equals(msg) && !"null".equals(msg)) {
				sb.append("try {");
				sb.append("var preHLInput = document.frm.currentHistoryLength;");
				sb.append("var preHLValue = preHLInput.value;");
				sb.append("if(preHLInput && preHLValue && ((window.history.length - preHLValue) <= 1)) {");
				sb.append("alert(\'" + msg + "\');");
				sb.append("}else{");
				sb.append("alert('" + msg + "');");
				sb.append("return;");
				sb.append("}");
				sb.append("}catch(e){");
				sb.append("alert(\'" + msg + "\');");
				sb.append("try{console.log(e);}catch(e1){var output = '줄번호:732\\n';for (var i in e) {output += i + ' : ' + e[i] + '\\n';}alert(output);}");
				sb.append("}");
			}
			sb.append("document.frm.action = \"_self\";");
			sb.append("document.frm.action = \"" + request.getContextPath() + url + "\";");
			sb.append("document.frm.submit();");
			sb.append("}");
			sb.append("</script>");
			sb.append("<body onload=\"fn_onload()\">");
			sb.append("<form name=\"frm\" method=\"POST\">");
			sb.append(hiddens);
			sb.append("</form>");
			sb.append("</body>");

			out.println(sb.toString());

		}
		catch (IOException ex)
		{
			LOGGER.error("Error in alertDirectMsgRedirect()", ex);
		}catch (Exception e)
		{
			LOGGER.error("Error in alertDirectMsgRedirect()", e);
		}finally
		{
			if(out != null){
				out.close();
			}
		}
	}}
