package flow;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.sound.midi.VoiceStatus;

import com.avaya.sce.runtime.Prompt;
import com.avaya.sce.runtime.PromptElement;
import com.avaya.sce.runtime.internal.PromptFinder;
import com.avaya.sce.runtimecommon.IVariableField;
import com.avaya.sce.runtimecommon.SCESession;

/**
 * Class that represents a menu. A menu can contain prompts, choices and links
 * as well as handlers for various events that may occur while the menu is
 * executing Last generated by Orchestration Designer at: 2021年5月25日 上午08时56分48秒
 */
public class menu1 extends com.avaya.sce.runtime.Menu {

	// {{START:CLASS:FIELDS
	// }}END:CLASS:FIELDS

	/**
	 * Default constructor Last generated by Orchestration Designer at:
	 * 2021年5月25日 上午08时56分48秒
	 */
	public menu1() {
		// {{START:CLASS:CONSTRUCTOR
		super();
		// }}END:CLASS:CONSTRUCTOR
	}

	/**
	 * This method is generated automatically and should not be manually edited.
	 * To manually edit the links for the node, override:
	 *     void updateLinks(Collection links, SCESession mySession)
	 * Last generated by Orchestration Designer at: 2023年4月14日 下午05时14分30秒
	 * @return a collection of links
	 */
	public java.util.Collection getLinks(com.avaya.sce.runtimecommon.SCESession mySession) {
		com.avaya.sce.runtime.Link link;
		java.util.List list;
		java.util.Collection grammarInfo = null;
		java.util.Collection<com.avaya.sce.runtime.CaptureExpression> captureExpr = null;
		// This item does not have any defined links
		list = new java.util.ArrayList();
		String ___tempGrammarName = null;

		return(list);
	}

	/**
	 * This method is generated automatically and should not be manually edited.
	 * To manually edit the properties for the node, override:
	 *     void updateProperties(Collection properties, SCESession mySession)
	 * Last generated by Orchestration Designer at: 2023年4月14日 下午05时14分30秒
	 * @return a collection of properties
	 */
	public java.util.Collection getProperties(com.avaya.sce.runtimecommon.SCESession mySession) {
		com.avaya.sce.runtime.Property property;
		java.util.List list;
		// This item does not have any defined properties
		list = new java.util.ArrayList();

		return(list);
	}

	/**
	 * This method is generated automatically and should not be manually edited.
	 * To manually edit the event handlers for the node, override:
	 *    void updateEvents(Collection events, SCESession mySession)
	 * Last generated by Orchestration Designer at: 2023年4月14日 下午05时14分30秒
	 * @return a collection of Events
	 */
	public java.util.Collection getEvents(com.avaya.sce.runtimecommon.SCESession mySession) {
		java.util.List list;
		com.avaya.sce.runtime.Event event;
		list = new java.util.ArrayList(3);
		java.util.List eventPromptNames = null;
		String ___tempPromptName = null;

		// Item has no prompts associated
		eventPromptNames = new java.util.ArrayList();

		event = new com.avaya.sce.runtime.Event(com.avaya.sce.runtimecommon.SCERT.EVENT_NOINPUT, (com.avaya.sce.runtime.PromptRefInfo[])eventPromptNames.toArray(new com.avaya.sce.runtime.PromptRefInfo[0]), new com.avaya.sce.runtime.Goto("ser_getMenuResult", 0, true, ""));
		list.add(event);

		// Item has no prompts associated
		eventPromptNames = new java.util.ArrayList();

		event = new com.avaya.sce.runtime.Event(com.avaya.sce.runtimecommon.SCERT.EVENT_NOMATCH, (com.avaya.sce.runtime.PromptRefInfo[])eventPromptNames.toArray(new com.avaya.sce.runtime.PromptRefInfo[0]), new com.avaya.sce.runtime.Goto("ser_getMenuResult", 0, true, ""));
		list.add(event);
		event = new com.avaya.sce.runtime.OnDisconnect("ser_getMenuEnd", false);
		list.add(event);
		return(list);
	}

	/**
	 * Builds the list of choices for the menu.  This list is built
	 * automatically by the code generator and should not be edited
	 * manually.  Future code generation may overwrite any changes
	 * to this method.<br>
	 * To modify the list of choices, override:<br>
	 *    updateChoices(Collection choices, SCESession mySession)
	 * Last generated by Orchestration Designer at: 2023年4月14日 下午05时14分30秒
	 */
	public java.util.Collection getChoices(com.avaya.sce.runtimecommon.SCESession mySession) {
		java.util.List list = null;
		com.avaya.sce.runtime.Choice choice = null;
		java.util.Collection grammarInfo = null;
		list = new java.util.ArrayList(1);
		String ___tempGrammarName = null;

		// build the list of grammar information objects for the choice
		grammarInfo = new java.util.ArrayList();


		choice = new com.avaya.sce.runtime.Choice("ch1", "1", true, "exact", "ser_getMenuResult", grammarInfo, true);
		list.add(choice);

		return(list);
	}

	@Override
	public void updatePrompts(Collection prompts, SCESession mySession) {
		System.out.println("进入menu1");
		prompts.clear();
		super.updatePrompts(prompts, mySession);
		Prompt pmpt = (Prompt) PromptFinder.load("pro_menutemplete",
				mySession.getCurrentLanguage(), mySession);
		int wavCount = 0;
		try {
			String NextNodeID = mySession
					.getVariableField(IProjectVariables.NEXT_MENU).getStringValue();
//			String NextNodeID = mySession.getVariableField(
//					IProjectVariables.NEXT_MENU).getStringValue();
			System.out.println("menu1:NextNodeID:"+NextNodeID);
			String next_node = "end";
			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 (hMapChild.containsKey(NextNodeID)) {
				ArrayList<CMenuItemConfig> list = hMapChild.get(NextNodeID);
				System.out.println("-----hMapChild.size():----------"
						+ list.size());
				for (int i = 0; i < list.size(); i++) {
					java.util.Collection grammarInfo = new java.util.ArrayList();
					CMenuItemConfig CMenuItemConfig = list.get(i);
					// System.out.println(CMenuItemConfig.PlayItem);
					String ID = CMenuItemConfig.getMenuItemId();
					int NodeType = CMenuItemConfig.getPlayType();
					String Filename = CMenuItemConfig.getPlayItem();
					
					if (Filename==null||Filename.equals("")) {
						continue;
					}
					String DTMF = CMenuItemConfig.getDTMF();
					int Enable = CMenuItemConfig.getBEnable();
					if (Enable == 1) {
						String subfile = Filename;
						System.out.println("动态拼接菜单："+CMenuItemConfig);
						if (NodeType == 1) {
							System.out.println("语音文件：" + subfile);
							PromptElement promptElement2 = Util.GetFileElement(
									subfile, wavCount, mySession);
							pmpt.add(1, promptElement2);
							wavCount++;
							if(DTMF==null||DTMF.equals(""))
							{
								continue;
							}
							else if (DTMF.equals("*")) {
								PromptElement promptElementStar = Util.GetFileElement(
										"xinghaojian", wavCount, mySession);
								pmpt.add(1, promptElementStar);
								wavCount++;
							} else if (DTMF.equals("#")) {
								System.out.println("#");
								PromptElement promptElementJing = Util.GetFileElement(
										"jing", wavCount, mySession);
								pmpt.add(1, promptElementJing);
								wavCount++;
							} else if (DTMF.equals("")) {
							} else {
								
								PromptElement promptElementDTMF = Util.GetFileElement(
										DTMF, wavCount, mySession);
								pmpt.add(1, promptElementDTMF);
								wavCount++;
							}
							if (wavCount >= 40) {
								break;
							}
						} 
						else if(NodeType == 3)
						{
							System.out.println("语音文件：" + subfile);
							PromptElement promptElement2 = Util.GetFileElement(
									subfile, wavCount, mySession);
							pmpt.add(1, promptElement2);
							wavCount++;
						}
						else {
							PromptElement textele2 = Util
									.getTextElement(subfile);
							pmpt.add(1, textele2);
							wavCount++;
							if(DTMF==null||DTMF.equals(""))
							{
								continue;
							}
							if (DTMF.equals("*")) {
								PromptElement texteleStar = Util
										.getTextElement("星号键");
								pmpt.add(1, texteleStar);
								wavCount++;
							} else if (DTMF.equals("#")) {
								PromptElement texteleJing = Util
										.getTextElement("警号键");
								pmpt.add(1, texteleJing);
								wavCount++;
							} else if (DTMF.equals("")) {
							} else {
								PromptElement texteleDtmf = Util
										.getTextElement(DTMF);
								pmpt.add(1, texteleDtmf);
								wavCount++;
							}

							if (wavCount >= 40) {
								break;
							}
//							System.out.println(pmpt);
						}

					}

				}
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
			//e.printStackTrace();
		}

		prompts.add(pmpt);

		super.updatePrompts(prompts, mySession);
	}

	public void updateChoices(Collection choices, SCESession mySession) {
		String NextNodeID = mySession
				.getVariableField(IProjectVariables.NEXT_MENU).getStringValue();
		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 (hMapChild.containsKey(NextNodeID)) {
			ArrayList<CMenuItemConfig> list = hMapChild.get(NextNodeID);
			com.avaya.sce.runtime.Choice choice = null;
			for (int i = 0; i < list.size(); i++) {
				java.util.Collection grammarInfo = new java.util.ArrayList();
				CMenuItemConfig CMenuItemConfig = list.get(i);
				String DTMF = CMenuItemConfig.getDTMF();
				int enable = CMenuItemConfig.getBEnable();
				if (DTMF==null||DTMF.equals("") || enable == 0) {
					continue;
				}
//				System.out.println("动态菜单：" + list.get(i));
				choice = new com.avaya.sce.runtime.Choice("Menu" + i, DTMF,
						true, "exact", "ser_getMenuResult", grammarInfo, true);
				choices.add(choice);
			}
		}

		super.updateChoices(choices, mySession);
	}

	/**
	 * Builds the list of prompts that are used by this flow item<br>
	 * This method is generated automatically and changes to it may
	 * be overwritten next time code is generated.  To modify the list
	 * of prompts for the flow item, override:
	 *     updatePrompts(Collection prompts, SCESession mySession)
	 * @return list of prompts for the menu
	 * Last generated by Orchestration Designer at: 2023年4月14日 下午05时14分30秒
	 */
	public java.util.Collection getPrompts(com.avaya.sce.runtimecommon.SCESession mySession) {
		java.util.List list = null;
		String ___tempPromptName = null;
		// Item has no prompts associated
		list = new java.util.ArrayList();

		return(list);
	}

	/**
	 * Builds the list of tracking items that are generated by this flow item<br>
	 * This method is generated automatically and changes to it may
	 * be overwritten next time code is generated.
	 * @return list of tracking items for the item
	 * Last generated by Orchestration Designer at: 2023年4月14日 下午05时14分30秒
	 */
	public java.util.Collection getTrackingActions(com.avaya.sce.runtimecommon.SCESession mySession) {
		java.util.List list = null;

		// Item has no tracking items.
		list = new java.util.ArrayList();
		return(list);
	}
	public static void main(String[] args) {
		try {
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
