package com.eactive.eai.agent.web;

import java.io.File;
import java.util.ArrayList;

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

import com.eactive.eai.batch.common.BatchDirUtil;
import com.eactive.eai.batch.flowController.BatchTargetVO;
import com.eactive.eai.batch.flowController.FlowControllerManager;
import com.eactive.eai.batch.osd.OutsideManager;
import com.eactive.eai.batch.osd.OutsideVO;
import com.eactive.eai.common.util.Logger;

public class CheckFileSysHealth  extends HttpServlet
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

   /**
    * 1. 기능 :
    * 2. 처리 개요 :
    * 3. 주의사항
    *
    * @param    request     HttpServletRequest
    * @param    response    HttpServletResponse
    * @return
    * @exception
    **/
    public void service(HttpServletRequest request, HttpServletResponse response) {
        try {
            logger.info("[CheckFileSysHealth] Servlet starting .......");
            response.setContentType("text/html;charset=euc-kr");
            StringBuffer sb = new StringBuffer();

            String strBatch  = request.getParameter("batch");
            if (strBatch == null)   strBatch = "";
            String strOutside  = request.getParameter("outside");
            if (strOutside==null)   strOutside = "";

            Object batchCodes[] = OutsideManager.getInstance().getBatchCodeArray();
            ArrayList<String>   batchNames = new ArrayList<String>();
            for (int i=0; i<batchCodes.length; i++) {
            	ArrayList<OutsideVO> organList = OutsideManager.getInstance().getOutsideInfo((String)batchCodes[i]);
                if (organList.size()>0) {
                    OutsideVO osdvo = (OutsideVO)organList.get(0);
                    BatchTargetVO bvo = FlowControllerManager.getInstance().getBatchTargetInfo(osdvo.getBatchCode(), osdvo.getOsdCode());
                    if (bvo == null) {
                        batchNames.add("");
                    } else {
                        batchNames.add(bvo.getProcessName());
                    }
                } else {
                    batchNames.add("");
                }
            }
            response.getWriter().println("<%@ taglib uri='/WEB-INF/tld/c.tld' prefix='c'%>");
            response.getWriter().println("<HTML><HEAD>");
            response.getWriter().println("</HEAD>");
            response.getWriter().println("<BODY TOPMARGIN=0 RIGHTMARGIN=0 LEFTMARGIN=0 >");
            response.getWriter().println("<table cellSpacing=1 cellPadding=3 width=640 bgColor=#7f7f7f border=0 STYLE='font-size:10pt'>");
            response.getWriter().println("<TBODY>");
            if (strBatch.equalsIgnoreCase("all")) {
                for (int i=0; i< batchCodes.length; i++) {
                    String strBatchCode = (String)batchCodes[i];
                    String strBatchName = (String)batchNames.get(i);

                    response.getWriter().println("<TR bgcolor='#ffbbbb' height='20' class='txt'>");
                    response.getWriter().println("<TD class=c style='PADDING-LEFT: 10px' height=20>");
                    response.getWriter().println(" BATCH CODE ");
                    response.getWriter().println(" </TD><TD> ");
                    response.getWriter().println( strBatchCode );
                    response.getWriter().println(" </TD>");
                    response.getWriter().println("<TD class=c style='PADDING-LEFT: 10px' height=20>");
                    response.getWriter().println(" BATCH NAME ");
                    response.getWriter().println(" </TD><TD>" );
                    response.getWriter().println( strBatchName );
                    response.getWriter().println(" </TD>");
                    response.getWriter().println(" </TR>");
                    PrintBatchSubDirectory(response, strOutside, strBatchName, strBatchCode);
                }

            } else  {
                for (int i=0; i< batchCodes.length; i++) {
                    String strBatchCode = (String)batchCodes[i];
                    String strBatchName = (String)batchNames.get(i);
                    if (strBatch.equalsIgnoreCase(strBatchCode)) {
                        response.getWriter().println("<TR bgcolor='#ffbbbb' height='20' class='txt'>");
                        response.getWriter().println("<TD class=c style='PADDING-LEFT: 10px' height=20>");
                        response.getWriter().println(" BATCH CODE ");
                        response.getWriter().println(" </TD><TD> ");
                        response.getWriter().println( strBatchCode );
                        response.getWriter().println(" </TD>");
                        response.getWriter().println("<TD class=c style='PADDING-LEFT: 10px' height=20>");
                        response.getWriter().println(" BATCH NAME ");
                        response.getWriter().println(" </TD><TD>" );
                        response.getWriter().println( strBatchName );
                        response.getWriter().println(" </TD>");
                        response.getWriter().println(" </TR>");
                        PrintBatchSubDirectory(response, strOutside, strBatchName, strBatchCode);
                        break;
                    }
                }
            }

            response.getWriter().println("</TBODY> </TABLE>");
            response.getWriter().println("</HTML>");
            response.getWriter().println(sb.toString());
        } catch (Exception ex) {
        	logger.error( ex.getMessage(), ex);
            response.setStatus(500);
            try {
                response.getWriter().println(ex.getMessage());
                throw ex;
            } catch (Exception e) {
            }
        }
    }

    private void PrintBatchSubDirectory(HttpServletResponse response, String strOsdCondition, String strBatchName, String strBatchCode) throws Exception
    {
    	ArrayList<OutsideVO> organList = OutsideManager.getInstance().getOutsideInfo(strBatchCode);
        for (int i=0; i<organList.size(); i++) {
            OutsideVO osdvo = (OutsideVO)organList.get(i);
            if (strOsdCondition.equalsIgnoreCase("") || strOsdCondition.equalsIgnoreCase("all")) {
                response.getWriter().println("<TR bgcolor='#ffff' height='20' class='txt'>");
                response.getWriter().println("<TD class=c style='PADDING-LEFT: 10px' bgcolor=#eeffff height=20>");
                response.getWriter().println(" 배치업무 ");
                response.getWriter().println(" </TD><TD> ");
                response.getWriter().println( strBatchName+"["+strBatchCode+"]" );
                response.getWriter().println(" </TD>");
                response.getWriter().println("<TD class=c style='PADDING-LEFT: 10px' bgcolor=#eeffff height=20>");
                response.getWriter().println(" 대외기관 ");
                response.getWriter().println(" </TD><TD>" );
                response.getWriter().println( osdvo.getOsdName()+"["+osdvo.getOsdCode()+"]" );
                response.getWriter().println(" </TD>");
                response.getWriter().println(" </TR>");
                PrintOsdSubDirectory(response, strBatchCode, osdvo.getOsdCode());
            } else {
                if (strOsdCondition.equalsIgnoreCase(osdvo.getOsdCode())) {
                    response.getWriter().println("<TR bgcolor='#ffff' height='20' class='txt'>");
                    response.getWriter().println("<TD class=c style='PADDING-LEFT: 10px' bgcolor=#eeffff height=20>");
                    response.getWriter().println(" 배치업무 ");
                    response.getWriter().println(" </TD><TD> ");
                    response.getWriter().println( strBatchName+"["+strBatchCode+"]" );
                    response.getWriter().println(" </TD>");
                    response.getWriter().println("<TD class=c style='PADDING-LEFT: 10px' bgcolor=#eeffff height=20>");
                    response.getWriter().println(" 대외기관 ");
                    response.getWriter().println(" </TD><TD>" );
                    response.getWriter().println( osdvo.getOsdName()+"["+osdvo.getOsdCode()+"]" );
                    response.getWriter().println(" </TD>");
                    response.getWriter().println(" </TR>");
                    PrintOsdSubDirectory(response, strBatchCode, osdvo.getOsdCode());
                }
            }
        }
    }

    private void PrintOsdSubDirectory(HttpServletResponse response, String strBatCd, String strOsdCd) throws Exception
    {
        StringBuffer sb = new StringBuffer();
        
        sb.append(strBatCd);
        sb.append("/");
        sb.append(strOsdCd);

        response.getWriter().println("<TR bgcolor='#dddddd' height='20' class='txt'>");
        response.getWriter().println("<TD align=left colspan=4 class=c style='PADDING-LEFT: 10px' bgcolor=#e7e7e7 height=20>");
        response.getWriter().println("요구송신용 디렉토리");
        response.getWriter().println(" </TD> ");

        PrintCheckResult(response, BatchDirUtil.getRequestArchDir()+"/"+sb.toString());
        PrintCheckResult(response, BatchDirUtil.getRequestErrorDir()+"/"+sb.toString());
//        PrintCheckResult(response, BatchDirUtil.getRequestRealDir()+"/"+sb.toString());
//        PrintCheckResult(response, BatchDirUtil.getRequestRootDir()+"/"+sb.toString());

        response.getWriter().println("<TR bgcolor='#dddddd' height='20' class='txt'>");
        response.getWriter().println("<TD align=left colspan=4 class=c style='PADDING-LEFT: 10px' bgcolor=#e7e7e7 height=20>");
        response.getWriter().println("요구/응답수신용 디렉토리");
        response.getWriter().println(" </TD> ");

        PrintCheckResult(response, BatchDirUtil.getResponseArchDir()+"/"+sb.toString());
        PrintCheckResult(response, BatchDirUtil.getResponseErrorDir()+"/"+sb.toString());
        PrintCheckResult(response, BatchDirUtil.getResponseRealDir()+"/"+sb.toString());
        PrintCheckResult(response, BatchDirUtil.getResponseRootDir()+"/"+sb.toString());
        
//        if (strGroupCondition.equals("") || strGroupCondition.equals("all")) {
//            sb = new StringBuffer();
//            sb.append(strBatNm +"_"+ strBatCd);
//            sb.append("/");
//            sb.append(strOsdNm +"_"+ strOsdCd);
//
//            response.getWriter().println("<TR bgcolor='#dddddd' height='20' class='txt'>");
//            response.getWriter().println("<TD align=left colspan=4 class=c style='PADDING-LEFT: 10px' bgcolor=#e7e7e7 height=20>");
//            response.getWriter().println("응답송신용 디렉토리");
//            response.getWriter().println(" </TD> ");
//
//            PrintCheckResult(response, BatchDirUtil.getResSendArchDir()+"/"+sb.toString());
//            PrintCheckResult(response, BatchDirUtil.getResSendRealDir()+"/"+sb.toString());
//        }
    }

    private void PrintCheckResult(HttpServletResponse response, String strDir) throws Exception
    {
        File f = new File(strDir);

        response.getWriter().println("<TR bgcolor='#ffffff' height='20' class='txt'>");
        response.getWriter().println("<TD align=left colspan=3 class=c style='PADDING-LEFT: 10px' bgcolor=#ffffff height=20>");
        response.getWriter().println(strDir);
        response.getWriter().println("</TD><TD align=center>");

        if (f.exists()) {
            response.getWriter().println("<font color=blue>EXIST</font>");
        } else {
            response.getWriter().println("<font color=red>NOT EXIST</font>");
        }
        response.getWriter().println("</TD>");
        response.getWriter().println("</TR>");
    }
}
