package kr.co.kihyun.beans.totsys.menu;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.MissingResourceException;

import javax.servlet.http.HttpServletRequest;

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

import kr.co.kihyun.moumi.MoumiConfig;

public class MenuBean implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory.getLogger(MenuBean.class);
	private URL url = null;
	private String menuName = null;

	public MenuBean(HttpServletRequest req, String path) throws MalformedURLException {
		this(new URL(new URL(new StringBuilder("http://").append(req.getServerName()).append(":")
				.append(req.getServerPort()).toString()), path)); //개발,로컬
//		this(new URL(new URL(new StringBuilder("https://").append(req.getServerName()).toString()), path)); 운영
	}

	public MenuBean(HttpServletRequest req, String path, String menuName) throws MalformedURLException {
		this(new URL(new URL(new StringBuilder("http://").append(req.getServerName()).append(":")
				.append(req.getServerPort()).toString()), path), menuName);//개발,로컬
//		this(new URL(new URL(new StringBuilder("https://").append(req.getServerName()).toString()), path), menuName); 운영
	}

	public MenuBean(URL url) {
		this.setUrl(url);
		try {
			if (url.getQuery() != null && url.getPath().contains("/totsys/totper/mydocbox/prssbox/doc_list.jsp")
					&& url.getQuery().contains("docType=END"))
				this.setMenuName(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.process.end")
						+ MoumiConfig.getMessageBundle().getString("moumi.message.doc"));
			else if (url.getQuery() != null && url.getPath().contains("/totsys/totper/mydocbox/prssbox/doc_list.jsp")
					&& url.getQuery().contains("docType=REG"))
				this.setMenuName(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.reg")
						+ MoumiConfig.getMessageBundle().getString("moumi.message.doc"));
			else if (url.getQuery() != null && url.getPath().contains("/totsys/totper/mydocbox/prssbox/doc_list.jsp")
					&& url.getQuery().contains("docType=CANCEL"))
				this.setMenuName(MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.end")
						+ MoumiConfig.getMessageBundle().getString("moumi.message.box") + " ["
						+ MoumiConfig.getMessageBundle().getString("moumi.message.tot_doc.process.cancel")
						+ MoumiConfig.getMessageBundle().getString("moumi.message.doc") + "]");
                        //left.jsp 메뉴에 내제출자료 및 전체제출자료 부분에서 링크되는 title.jsp부분 이름 상이 하여 아래 추가함
                        else if(url.getQuery() != null && url.getPath().contains("/totsys/repoper/mydocbox/endbox/report_list.jsp")
                                        && url.getQuery().contains("reportType=COMP"))
                                this.setMenuName(MoumiConfig.getMessageBundle().getString("moumi.message.menu.mySubmit")
                                                + MoumiConfig.getMessageBundle().getString("moumi.message.menu.doc"));
                        else if(url.getQuery() != null && url.getPath().contains("/totsys/repoper/mydocbox/endbox/report_list.jsp")
                                        && url.getQuery().contains("reportType=END"))
                                this.setMenuName(MoumiConfig.getMessageBundle().getString("moumi.message.menu.allSubmit")
                                                + MoumiConfig.getMessageBundle().getString("moumi.message.menu.doc"));
                        //게시판 title.jsp메뉴에 표시가 안되어 추가함(moumi_message_ko_KR.properties파일에도 추가됨
                        else if(url.getQuery() != null && url.getPath().contains("/totsys/sysadm/board/board_list.jsp")
                                        && url.getQuery().contains("boardGroupID=ID_FAQ"))
                            this.setMenuName(MoumiConfig.getMessageBundle().getString("moumi.message.board.faq"));
            /****
                        else if(url.getQuery() != null && url.getPath().contains("/totsys/sysadm/board/board_list.jsp")
                                        && url.getQuery().contains("boardGroupID=ID_QNA"))
                            this.setMenuName(MoumiConfig.getMessageBundle().getString("moumi.message.board.qna"));
             ****/
                        else if(url.getQuery() != null && url.getPath().contains("/totsys/sysadm/board/board_list.jsp")
                                        && url.getQuery().contains("boardGroupID=ID_PDS"))
                            this.setMenuName(MoumiConfig.getMessageBundle().getString("moumi.message.board.pds"));
                        else if(url.getQuery() != null && url.getPath().contains("/totsys/common/inc/statistics/statistics.jsp"))
                            this.setMenuName(MoumiConfig.getMessageBundle().getString("moumi.message.menu.statistics")+"(집계.제출)");
			else
				this.setMenuName(MoumiConfig.getMessageBundle().getString(
						url.getPath().replaceAll("^/", "moumi/message/").replaceAll("\\.", "_").replaceAll("/", ".")));
		} catch (MissingResourceException mre) {
			this.setMenuName(this.url.getPath().substring(this.url.getPath().lastIndexOf('/') + 1));
			LOG.error("{} is not defined in moumi_message.properties file",
					url.getPath().replaceAll("^/", "moumi/message/").replaceAll("\\.", "_").replaceAll("/", "."));
		}
	}

	public MenuBean(URL url, String menuName) {
		this.setUrl(url);
		this.setMenuName(menuName);
	}

	public void setUrl(URL url) {
		this.url = url;
	}

	public URL getUrl() {
		return url;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public String getMenuName() {
		return menuName;
	}

	public String toString() {
		return getMenuName();
	}
}
