package jp.ac.kyutech.mns.ist;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import jp.ac.kyutech.mns.ist.util.MyAction;
import jp.ac.kyutech.mns.ist.util.QuickMenuActionManager;

/**
 * メニューとツールバーを構成するための便利クラス
 * @author miuramo
 *
 */
public class SimpleATN_MenuToolbarAction extends QuickMenuActionManager {
//	スーパークラスでの定義
//	protected KeepOrderHash<String,ArrayList<MyAction>> menurepos; // "File"=> ArrayList
//	protected Hashtable<String,MyAction> name2act;
//	protected Hashtable<String,AbstractButton> name2tbutton;
	
	SimpleATN satn;
	JFrame jf;
	public SimpleATN_MenuToolbarAction(SimpleATN _satn){
		satn = _satn;
		jf = _satn; //attach target
		
		addAction("DB",new MyAction("Load from DB","general/Import","Load from DB"));
		//直前に追加したMyActionについて，処理を追加する
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				satn.removeAllNote(true);
				satn.openConnection();
			}
		});
		addAction("DB",new MyAction("Fix as Named Lecture","general/Bookmarks","Fix as Named Lecture"));
		//直前に追加したMyActionについて，処理を追加する
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				satn.mysqldb.fixAsNamedLecture(null);
			}
		});
		addAction("DB",new MyAction("Merge Lectures","general/Copy","Merge Lectures"));
		//直前に追加したMyActionについて，処理を追加する
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				satn.mysqldb.mergeLectures();
			}
		});
		addAction("DB",new MyAction("Delete Drawings","general/Remove","Delete Drawings"));
		//直前に追加したMyActionについて，処理を追加する
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				satn.mysqldb.deleteTemporaryDrawings();
			}
		});
//		addAction("DB",new MyAction("Change Database","_database","Change Database"));
//		//直前に追加したMyActionについて，処理を追加する
//		addHandler_Action(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				int res = JOptionPane.showConfirmDialog(satn, "現在のデータベースは "+satn.mysqldb.db+" です．\n\n変更しますか？", "データベースの変更", JOptionPane.YES_NO_OPTION);
//				if (res == JOptionPane.YES_OPTION){
//					satn.mysqldb.change_db();
//				} else {
//					res = JOptionPane.showConfirmDialog(satn, "新規データベースを作成し，sqlファイルからインポートしますか？", "インポート", JOptionPane.YES_NO_OPTION);
//					if (res == JOptionPane.YES_OPTION){
//						satn.mysqldb_root.import_file();
//					}
//				}
//			}
//		});

		addAction("File",new MyAction("Clear","general/New","Clear"));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				satn.removeAllNote(false);
			}
		});

		addAction("File",new MyAction("Open","general/Open","Open File"));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				satn.loadFromFile(null,0);
			}
		});
		addAction("File",new MyAction("Save","general/Save","Save to File"));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				satn.saveToFile(null); //nullはファイル名を指定する
			}
		});
//		addAction("File",new MyAction("FrameSave","general/SaveAs","Save Frame to File"));
//		addHandler_Action(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				satn.saveFrameToFile(null); //nullはファイル名を指定する
//			}
//		});
		addAction("File",new MyAction("Print","general/Print","Print to PDF"));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//選択した番号のシートを印刷する
				//選択したFocusFrameの内容を印刷する
				//選択したFocusFrameの内容を，1pageになるべく納めて印刷する
				satn.printToPDF();
			}
		});

		addAction("View",new MyAction("Secondary Display","development/Host","Secondary Display",true));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//ここでは記述せず，FocusFrameを表示する箇所で，チェック状態のときだけ新規フレームの場所をセカンダリ画面に移動する
				
//				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
//				satn.showSecondaryFrame(!mya.isSelected());				
			}
		});
		
		addAction("View",new MyAction("Zoom Home","navigation/Home","Zoom Home"));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				satn.upview();
//				satn.relayout(1000, "MenuToolbar.ZOOMHOME");
			}
		});

		addAction("View",new MyAction("Reload","general/Refresh","Auto Reload",true));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
//				satn.setFullScreenMode(!mya.isSelected());
			}
		});
		//初期状態は選択不可に（どちらの書き方でも可）
//		lastOne.setEnabled(false);
		name2act.get("Reload").setEnabled(false);
		
		
		addAction("View",new MyAction("ReLayoutOnNew","_seatmap_color","ReLayout on new data",true));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
//				satn.setFullScreenMode(!mya.isSelected());
			}
		});
		
		addAction("View",new MyAction("NotifyOnNew","_bulb_new","Notify on new data",true));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
//				satn.setFullScreenMode(!mya.isSelected());
			}
		});
		addAction("View",new MyAction("Landscape Mode","_landscape","Landscape Mode",true));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
				satn.setLandscapeMode(!mya.isSelected(),true,true);
			}
		});
		addAction("View",new MyAction("SheetMap","_seatmap_mono","筆席マップ",true));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
//				satn.relayout(1000, "MenuToolbar.SheetMap");
				if (ConfigWindow.configWin.seatmap.active_smap != null)
					ConfigWindow.configWin.seatmap.active_smap.layoutLater();
			}
		});
//		addAction("View",new MyAction("BGImage","development/Applet","背景画像フォルダを設定"));
//		addHandler_Action(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
////				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
////				satn.setFullScreenMode(!mya.isSelected());
//				satn.pimagePathFinder.selectBGImagePath();
//			}
//		});
		
		
		
		addAction("Edit",new MyAction("Add Note","general/Add","Add Note"));
		//直前に追加したMyActionについて，処理を追加する
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sid = JOptionPane.showInputDialog(satn, "Input NoteID","Add Note",JOptionPane.QUESTION_MESSAGE);
				if (sid != null) {
					Note n = satn.addNote(Integer.parseInt(sid));
					if (n == null) {
						JOptionPane.showMessageDialog(satn, "Note "+sid+" already exists.", "Error", JOptionPane.ERROR_MESSAGE);
					}
					n.applySheetVisible();
					satn.applyLandscapeMode();
					satn.relayout(1000, "AddNote Button");
					satn.upview();
					satn.applyDrawingMode();
				}
			}
		});
		addAction("Edit",new MyAction("Draw","general/Edit","Draw Mode",true));
		//直前に追加したMyActionについて，処理を追加する
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
				satn.setDrawMode(!mya.isSelected());
			}
		});
		

		addAction("Help",new MyAction("Config","general/Preferences","Show Config Window",true));
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
//				SimpleATN_MenuToolbarAction.this.foldToolbarText(mya.isSelected());
				ConfigWindow.setVisible(satn, !mya.isSelected());
			}
		});

		addAction("Help",new MyAction("Web Manual","general/Help","Web Manual"));
		//直前に追加したMyActionについて，処理を追加する
		addHandler_Action(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				String url = "https://ist.mns.kyutech.ac.jp/miura/cgi-bin/fswiki/wiki.cgi/atn?page=SimpleATN%A5%DE%A5%CB%A5%E5%A5%A2%A5%EB";
				String url = "https://istlab.info/miura/simpleatn/index.html";
				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();
					}
				}
			}
		});
		//見えているものにカーソルを合わせると，情報を表示してくれるインスペクタ（デバッグ・ソースコード理解用）
//		addAction("Help",new MyAction("Inspector","general/Replace","Inspector",true));
//		addHandler_Action(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
//				satn.inspector.setInspectorMode(!mya.isSelected());
//			}
//		});
//		addAction("Help",new MyAction("Test","_ho","Test",true));
//		addHandler_Action(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
////				MyAction mya = (MyAction) e.getSource();//トグル状態を教えてくれるMyAction
////				SimpleATN_MenuToolbarAction.this.foldToolbarText(mya.isSelected());
//			}
//		});
		//初期状態は選択可に（どちらの書き方でも可）
//		lastOne.setEnabled(false);
//		name2act.get("Inspector").setEnabled(true);
		
		
		
		jf.setJMenuBar(getMenuBar());
		jf.getContentPane().add(getToolBar(), BorderLayout.NORTH);
		
		getAction("NotifyOnNew").setSelected(true);
		getAction("ReLayoutOnNew").setSelected(false);
		
		
	}
	public void disableDBMenus() {
		String[] menus = {"Import from DB",
			"Fix as Named Lecture",
			"Merge Lectures",
			"Delete Drawings",
			"NotifyOnNew",
			"ReLayoutOnNew"
		};
		for(String s: menus){
			MyAction mya = getAction(s); //MySQLが停止中は、ここがnullになる
			if (mya != null) mya.setEnabled(false);
		}
		satn.setTitle(satn.getTitle()+" (File Mode)");
		satn.isNoDB = true;
	}
}
