package flow;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.avaya.sce.runtime.PromptElement;
import com.avaya.sce.runtimecommon.IVariableField;
import com.avaya.sce.runtimecommon.SCESession;

public class Util {
	private static Logger log = Logger.getLogger(Util.class);
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//InitMenu();

	}

	public static PromptElement getTextElement(String TXT) {
		try {
			return new com.avaya.sce.runtime.PromptElement(
					com.avaya.sce.runtime.PromptElement.TEXT, TXT, null);

		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	public static PromptElement GetFileElement(String subfile, int wavCount,
			SCESession mySession) {
		com.avaya.sce.runtime.Format format1 = new com.avaya.sce.runtime.Format();
		format1.add(com.avaya.sce.runtime.Format.FORMAT,
				com.avaya.sce.runtime.Format.FMT_FILENAME);
		PromptElement promptElement2 = new PromptElement(0, "aaa", format1);
		mySession.getVariableField("FilePath", "path" + wavCount).setValue(
				subfile + ".wav");
		System.out.println(mySession.getVariableField("FilePath", "path" + wavCount).getStringValue());
		promptElement2.setWaveFile(true);
		promptElement2.setType(PromptElement.VARIABLE_TEXT);
		promptElement2.setValue("FilePath:path" + wavCount);
		return promptElement2;

	}

	public static void InitMenu(com.avaya.sce.runtimecommon.SCESession mySession) {
		Connection conn = null;
		try {
			String type = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_TYPE).getStringValue();
			String driver = null;
			String user = null;
			String password = null;
			String url = null;
			// 若安装多个实例时，要确认相应的port及实例名
			if(type.equals("0")){
				log.debug("-----当前数据库为测试库-----");
				driver = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_DRIVER_2).getStringValue();
				Class.forName(driver);
				user = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_USER_2).getStringValue();
				password = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_PASSWORD_2).getStringValue();
				url = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_URL_2).getStringValue();
			}else {
				log.debug("-----当前数据库为正式库-----");
				driver = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_DRIVER_1).getStringValue();
				Class.forName(driver);
				user = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_USER_1).getStringValue();
				password = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_PASSWORD_1).getStringValue();
				url = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_URL_1).getStringValue();
			}
			log.debug("-----初始化数据库基础参数   URL:"+url+"   ,USER:"+user+"   ,password:"+password);
			//String DBType = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_TYPE).getStringValue();
			//获取目录表名称
			String catalogue = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_TABLE_1).getStringValue();
			//获取细节节点表名称
			String details = mySession.getVariableField(IProjectVariables.DB,IProjectVariables.DB_FIELD_TABLE_2).getStringValue();
			log.debug("-----初始化数据库基础参数对应表名称   目录表名称:"+catalogue+"   ,细节节点表名称:"+details);
			DriverManager.setLoginTimeout(5);
			conn = DriverManager.getConnection(url, user, password);
			System.out.println("链接成功");
			//String sql = "SELECT a.[MenuID] parentMenuID,a.[MenuName] parentMenuName,a.[MenuType] parentMenuType,a.[NextMenu] parentNextMenu,a.[Remark] parentRemark,a.BEnable parentBEnable,a.MenuErrorMaxCount,a.Strategy,a.NodeType,b.[RowIDs],b.[ParentMenuID],b.[MenuItemID],b.[PlayType],b.[PlayItem],b.[BEnable],b.[MenuOrder],B.[TransferNO] ,b.[NextMenu],b.[DTMF] ,b.[Remark],b.[SMSTemplate],b.ItemMenuName,b.SonNodeType FROM "+catalogue+" a left join "+details+" b on a.MenuID=b.ParentMenuID order by b.ParentMenuID,MenuOrder";
			String sql = "SELECT A.MENUID PARENTMENUID,A.MENUNAME PARENTMENUNAME,A.MENUTYPE PARENTMENUTYPE,A.NEXTMENU PARENTNEXTMENU,A.REMARK PARENTREMARK,A.BENABLE PARENTBENABLE,A.MENUERRORMAXCOUNT,A.STRATEGY,A.NODETYPE,B.ROWIDS AS ROWIDSDH,B.PARENTMENUID,B.MENUITEMID,B.PLAYTYPE,B.PLAYITEM,B.BENABLE,B.MENUORDER,B.TRANSFERNO ,B.NEXTMENU,B.DTMF ,B.REMARK,B.SMSTEMPLATE,B.ITEMMENUNAME,B.SONNODETYPE FROM "+catalogue+" A LEFT JOIN "+details+" B ON A.MENUID=B.PARENTMENUID ORDER BY B.PARENTMENUID,MENUORDER";
			
			log.debug("-----初始化数据库基础参数   对应SQL语句:"+sql);
			PreparedStatement pstmt = conn.prepareStatement(sql);

			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				String parentMenuID = rs.getString("PARENTMENUID");
//				System.out.println(parentMenuID);
				String parentMenuName = rs.getString("PARENTMENUNAME");
				int parentMenuType = rs.getInt("PARENTMENUTYPE");
				String parentNextMenu = rs.getString("PARENTNEXTMENU");
				String parentRemark = rs.getString("PARENTREMARK");
				int parentBEnable = rs.getInt("PARENTBENABLE");
				int MenuErrorMaxCount= rs.getInt("MENUERRORMAXCOUNT");
				String strategy = rs.getString("STRATEGY");
				String nodeType = rs.getString("NODETYPE");
				CMenuConfig CMenuConfig = new CMenuConfig();
				CMenuConfig.setMenuID(parentMenuID);
				CMenuConfig.setMenuName(parentMenuName);
				CMenuConfig.setMenuType(parentMenuType);
				CMenuConfig.setRemark(parentRemark);
				CMenuConfig.setNextMenu(parentNextMenu);
				CMenuConfig.setMenuErrorMaxCount(MenuErrorMaxCount);
				CMenuConfig.setStrategy(strategy);
				CMenuConfig.setNodeType(nodeType);
				HashMap<String, Object>  hMapAll = (HashMap<String, Object>) mySession
						.getVariableField(IProjectVariables.MENU_ALL)
						.getObjectValue();
				HashMap<String, ArrayList<CMenuItemConfig>> hMapChild = (HashMap<String, ArrayList<CMenuItemConfig>>) mySession
						.getVariableField(IProjectVariables.MENU_CHILD)
						.getObjectValue();
				HashMap<String, CMenuConfig> hMapParent = (HashMap<String, CMenuConfig>) mySession
						.getVariableField(IProjectVariables.MENU_PARENT)
						.getObjectValue();

				if (!hMapParent.containsKey(parentMenuID)) {
					hMapParent.put(parentMenuID, CMenuConfig);
				}
				if (!hMapAll.containsKey(parentMenuID)) {
					hMapAll.put(parentMenuID, CMenuConfig);
				}
				// --------------一下处理Child节点----------------------------
				int RowID = rs.getInt("ROWIDSDH");
				String ParentMenuID = rs.getString("PARENTMENUID");
				String MenuItemID = rs.getString("MENUITEMID");
				int PlayType = rs.getInt("PLAYTYPE");
				String PlayItem = rs.getString("PLAYITEM");
				int BEnable = rs.getInt("BENABLE");
				int MenuOrder = rs.getInt("MENUORDER");
				String NextMenu = rs.getString("NEXTMENU");
				String DTMF = rs.getString("DTMF");
				String Remark = rs.getString("REMARK");
				String SMSTemplate = rs.getString("SMSTEMPLATE");
				String ItemMenuName = rs.getString("ITEMMENUNAME");
				String TransferNO = rs.getString("TRANSFERNO");
				String sonNodeType = rs.getString("SONNODETYPE");
				
				CMenuItemConfig CMenuItemConfig = new CMenuItemConfig();
				CMenuItemConfig.setBEnable(BEnable);
				CMenuItemConfig.setDTMF(DTMF);
				CMenuItemConfig.setMenuItemId(MenuItemID);
				CMenuItemConfig.setMenuOrder(MenuOrder);
				CMenuItemConfig.setNextMenu(NextMenu);
				CMenuItemConfig.setParentMenu(CMenuConfig);
				CMenuItemConfig.setPlayItem(PlayItem);
				CMenuItemConfig.setPlayType(PlayType);
				CMenuItemConfig.setRemark(Remark);
				CMenuItemConfig.setRowID(RowID);
				CMenuItemConfig.setSMSTemplate(SMSTemplate);
				CMenuItemConfig.setItemMenuName(ItemMenuName);
				CMenuItemConfig.setTransferNO(TransferNO);
				CMenuItemConfig.setSonNodeType(sonNodeType);
				if (!hMapChild.containsKey(ParentMenuID)) {
					hMapChild.put(ParentMenuID,
							new ArrayList<CMenuItemConfig>());
				}
				hMapChild.get(ParentMenuID).add(CMenuItemConfig);
				if (!hMapAll.containsKey(MenuItemID)) {
					hMapAll.put(MenuItemID, CMenuItemConfig);
				}
			}
			System.out.println("查询结束");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				conn = null;

			}

		}

	}

}
