/**
 * @(#)file    SelectMaker.java
 * @(#)author  chj
 * @(#)version 1.0
 * @(#)date    2014-06-24
 * @(#)since   JDK1.6
 *
 * Copyright (c)  www.udapsoft.co.kr, Inc.
 */
package com.udapsoft.waf.common.ui;

import kr.co.hsnc.common.sql.RowSet;
import kr.co.hsnc.common.sql.util.RowSetUtility;
import kr.co.hsnc.common.util.ValueObject;
import kr.co.hsnc.common.util.StringUtil;

/**
 * HTML SELECT Tag Manager
 * 
 * @author DaeJin Lee
 */
public class HTMLMaker {
	
    private static String R = "\n";
	
	public static String getSelectTag(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr, String defaultOption) {

        return getSelectTag(rowSet, valueName, dscrName, inputName, sytleStr, defaultOption, null);
    }

    /**
     * selectbox.htc를 이용하는 HTML SELECT Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @param sytleStr
	 * @param defaultOption
	 * @param selectedValue
	 * @return
	 */
	public static String getSelectTag(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr, String defaultOption, String selectedValue) {

        return getSelectTag(rowSet, valueName, dscrName, inputName, sytleStr, defaultOption, selectedValue, 10);
    }

	/**
     * selectbox.htc를 이용하는 HTML SELECT Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @param sytleStr
	 * @param defaultOption
	 * @param selectedValue
	 * @param size
	 * @return
	 */
	public static String getSelectTag(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr, String defaultOption, String selectedValue, int size) {
		return getSelectTag(rowSet, valueName, dscrName, inputName, sytleStr, defaultOption, selectedValue, size, null);
	}
	
	public static String getSelectTag(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String strClassNm) {
		return getSelectTag(rowSet, valueName, dscrName, inputName, sytleStr, defaultOption, selectedValue, size, strClassNm, null);
	}
	
	public static String getSelectTag(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String strClassNm, String eventStr) {

		String strSelectClassName = "input_select";
        ValueObject rowObject = null;

        if( sytleStr == null || sytleStr.equals("") )
            sytleStr = "width:100px;height=18;";

        if( sytleStr.toLowerCase().indexOf("style") == -1 ) {

            sytleStr = "style='" + sytleStr + "'";
        }
        
        // 넘어온 class name 이 있는 경우 해당 class 명으로 대체한다.
        if(strClassNm!=null && strClassNm.length()>0)
        	strSelectClassName = strClassNm;
        
        if( eventStr == null || eventStr.equals("") )
            eventStr = "";
        
        String selectStr = "<SELECT class='" + strSelectClassName + "' id='" + inputName + "' name='" + inputName + "' " + sytleStr + " " + eventStr + ">" + R;

        if( defaultOption != null && !defaultOption.equals("") )
            selectStr += defaultOption  + R;

        int rowSize = rowSet.size();
        if(rowSet != null && rowSize > 0){
            for( int i = 0 ; i < rowSize ; i++ ) {
                rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
                selectStr += "<OPTION value='" 
                           + toHtml(rowObject.get(valueName)) + "'"    + (rowObject.get(valueName).equals(selectedValue) ? "selected" : "") + ">" 
                           + toHtml(rowObject.get(dscrName))  + "</OPTION>" + R;
            }
        }
        else{
            selectStr += "<OPTION value=''></OPTION>";
        }

        selectStr += "</SELECT>";
        return selectStr;
    }
	/**
     * getSelectTag 와 같은 기능을 한다. 단... \n 이 빠져서 리턴한다.
	 */	
	public static String getSelectTag2(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr, String defaultOption, String selectedValue, int size, String strClassNm) {
		
		String strSelectClassName = "input_select";
		ValueObject rowObject = null;
		
		if( sytleStr == null || sytleStr.equals("") )
			sytleStr = "width:100px;height=18;";
		
		if( sytleStr.toLowerCase().indexOf("style") == -1 ) {
			
			sytleStr = "style='" + sytleStr + "'";
		}
		
		// 넘어온 class name 이 있는 경우 해당 class 명으로 대체한다.
		if(strClassNm!=null && strClassNm.length()>0)
		{
			strSelectClassName = strClassNm;
		}
		
		String selectStr = "<SELECT class='" + strSelectClassName + "' id='" + inputName + "_id' name='" + inputName + "' " + sytleStr + ">";
		
		if( defaultOption != null && !defaultOption.equals("") )
			selectStr += defaultOption;
		
		int rowSize = rowSet.size();
		if(rowSet != null && rowSize > 0){
			for( int i = 0 ; i < rowSize ; i++ ) {
				rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
				selectStr += "<OPTION value='" 
					+ toHtml(rowObject.get(valueName)) + "'"    + (rowObject.get(valueName).equals(selectedValue) ? "selected" : "") + ">" 
					+ toHtml(rowObject.get(dscrName))  + "</OPTION>";
			}
		}
		else{
			selectStr += "<OPTION value=''></OPTION>";
		}
		
		selectStr += "</SELECT>";
		return selectStr;
	}

	private static String toHtml(String str) {
		str = StringUtil.replace(str, "\"", "&quot;");
		str = StringUtil.replace(str, "'", "`");
	    return str;
	}
	
    /**
     * HTML SELECT Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @return
	 */
	public static String getGeneralSelectTag(RowSet rowSet, String valueName, String dscrName, String inputName) {

        return getGeneralSelectTag(rowSet, valueName, dscrName, inputName, null, null, null);
    }

    /**
     * HTML SELECT Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @param sytleStr
	 * @return
	 */
	public static String getGeneralSelectTag(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr) {

        return getGeneralSelectTag(rowSet, valueName, dscrName, inputName, sytleStr, null, null);
    }

    /**
     * HTML SELECT Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @param sytleStr
	 * @param defaultOption
	 * @return
	 */
	public static String getGeneralSelectTag(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr, String defaultOption) {

        return getGeneralSelectTag(rowSet, valueName, dscrName, inputName, sytleStr, defaultOption, null);
    }


    /**
     * HTML SELECT Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @param sytleStr
	 * @param defaultOption
	 * @param selectedValue
	 * @return
	 */
	public static String getGeneralSelectTag(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr, String defaultOption, String selectedValue) {
    
        ValueObject rowObject = null;
   
        String selectStr = "<SELECT id='" + inputName + "_id' name='" + inputName + "' " + sytleStr + ">";
    
        if( defaultOption != null && !defaultOption.equals("") )
            selectStr += defaultOption;
    
        int size = rowSet.size();
        for( int i = 0 ; i < size ; i++ ) {
            rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
            selectStr += "<OPTION value='" 
                       + toHtml(rowObject.get(valueName)) + "'"    + (rowObject.get(valueName).equals(selectedValue) ? "selected" : "") + ">" 
                       + toHtml(rowObject.get(dscrName))  + "";
        }
    
        selectStr += "</SELECT>";

        return selectStr;
    }

    /**
     * HTML RADIO Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @return
	 * @throws Exception
	 */
	public static String getRadioTag(RowSet rowSet, String valueName, String dscrName, String inputName) throws Exception {

        return getRadioTag(rowSet, valueName, dscrName, inputName, null);
    }

    /**
     * HTML RADIO Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @param checkedValue
	 * @return
	 * @throws Exception
	 */
	public static String getRadioTag(RowSet rowSet, String valueName, String dscrName, String inputName, String checkedValue) throws Exception {

        return getRadioTag(rowSet, valueName, dscrName, inputName, checkedValue, null);
    }

    /**
     * HTML RADIO Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @param checkedValue
	 * @param styleStr
     *  * @return
	 * @throws Exception
	 */
	public static String getRadioTag(RowSet rowSet, String valueName, String dscrName, String inputName, String checkedValue, String styleStr) throws Exception {

        ValueObject rowObject = null;

        String radioStr = "";

        int size = rowSet.size();
        for( int i = 0 ; i < size ; i++ ) {

            rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
            radioStr += "<LABEL for='" + inputName + "_" + i + "'>" 
                      + "<INPUT id='" + inputName + "_" + i + "' " 
                      +        "type='radio' "
                      +        "name='" + inputName + "' "
                      +        "value='" + toHtml(rowObject.get(valueName)) + "' " 
                      + (rowObject.get(valueName).equals(checkedValue) ? "checked " : "")
                      + ( styleStr == null ? "" : styleStr )
                      + ">"
                      + toHtml(rowObject.get(dscrName)) + "</LABEL>" + "&nbsp;" + "\n";
        }
        return radioStr;
    }
	
	


	/**
     * HTML RADIO Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @param checkedValue
	 * @param styleStr
     *  * @return
	 * @throws Exception
	 */
	// 2006-10-30 박선우 추가
	// 기본값을 추가할수 있도록 addValue값을 받게 Func.을 추가
	
	public static String getRadioTag(RowSet rowSet, String valueName, String dscrName, String inputName, String checkedValue, String styleStr,String addValue) throws Exception {

        ValueObject rowObject = null;

        String radioStr = "";

        int size = rowSet.size();
        for( int i = 0 ; i < size ; i++ ) {

            rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
            radioStr += "<LABEL for='" + inputName + "_" + i + "'>" 
                      + "<INPUT id='" + inputName + "_" + i + "' " 
                      +        "type='radio' "
                      +        "name='" + inputName + "' "
                      +        "value='" + toHtml(rowObject.get(valueName)) + "' " 
                      + (rowObject.get(valueName).equals(checkedValue) ? "checked " : "")
                      + ( styleStr == null ? "" : styleStr )
                      + ">"
                      + toHtml(rowObject.get(dscrName)) + "</LABEL>" + "&nbsp;" + "\n";
        }
        
        radioStr = addValue + radioStr;
        
        return radioStr;
    }
	
    /**
     * HTML CHECKBOX Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @return
	 * @throws Exception
	 */
	public static String getCheckBoxTag(RowSet rowSet, String valueName, String dscrName, String inputName) throws Exception {

        return getCheckBoxTag(rowSet, valueName, dscrName, inputName, null, null);
    }

    /**
     * HTML CHECKBOX Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @param checkedValues : ex> "VALUE1, VALUE2, VALUE3"
	 * @return
	 * @throws Exception
	 */
	public static String getCheckBoxTag(RowSet rowSet, String valueName, String dscrName, String inputName, String checkedValues) throws Exception {

        return getCheckBoxTag(rowSet, valueName, dscrName, inputName, checkedValues, null);
    }

    /**
     * HTML CHECKBOX Tag로 변환해 리턴을 한다.
	 * @param rowSet
	 * @param valueName
	 * @param dscrName
	 * @param inputName
	 * @param checkedValues : ex> "VALUE1, VALUE2, VALUE3"
	 * @param styleStr
	 * @return
	 * @throws Exception
	 */
	public static String getCheckBoxTag(RowSet rowSet, String valueName, String dscrName, String inputName, String checkedValues, String styleStr) throws Exception {

        ValueObject rowObject = null;

        String checkBoxStr = "";

        int size = rowSet.size();
        for( int i = 0 ; i < size ; i++ ) {

            rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
            checkBoxStr += "<LABEL for='" + inputName + "_" + i + "'>" 
                         + "<INPUT id='" + inputName + "_" + i + "' " 
                         +        "type='checkbox' "
                         +        "name='" + inputName + "' "
                         +        "value='" + toHtml(rowObject.get(valueName)) + "' " 
                         + ( StringLogic.inString(rowObject.get(valueName), checkedValues) ? "checked " : "")
                         + ( styleStr == null ? "" : styleStr )
                         + ">"
                         + toHtml(rowObject.get(dscrName)) + "</LABEL>" + "&nbsp;" + "\n";
        }
        return checkBoxStr;
    }


	/**
     * 일반 select_box HTML SELECT Tag로 변환해 리턴을 한다.
	 * 2004-06-28 sukjin chang 일반 select_box
	 */
	public static String common_getSelectTag(RowSet rowSet, String select_name, String style, String input_value, String input_name, String defalt_option, String selected_value) {
    
        ValueObject rowObject = null;
  
        String selectStr = "<SELECT  name='" + select_name + "' " + style + ">";
    
        if( defalt_option != null && !defalt_option.equals("") )
            selectStr += defalt_option;
		
		int rowSize = (rowSet != null ? rowSet.size() : 0);	
		
        for( int i = 0 ; i < rowSize ; i++ ) {
            rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
            selectStr  += "<OPTION value='" 
                       + rowObject.get(input_value) + "'"    + (rowObject.get(input_value).equals(selected_value) ? "selected" : "") + ">" 
                       + rowObject.get(input_name)  + "</OPTION>";
        }
            
        selectStr += "</SELECT>";
        return selectStr;
    }
	
	//2004-12-30추가 getSelect_Ac_unit_cdTag(commCodeRowSet, code_name, eventStr, name_gubun, defaultOption_value, selectedValue);
	public static String getSelect_Ac_unit_cdTag(RowSet rowSet, String code_name, String eventStr, String name_gubun, String defaultOption_value, String selectedValue) {
		
		int size = 0;
		String sytleStr = "";
		String dscrName = "";
		String valueName = "DETAILCD";
        
		ValueObject rowObject = null;
		
		
        if( name_gubun.equals("S") )
		{
            sytleStr = "style='width:100px;height=18;'";
			dscrName = "DETAILNM";
		}
		else 
		{
			sytleStr = "style='width:130px;height=18;';";
			dscrName = "DETAILSORTNM";
		}

        if( eventStr == null || eventStr.equals("") )
            eventStr = "";

        String selectStr = "<SCRIPT language='JavaScript'> var selectObj_" + code_name + " = writeSelect(\""
                         + "<SELECT id='" + code_name + "_id' name='" + code_name + "' " + sytleStr + " " + eventStr + ">";
    
        if( defaultOption_value != null && !defaultOption_value.equals(""))
            selectStr += "<OPTION value='" + defaultOption_value + "'" + (defaultOption_value.equals(selectedValue) ? "selected" : "") +">전체</OPTION>";
    
        int rowSize = rowSet.size();
        if(rowSet != null && rowSize > 0){
            for( int i = 0 ; i < rowSize ; i++ ) {
                rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
                selectStr += "<OPTION value='" 
                           + toHtml(rowObject.get(valueName)) + "'"    + (rowObject.get(valueName).equals(selectedValue) ? "selected" : "") + ">" 
                           + toHtml(rowObject.get(dscrName))  + "";
				size++;
            }
        }else{
            selectStr += "<OPTION value=''>";
        }
		
		if(size <= 1) size = 0; //select box 1건일때 고정할려고
		else          size = 10;
		
        selectStr += "</SELECT>\", " + size + ", true);</SCRIPT>";      
        return selectStr;
    }
    
    public static String getUpDownArrow(String input_text,String obj_name){
        String updownStr = "<TABLE height=\"20\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> \n" +
                           "    <TR> \n"+
                           "        <TD rowspan='2'>"+input_text+"</TD>"+
                           "        <TD rowspan='2' width='5'></TD>"+
                           "        <TD WIDTH='20'><IMG SRC='/img/button/btn_input_up_arrow.gif' HEIGHT='10' WIDTH='20' border=0 style='cursor:hand' onclick='upArrowClik("+obj_name+")'></TD> \n"+
                           "    </TR> \n"+
                           "    <TR> \n"+
                           "        <TD><IMG SRC='/img/button/btn_input_down_arrow.gif' HEIGHT='10' WIDTH='20' border=0 style='cursor:hand' onclick='downArrowClik("+obj_name+")'></TD> \n"+
                           "    </TR> \n"+
                           "</TABLE>";
        return updownStr;
    }	
	
	
	/*
	 * IBSheet 내에서 콤보 박스 생성시 이용 (Text 값) 
	 */
	public static String getSelectTagCombo(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr, String defaultOption, String selectedValue, int size) {
		ValueObject rowObject = null;
		String selectStr = "";
		
		int rowSize = rowSet.size();
        if(rowSet != null && rowSize > 0){
        	for( int i = 0 ; i < rowSize ; i++ ) {
        		rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
        		selectStr += toHtml(rowObject.get(dscrName)) + "|";
        	}
        	
        }
		
		return selectStr;
	}

	/*
	 * IBSheet 내에서 콤보 박스 생성시 이용 (value 값) 
	 */
	
	public static String getSelectTagCode(RowSet rowSet, String valueName, String dscrName, String inputName, String sytleStr, String defaultOption, String selectedValue, int size) {
		ValueObject rowObject = null;
		String selectStr = "";
		
		int rowSize = rowSet.size();
        if(rowSet != null && rowSize > 0){
        	for( int i = 0 ; i < rowSize ; i++ ) {
        		rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
        		selectStr += toHtml(rowObject.get(valueName)) + "|";
        	}
        	
        }
		
		return selectStr;
	}
	
	public static String getSelectTagText4Sheet(RowSet rowSet){
		ValueObject rowObject = null;
		String selectStr = "";
		
		int rowSize = rowSet.size();
        if(rowSet != null && rowSize > 0){
        	for( int i = 0 ; i < rowSize ; i++ ) {
        		rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
        		selectStr += toHtml(rowObject.get(0)) + "|";
        	}
        	
        }
		
		return selectStr;
	}
	
	public static String getSelectTagValue4Sheet(RowSet rowSet){
		ValueObject rowObject = null;
		String selectStr = "";
		
		int rowSize = rowSet.size();
        if(rowSet != null && rowSize > 0){
        	for( int i = 0 ; i < rowSize ; i++ ) {
        		rowObject = RowSetUtility.getValueObject(rowSet.getRow(i));
        		selectStr += toHtml(rowObject.get(1)) + "|";
        	}
        	
        }
		
		return selectStr;
	}

	
}