/**
 * @(#)file    HelpManager.java
 * @(#)author  cho sung geun
 * @(#)version 1.0
 * @(#)date    Jul 10, 2009
 * @(#)since   JDK 1.4.2
 *
 * Copyright (c) www.hwenc.co.kr, Inc.
 * All rights reserved.
 * This software is the proprietary information of Hanwha E&C, Inc.
 */
package kr.co.udapsoft.common.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.HttpServletRequest;
import kr.co.hsnc.common.base.WAFLogger;
import kr.co.hsnc.common.config.WAFConfig;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.hsnc.j2ee.waf.controller.web.util.WebKeys;

public class HelpManager {

    private HttpServletRequest request = null;

    private String EMP_NO = "";


    public HelpManager(HttpServletRequest request) {

        this.request = request;
        ValueObject user = (ValueObject)request.getSession().getAttribute(WebKeys.SIGNED_ON_USER);

        if( user != null ) {
            EMP_NO = user.get("USER_ID");
        }
    }

    /**
     * URLConnection 으로 Template 을 실행한 후 결과를 HTML 로 생성한다.
     * 각 메뉴화면에서 연결할 경우 사용한다.
     * @param  currentURI   -- 선택하 메뉴의 currentURI 
     * @param  useSystem    -- VOC , PCMS 등 카테고리 
     * @return String
     * @throws Exception
     */       
    public String createHtmlFile(String currentURI, String useSystem) {
 
        URL url = null;
        URLConnection urlConn = null;
        InputStream input = null;
        BufferedReader reader = null;
        StringBuffer html = null;
        
        String rtnStr = "";
        
        try {  
            
            html = new StringBuffer();  
            
            String protocol = "";           //"http";            
            String host     = "";           //"127.0.0.1";
            int    port     = 20000;            //8888;             
            
            //2008.8.15 L4 IP Drop(소스 ip와 목적지 ip가 같은경우) 해결위해 URL변경.
            if(WAFConfig.get("waf.was.ip").equals("")||WAFConfig.get("waf.was.port").equals("")){
                protocol = request.getScheme(); 
                host     = request.getServerName(); 
            }else{
                protocol = request.getScheme(); 
                host     = WAFConfig.get("waf.was.ip"); 
            }         
            
            String urlStr = protocol+"://"+host+":"+port+"/returnIcon.screen?emp_no="+EMP_NO+"&category="+useSystem+"&menu_name="+currentURI;
            
            url = new URL(urlStr);
            urlConn = url.openConnection();    
                                              
            if (urlConn!=null) {     
                
                String line = null;                                           
                
                input = urlConn.getInputStream(); 
                reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
                
                while((line = reader.readLine()) != null){
                    html.append(line);
                }
                
                rtnStr = html.toString();
                
                rtnStr = rtnStr.replaceAll("\r", "");
                rtnStr = rtnStr.replaceAll("\n", "");
            }
            
        } catch(Exception e) {
            WAFLogger.error("헬프 아이콘을 받아오는 중 에러가 발생 하였습니다.\n");
            
        }  finally {
            try {
                if (reader != null) reader.close();
            } catch (Exception ex_close) {}
        }//end of try
        
        return rtnStr;
        
    }//end of method  
    
    /**
     * 상단 메인 화면에서 연결할 경우 사용한다.
     * 각 시스템에 help.js를 제공해 callHelp()를 구현하도록 한다.
     * @return String
     * @throws Exception
     */       
    public String getTopHelpIcon( String useSystem , String fontColor ) {
 
        String rtnStr = "";
        
        //rtnStr = "<A href=\"javascript:callHelp('"+EMP_NO+"','"+useSystem+"');\" title=\"도움말 보기\"><img src='/img/main/help.gif'> </A>";
        rtnStr = "<img src=\"/img/main/help.gif\" alt=\"도움말 보기\" onclick=\"callHelp('"+EMP_NO+"','"+useSystem+"');\" style='cursor:hand'>";
        
            
        return rtnStr;
        
    }//end of method  
    
    
}