package com.eactive.eai.transformer.util;

import org.apache.commons.lang3.StringUtils;

import com.eactive.eai.transformer.function.Function;
import com.eactive.eai.transformer.layout.Item;
import com.eactive.eai.transformer.layout.Types;


public class ConstantUtil {
	private ConstantUtil() {
		
	}

	public static int getDataTypeId(String typeName, String layoutTypeName) {
		/**
		 * host data type
		 */
		if (layoutTypeName != null && ("BYTES".equalsIgnoreCase(layoutTypeName)
				|| "EBCDIC".equalsIgnoreCase(layoutTypeName) || "VBYTES".equalsIgnoreCase(layoutTypeName)
				|| "VFIELD".equalsIgnoreCase(layoutTypeName) || "VEBCDIC".equalsIgnoreCase(layoutTypeName)
				|| "COMBYTES".equalsIgnoreCase(layoutTypeName) || "COMEBCDIC".equalsIgnoreCase(layoutTypeName)
				|| "ODDBYTES".equalsIgnoreCase(layoutTypeName) || "CARD".equalsIgnoreCase(layoutTypeName)
				|| StringUtils.startsWith(layoutTypeName, "ISO8583"))) {
			return Types.TYPE_BYTEARRAY;
		} else {
			if ("PIC X(n)".equalsIgnoreCase(typeName)) {
				return Types.TYPE_STRING;
			} else if ("PIC 9(n)".equalsIgnoreCase(typeName)) {
				return Types.TYPE_INT;
			} else if ("PIC S9(n)".equalsIgnoreCase(typeName)) {
				return Types.TYPE_INT;
			} else if ("PIC 9(n)V9(d)".equalsIgnoreCase(typeName)) {
				return Types.TYPE_BIGDECIMAL;
			} else if ("PIC S9(n)V9(d)".equalsIgnoreCase(typeName)) {
				return Types.TYPE_BIGDECIMAL;
			}
		}

		/**
		 * normal
		 */
		if ("STRING".equalsIgnoreCase(typeName)) {
			return Types.TYPE_STRING;
		} else if (StringUtils.equalsAnyIgnoreCase(typeName, "INT", "INTEGER")) {
			return Types.TYPE_INT;
		} else if ("BIGDECIMAL".equalsIgnoreCase(typeName)) {
			return Types.TYPE_BIGDECIMAL;
		} else if ("LONG".equalsIgnoreCase(typeName)) {
			return Types.TYPE_LONG;
		} else if ("SHORT".equalsIgnoreCase(typeName)) {
			return Types.TYPE_SHORT;
		} else if ("DOUBLE".equalsIgnoreCase(typeName)) {
			return Types.TYPE_DOUBLE;
		} else if ("FLOAT".equalsIgnoreCase(typeName)) {
			return Types.TYPE_FLOAT;
		} else if ("CHAR".equalsIgnoreCase(typeName)) {
			return Types.TYPE_CHAR;
		} else if ("BYTE".equalsIgnoreCase(typeName)) {
			return Types.TYPE_BYTE;
		} else if ("DATE".equalsIgnoreCase(typeName)) {
			return Types.TYPE_DATE;
		} else if ("TIME".equalsIgnoreCase(typeName)) {
			return Types.TYPE_TIME;
		} else if ("TIMESTAMP".equalsIgnoreCase(typeName)) {
			return Types.TYPE_TIMESTAMP;
		} else if ("NUMBER".equalsIgnoreCase(typeName)) {
			return Types.TYPE_NUMBER;
		} else if ("BYTEARRAY".equalsIgnoreCase(typeName)) {
			return Types.TYPE_BYTEARRAY;
		} else if ("NUMERIC".equalsIgnoreCase(typeName)) {
			return Types.TYPE_NUMERIC;
		} else if ("PACKDECIMAL".equalsIgnoreCase(typeName)) {
			return Types.TYPE_PACKDECIMAL;
		} else if ("OBJECT".equalsIgnoreCase(typeName)) {
			return Types.TYPE_OBJECT;
		} else if ("UNKNOWN".equalsIgnoreCase(typeName)) {
			return Types.TYPE_UNKNOWN;
		} else if ("BOOLEAN".equalsIgnoreCase(typeName)) {
			return Types.TYPE_BOOLEAN;
		} else if ("".equalsIgnoreCase(typeName.trim())) {
			return Types.TYPE_UNKNOWN;
		}

		throw new RuntimeException("알 수 없는 타입을 지정하였습니다. - " + typeName);
	}

    public static String getDataTypeName(int typeId) {
        switch (typeId) {
        case Types.TYPE_STRING:
            return "STRING";
        case Types.TYPE_INT:
            return "INT";
        case Types.TYPE_LONG:
            return "LONG";
        case Types.TYPE_SHORT:
            return "SHORT";
        case Types.TYPE_DOUBLE:
            return "DOUBLE";
        case Types.TYPE_FLOAT:
            return "FLOAT";
        case Types.TYPE_CHAR:
            return "CHAR";
        case Types.TYPE_BYTE:
            return "BYTE";
        case Types.TYPE_DATE:
            return "DATE";
        case Types.TYPE_TIME:
            return "TIME";
        case Types.TYPE_TIMESTAMP:
            return "TIMESTAMP";
        case Types.TYPE_NUMBER:
            return "NUMBER";
        case Types.TYPE_BYTEARRAY:
            return "BYTEARRAY";
        case Types.TYPE_NUMERIC:
            return "NUMERIC";
        case Types.TYPE_PACKDECIMAL:
            return "PACKDECIMAL";
        case Types.TYPE_OBJECT:
            return "OBJECT";
        case Types.TYPE_UNKNOWN:
            return "UNKNOWN";
        case Types.TYPE_BOOLEAN:
            return "BOOLEAN";
        default:
            throw new RuntimeException("알 수 없는 타입을 지정하였습니다. - " + typeId);
        }
    }

    public static int getNodeTypeId(String nodeTypeName) {
        if ("ATTR".equals(nodeTypeName)) {
            return Item.NODE_ATTR;
        } else if ("FIELD".equals(nodeTypeName)) {
            return Item.NODE_FIELD;
        } else if ("GROUP".equals(nodeTypeName)) {   // 배열 구조는 아니면서 계층 구조를 표현하기 위한 항목
            return Item.NODE_GROUP;
        } else if ("MESSAGE".equals(nodeTypeName)) {
            return Item.NODE_MESSAGE;
        // 추가된 부분
        } else if ("ARRAY".equals(nodeTypeName)) {  // 배열 구조인 경우
            return Item.NODE_GROUP;
        } else if ("COLUMN".equals(nodeTypeName)) {  // 배열구조 내의 단일 필드
            return Item.NODE_FIELD;
        } else if ("INCLUDE".equals(nodeTypeName)) { // 메시지 내의 메시지
            return Item.NODE_MESSAGE;
        }

        throw new RuntimeException("알 수 없는 노드 타입을 지정하였습니다. - " + nodeTypeName);
    }

    public static String getNodeType(int nodeType) {
        switch (nodeType) {
        case Item.NODE_ATTR:
            return "NODE_ATTR";        
        case Item.NODE_FIELD:
            return "NODE_FIELD";        
        case Item.NODE_GROUP:
            return "NODE_GROUP";
        case Item.NODE_MESSAGE:
            return "NODE_MESSAGE";
        default:
            throw new RuntimeException("알 수 없는 노드 타입을 지정하였습니다. - " + nodeType);
        }
    }

    public static int getFunctionTypeId(String functionTypeName) {
        if ("PRIMITIVE".equals(functionTypeName)) {
            return Function.TYPE_PRIMITIVE;
        } else if ("GENERIC".equals(functionTypeName)) {
            return Function.TYPE_GENERIC;
        } else if ("CONVERT".equals(functionTypeName)) {
            return Function.TYPE_CONVERT;
        } else if ("USERDEFINED".equals(functionTypeName)) {
            return Function.TYPE_USERDEFINED;
        } else if ("BUILTIN".equals(functionTypeName)) {
            return Function.TYPE_BUILTIN;
        }

        throw new RuntimeException("알 수 없는 함수 타입을 지정하였습니다. - " +
            functionTypeName);
    }
}
