package jp.ac.kyutech.mns.ist;

import java.awt.BasicStroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import jp.ac.kyutech.mns.ist.filter.RegionSS;
import jp.ac.kyutech.mns.ist.filter.RegionSSFilter;
import jp.ac.kyutech.mns.ist.util.NetUtil;
import jp.ac.kyutech.mns.ist.util.Poster;
import jp.ac.kyutech.mns.ist.util.QuickMenuActionManager;
import edu.umd.cs.piccolo.nodes.PPath;

public class ConfigPanel_LBoxTomcat extends ConfigPanel implements Runnable {
	private static final long serialVersionUID = -1308877143307915884L;
	Thread t;
	final SimpleATN satn;


	public JPanel jp;



	JLabel current;
	public ConfigPanel_LBoxTomcat(final SimpleATN satn, JFrame parent){
		super(parent);
		this.satn = satn;
		//		t = new Thread(this);
		//		t.start();
		run();

		//設定画面用の画面
		tabIcon = QuickMenuActionManager.createNavigationIcon("_tomcat"+"24");
		tabTitle = "Tomcat";

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		jp = new JPanel();
		jp.setBorder(BorderFactory.createTitledBorder("動作確認アクション"));
		//		jp.setLayout(new BorderLayout());
		JButton browse8080 = new JButton("Tomcatの動作確認Webページを開く");
		browse8080.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				NetUtil.openUrl("http://localhost:8080/PRX");
			}
		});
		jp.add(browse8080);

		JButton browse808 = new JButton("Tomcatのフォルダを開く");
		browse808.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
//				NetUtil.exec("cmd.exe /c start explorer C:\\xampp\\tomcat");
				NetUtil.openpath("C:\\xampp\\tomcat");
			}
		});
		jp.add(browse808);
		JButton browse80 = new JButton("サービスマネージャー(SCM)を開く");
		browse80.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				NetUtil.exec("cmd.exe /c start services.msc");
			}
		});
		jp.add(browse80);
		JButton browse = new JButton("Ping(0.5,0.6)");
		browse.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				NetUtil.pingStdOut("192.168.0.5");
				NetUtil.pingStdOut("192.168.0.6");
			}
		});
		jp.add(browse);
		JButton browse8 = new JButton("LBOXの時刻合わせとRegHost(0.5,0.6)");
		browse8.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String url = "http://dpgw:lbox@192.168.0.5/dpgw/reghost.php";
				NetUtil.openUrl(url);
				String url2 = "http://dpgw:lbox@192.168.0.6/dpgw/reghost.php";
				NetUtil.openUrl(url2);
			}
		});
		jp.add(browse8);
		JButton brows = new JButton("LBOX DPGW(0.5,0.6)");
		brows.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String url = "http://dpgw:lbox@192.168.0.5/dpgw/";
				NetUtil.openUrl(url);
				String url2 = "http://dpgw:lbox@192.168.0.6/dpgw/";
				NetUtil.openUrl(url2);
			}
		});
		jp.add(brows);
		JButton brow = new JButton("LBOX TOP(0.5,0.6)");
		brow.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String url = "http://192.168.0.5/";
				NetUtil.openUrl(url);
				String url2 = "http://192.168.0.6/";
				NetUtil.openUrl(url2);
			}
		});
		jp.add(brow);


		add(jp);
		update();

	}
	public JFrame findActiveParent(){
		if (frame.isShowing()) return frame;
		else return satn;
	}
	public void update(){
	}

	@Override
	public void run() {
		NetUtil.assoc("simpleatn", "satn", "simpleatn_assoc.bat", "http://ist.mns.kyutech.ac.jp/jws/simpleatn.jnlp");

		if (satn.initArgFile==null){
			Poster poster = new Poster();
			String res = null;
			try{
				res = poster.get("http://localhost:8080/PRX");
			}catch(Exception ex){
			}
			if (res==null) {
				//			int r = JOptionPane.showConfirmDialog(null, "Tomcatが起動していません．\n起動しますか？");
				//			if (r == JOptionPane.YES_OPTION){

				Runtime runtime = Runtime.getRuntime();
				String os = System.getProperty("os.name");
				try {
					if (os.startsWith("Windows")){
						//					runtime.exec("cmd.exe /c start sc start Tomcat7");
						//						runtime.exec("cmd.exe /c start services.msc");
						runtime.exec("cmd.exe /c start explorer C:\\xampp\\tomcat");
						//					JOptionPane.showMessageDialog(frame, "Tomcatを起動しました．");
						System.out.println("Tomcatを起動しました．");
					} else if (os.startsWith("Mac OS")){
						//						runtime.exec("open " + path + "");
					} else {
					}
				}
				catch(java.io.IOException ex){
					ex.printStackTrace();		
				}

				//			}
			}

			ArrayList<String> ips = NetUtil.getHostIps();
			for(String s:ips){
				System.out.println("IP Address : "+s);
				System.out.println(NetUtil.ping(s));
				if (s.startsWith("192.168.0.")){
					if (NetUtil.ping("192.168.0.5")){
						String url = "http://dpgw:lbox@192.168.0.5/dpgw/reghost.php";
						NetUtil.openUrl(url);
					}
					if (NetUtil.ping("192.168.0.6")){
						String url2 = "http://dpgw:lbox@192.168.0.6/dpgw/reghost.php";
						NetUtil.openUrl(url2);
					}
				}
				if (s.startsWith("192.168.1.")){
					if (NetUtil.ping("192.168.1.5")){
						String url = "http://dpgw:lbox@192.168.1.5/dpgw/reghost.php";
						NetUtil.openUrl(url);
					}
					if (NetUtil.ping("192.168.1.6")){
						String url2 = "http://dpgw:lbox@192.168.1.6/dpgw/reghost.php";
						NetUtil.openUrl(url2);
					}
				}
			}
		}
	}
	public void saveBeforeExit() {
	}

}
