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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;


import org.apache.commons.lang.SystemUtils;


public class InfoGetter {
	public String uid;
	public OSName os;
	public String name;
	public String hostip;


	public int id;
	public int lecture_id;
	public String last;
	public String first;

	public int seatnum;

	public boolean startNetService = true;
	public static String PROTOCOL="http://";
	public static String SERVER="ist.mns.kyutech.ac.jp";
	public String passHash=null;
	Poster poster = new Poster();
	
	String last_inputted_report_src_id = null;


	/**OSの種別*/
	public enum OSName {
		/**AIX*/
		AIX,
		/**HP_UX*/
		HP_UX,
		/**IRIX*/
		IRIX,
		/**LINUX*/
		LINUX,
		/**MAC*/
		MAC,
		/**MAC_OSX*/
		MAC_OSX,
		/**OS2*/
		OS2,
		/**SOLARIS*/
		SOLARIS,
		/**SUN_OS*/
		SUN_OS,
		/**WINDOWS*/
		WINDOWS,
		/**不明*/
		UNKNOWN
	}

	/**
	 * 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 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 InfoGetter(){
		getUid();
		getHostIp();
		getOs();
		System.out.println(getOsNameString());
	}

	public void getUid(){
		uid = System.getProperty("user.name");
	}
	public void getOs(){
		os = getOsName();
	}

	public void checkHomeDirState(){
		//statコマンドで，ホームディレクトリのモードを得る
		String command = "/usr/bin/stat /home/t/"+uid+" -c %a ";
		//		String command = "/home/t/miura/bin/myfinger";
		//		System.out.println("command: "+command);
		Process process = null;
		try {
			process = Runtime.getRuntime().exec(command);
		} catch (IOException e1) {
			e1.printStackTrace();
			return;
		}
		InputStream is = process.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line;
		try {
			while ((line = br.readLine()) != null) {
				System.out.println(line);
				if (!line.startsWith("700") ){ //TODO:miuraなら，試験モードにしない
					if (!uid.equals("miura")) siken_mode(); else System.out.println("miuraなら，試験モードにしない");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void siken_mode(){
		String command2 = "/edu/bin/siken-mode";
		//		String command = "/home/t/miura/bin/myfinger";
		//		System.out.println("command2: "+command2);
		Process process2 = null;
		try {
			process2 = Runtime.getRuntime().exec(command2);
		} catch (IOException e1) {
			e1.printStackTrace();
			return;
		}
		InputStream is = process2.getInputStream();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String line=null;
		try {
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String capitalize(String s) {
		if (s.length() == 0) return s;
		return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
	}

	public String getHostIp(){
		//IPの取得
		StringBuffer sb = new StringBuffer();
		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")){
						if (sb.length()>0) sb.append("/");
						sb.append(in4.getHostAddress());
					}
				}
			}
		}
		return sb.toString();
	}

	public String toString(){
		return (uid+" "+name+" "+hostip+" "+id+" "+lecture_id+" "+last+" "+first);
	}
}
