package jp.ac.kyutech.mns.ist.util;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;

import jp.ac.kyutech.mns.ist.util.InfoGetter.OSName;

import org.apache.commons.lang.SystemUtils;

public class NetUtil {
	// simpleatn
	// satn
	// simpleatn_assoc.bat
	// http://ist.mns.kyutech.ac.jp/jws/simpleatn.jnlp
	public static void assoc(String label, String ext, String batchfilename,String javawsurl){
		Runtime runtime = Runtime.getRuntime();
		String os = System.getProperty("os.name");
		try {
			if (os.startsWith("Windows")){
				String home = System.getProperty("user.home");
				File batchf = new File(home+File.separator+batchfilename);
				if (false/*batchf.exists() && batchf.canExecute()*/){
					System.out.println(home);
				} else {
					//バッチファイルを生成する
					String[] batchcontent = {"@echo off","javaws -open %1 "+javawsurl};
					FileReadWriter.putLines(batchf.getAbsolutePath(), batchcontent);					
					runtime.exec("cmd.exe /c assoc ."+ext+"="+label);
					runtime.exec("cmd.exe /c ftype "+label+"="+batchf.getAbsolutePath()+" \"%1\"");
				}
			} else if (os.startsWith("Mac OS")){
				runtime.exec("open ");
			} else {
				runtime.exec("acroread ");
			}
		}
		catch(java.io.IOException ex){
			ex.printStackTrace();		
		}
	}
	public static void openPDF(String path){
		Runtime runtime = Runtime.getRuntime();
		String os = System.getProperty("os.name");

		try {
			if (os.startsWith("Windows"))
				runtime.exec("cmd.exe /c start " + path+"");
			else if (os.startsWith("Mac OS")){
				runtime.exec("open " + path + "");
			} else {
				runtime.exec("acroread "+path);
			}
		}
		catch(java.io.IOException ex){
			ex.printStackTrace();		
		}
	}
	
	public static void openpath(String path){
		if (Desktop.isDesktopSupported()) {
		    try {
				Desktop.getDesktop().open(new File(path));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
//		Runtime runtime = Runtime.getRuntime();
//		String os = System.getProperty("os.name");
//		try {
//			if (os.startsWith("Windows")){
//				runtime.exec("cmd.exe /c start explorer "+path);
//			} else if (os.startsWith("Mac OS")){
//				runtime.exec("open " + path + "");
//			} else {
//			}
//		}
//		catch(java.io.IOException ex){
//			ex.printStackTrace();		
//		}
	}
	public static void exec(String cmd){
		Runtime runtime = Runtime.getRuntime();
		String os = System.getProperty("os.name");
		try {
			if (os.startsWith("Windows")){
				runtime.exec(cmd);
			} else if (os.startsWith("Mac OS")){
//				runtime.exec("open " + path + "");
			} else {
			}
		}
		catch(java.io.IOException ex){
			ex.printStackTrace();		
		}
	}
	/**
	 * OS名を取得する。<
	 * @return OS名
	 */
	public static final OSName getOsName() {
		if ( SystemUtils.IS_OS_AIX ) {
			return OSName.AIX;
		} else if ( SystemUtils.IS_OS_HP_UX ) {
			return OSName.HP_UX;
		} else if ( SystemUtils.IS_OS_IRIX ) {
			return OSName.IRIX;
		} else if ( SystemUtils.IS_OS_LINUX ) {
			return OSName.LINUX;
		} else if ( SystemUtils.IS_OS_MAC ) {
			return OSName.MAC;
		} else if ( SystemUtils.IS_OS_MAC_OSX ) {
			return OSName.MAC_OSX;
		} else if ( SystemUtils.IS_OS_OS2 ) {
			return OSName.OS2;
		} else if ( SystemUtils.IS_OS_SOLARIS ) {
			return OSName.SOLARIS;
		} else if ( SystemUtils.IS_OS_SUN_OS ) {
			return OSName.SUN_OS;
		} else if ( SystemUtils.IS_OS_WINDOWS ) {
			return OSName.WINDOWS;
		}
		return OSName.UNKNOWN;
	}
	public static boolean ping_reach(String hostip){
		InetAddress inetAddress = null;
		try {
			inetAddress = InetAddress.getByName( hostip );
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		// isReachableメソッドでpingが実現できます。引数はタイムアウト(ミリ秒指定)。
		boolean pingdata = false;
		try {
			pingdata = inetAddress.isReachable( 1000 );
			System.out.println(hostip+" 結果:" + pingdata );
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pingdata;
	}
	public static boolean ping(String hostip) {
		Runtime runtime = Runtime.getRuntime();
		String os = System.getProperty("os.name");
		try {
			if (os.startsWith("Windows")){
				// Windows の場合
				String[] command = {"ping", "-n", "1", "-w", "1000", hostip};
				try {
					return new ProcessBuilder(command).start().waitFor() == 0;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} else if (os.startsWith("Mac OS")){
				//						runtime.exec("open " + path + "");
			} else {
				// Linux の場合
				String[] command = {"ping", "-c", "1", "-t", "1000", hostip};
				try {
					return new ProcessBuilder(command).start().waitFor() == 0;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		catch(java.io.IOException ex){
			ex.printStackTrace();		
		}
		return false;
	}
	
	public static void openUrl(String url){
		if (Desktop.isDesktopSupported()){
			try {
				Desktop.getDesktop().browse(new URI(url));
				//				System.out.println("Desktop Support");
			} catch (IOException e2) {
				e2.printStackTrace();
			} catch (URISyntaxException e3) {
				e3.printStackTrace();
			}
		} else {
			Runtime runtime = Runtime.getRuntime();
			try{
				runtime.exec("cmd.exe /c start " + url);
			}catch(IOException ex){
				ex.printStackTrace();
			}
		}
	}

	public static final String getOsNameString() {
		if ( SystemUtils.IS_OS_AIX ) {
			return "AIX";
		} else if ( SystemUtils.IS_OS_HP_UX ) {
			return "HP_UX";
		} else if ( SystemUtils.IS_OS_IRIX ) {
			return "IRIX";
		} else if ( SystemUtils.IS_OS_LINUX ) {
			return "LINUX";
		} else if ( SystemUtils.IS_OS_MAC ) {
			return "MAC";
		} else if ( SystemUtils.IS_OS_MAC_OSX ) {
			return "MAC_OSX";
		} else if ( SystemUtils.IS_OS_OS2 ) {
			return "OS2";
		} else if ( SystemUtils.IS_OS_SOLARIS ) {
			return "SOLARIS";
		} else if ( SystemUtils.IS_OS_SUN_OS ) {
			return "SUN_OS";
		} else if ( SystemUtils.IS_OS_WINDOWS ) {
			return "WINDOWS";
		}
		return "UNKNOWN";
	}
	public static String getUid(){
		return System.getProperty("user.name");
	}
	
	public static ArrayList<String> getHostIps(){
		//IPの取得
		ArrayList<String> ips = new ArrayList<String>();
		Enumeration<NetworkInterface> enuIfs = null;
		try {
			enuIfs = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e1) {
			e1.printStackTrace();
		}
		if (null != enuIfs){
			while (enuIfs.hasMoreElements()) {
				NetworkInterface ni = (NetworkInterface)enuIfs.nextElement();
				Enumeration<InetAddress> enuAddrs = ni.getInetAddresses();
				while (enuAddrs.hasMoreElements()) {
					InetAddress in4 = (InetAddress)enuAddrs.nextElement();
					if (in4.getHostAddress().indexOf(":")>0) continue; //IPV6 address
					if (!in4.getHostAddress().equals("127.0.0.1")){
						ips.add(in4.getHostAddress());
					}
				}
			}
		}
		return ips;
	}
	public static void pingStdOut(String string) {
		System.out.println(string+" : "+NetUtil.ping(string));
	}
}
