package com.baosight.hdsdk.example;

import java.io.PrintStream;
import java.util.List;
import java.util.Scanner;

import com.baosight.hdsdk.HDConnectionFactory;
import com.baosight.hdsdk.HDServerFactory;
import com.baosight.hdsdk.HDTagManager;
import com.baosight.hdsdk.domain.data.HDBasicTag;
import com.baosight.hdsdk.domain.data.HDDataConnection;
import com.baosight.hdsdk.exception.HDSdkException;

/**
 * 该类为实时数据库JavaSDK的示例程序
 * 
 * @author kongxiaoyang 
 *
 */
public class Demo {

	private static final String NEW_LINE = System.getProperty("line.separator");
	
	private final String HD_JAVASDK_VERSION = "3.6";
	
	private String mainpage = 
		"----------Welcome to HDJavaSDK Demo V=" + HD_JAVASDK_VERSION + "---------------------" + NEW_LINE
		+ "Please input the number for operation : " + NEW_LINE
		+ "1. Connect to Server" + NEW_LINE
		+ "2. Get Group Node from ResMgrSrv(not support!)" + NEW_LINE
		+ "3. Get TagInfo from iHyperDB server" + NEW_LINE
		+ NEW_LINE + "'q/Q' for quit." + NEW_LINE;
	
	/**
	 * 全局输入扫描器
	 */
	private Scanner scanner = new Scanner(System.in);
	
	private PrintStream writer = System.out;
	
	private HDDataConnection dataconn = null;
	//private HDResourceConnection resconn = null;
	
	
	/**
	 * 该程序也可以作为HDJavaSDK测试使用
	 * 通过命令 java -jar hd-datasdk-x.x.x.jar com.baosight.hdsdk.example.Demo
	 * 进行启动
	 */
	
	/**
	 * 主程序
	 * @param args
	 */
	public static void main(String[] args) {
		Demo hdSDKDemo = new Demo();
		hdSDKDemo.start();
	}
	
	/**
	 * 启动
	 *
	 */
	void start() {
		while(true) {
			//主循环
			showMainMenu();
			String userCommand = getUserInput();
			if (userCommand.toLowerCase().equals("q")) {
				//quit
				
				System.exit(0);
			}
			int commandSwitch = 0;
			try {
				commandSwitch = Integer.parseInt(userCommand);
			} catch (NumberFormatException e) {
				System.err.println("Input wrong!");
				continue;
			}
			
			switch (commandSwitch) {
			case 1:
				//connection
				enterConnectionPage();
				break;
			case 2:
				//group
				//enterGroupNodePage();
				break;
			case 3:
				//get taginfo
				enterTagInfoPage();
				break;
			default:
				System.err.println("Input wrong");
				break;
			}
		}
	}
	
	/**
	 * 展示主菜单
	 *
	 */
	void showMainMenu() {
		System.out.println(mainpage);
	}
	
	/**
	 * 获取Tag信息
	 *
	 */
	void enterTagInfoPage() {
		if (dataconn == null) {
			writer.println("please connect to data server first!");
			return;
		}
		writer.println("please input the tagname :");
		String tagName = getUserInput();
		HDTagManager tagMgr = dataconn.getTagManager();
		try {
			HDBasicTag bTag = tagMgr.getBasicTagByName(tagName);
			writer.println("get taginfo ok");
			writer.println(bTag.toString());
		} catch (HDSdkException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	/**
	 * 进入连接页面
	 *
	 */
	void enterConnectionPage() {
		System.out.println("please choose server type 1 = iHyperDB-ConfigCenter; 2 = iHyperDB-ResServer(not support)" + NEW_LINE);
		String input = getUserInput();
		int command;
		try {
			command = Integer.parseInt(input);
		} catch (NumberFormatException e) {
			System.err.println("input wrong");
			return;
		}
		
		switch (command) {
		case 1:
			//connect to iHD server
			writer.println("please input server ip");
			String serverip = getUserInput();
			writer.println("please input server port");
			int serverport = Integer.parseInt(getUserInput());
			try {
					dataconn = 
						HDConnectionFactory.getHDDataConnection(HDServerFactory.getHDDataServer(serverip, serverport));
					writer.println("connect OK!");
					System.out.println(dataconn.toString());
				} catch (HDSdkException e) {
					e.printStackTrace();
					System.out.println("connect failed");
				}
			break;

		case 2:
			//connection to Res server
			writer.println("not support!");			
			break;
		default:
			writer.println("input wrong");
			break;
		}
	}
	
	/**
	 * 该方法从控制台获取用户输入，阻塞进程
	 * @return
	 */
	String getUserInput() {
		
		System.out.println("please input : ");
		String userInput = this.scanner.nextLine();
		return userInput;
	}
}
