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

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

import jp.ac.kyutech.mns.ist.SimpleATN;
import jp.ac.kyutech.mns.ist.util.MultiDisplay;
import edu.umd.cs.piccolo.PCanvas;
import edu.umd.cs.piccolo.util.PBounds;

public class WindowManagerPlugin extends DefaultPlugin implements ActionListener {
	JFrame srcjframe;
	PCanvas canvas;
	boolean isActive = false;
	public WindowManagerPlugin(PCanvas can, JFrame f){
		canvas = can;
		srcjframe = f;
	}

	public void actionPerformed(ActionEvent e){
		System.out.println(e.getActionCommand());
		if (e.getActionCommand().equals("Full on Secondary")){
			MultiDisplay.moveWindow(SimpleATN.theapp, srcjframe, true, true);
		} else if (e.getActionCommand().equals("Normal on Primary")){
			MultiDisplay.moveWindow(SimpleATN.theapp, srcjframe, false, false);
		} else if (e.getActionCommand().equals("Move to Secondary")){
			MultiDisplay.changeDisplayOfWindow(SimpleATN.theapp, srcjframe, true);
		} else if (e.getActionCommand().equals("Move to Primary")){
			MultiDisplay.changeDisplayOfWindow(SimpleATN.theapp, srcjframe, false);
		} else if (e.getActionCommand().equals("Full")){
			MultiDisplay.moveWindow(SimpleATN.theapp, srcjframe, true,true);
		} else if (e.getActionCommand().equals("Normal")){
			MultiDisplay.moveWindow(SimpleATN.theapp, srcjframe, true,false);
		}
	}
	public void addMenu(JPopupMenu menu){
		JMenu insM;

		insM = new JMenu("Window");

		ArrayList<Rectangle> dispList = MultiDisplay.getMultiDisplayRectList();
		JMenuItem mi;
		if (dispList.size()>1){			

			mi = new JMenuItem("Full on Secondary");
			mi.addActionListener(this);
			insM.add(mi);

			mi = new JMenuItem("Normal on Primary");
			mi.addActionListener(this);
			insM.add(mi);

			mi = new JMenuItem("Move to Secondary");
			mi.addActionListener(this);
			insM.add(mi);

			mi = new JMenuItem("Move to Primary");
			mi.addActionListener(this);
			insM.add(mi);
			
			mi = new JMenuItem("マルチディスプレイの意味について解説を読む");
			mi.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					JOptionPane.showMessageDialog(srcjframe, "ここでの Primary とは，SimpleATN のメインウィンドウが\n" +
							"表示されているディスプレイを意味します．\n" +
							"\n" +
							"メインウィンドウが別のディスプレイに移動すると，Primary は入れ替わります．\n" +
							"\n" +
							"Windowsのディスプレイ番号は関係ありません．");
				}
			});
			mi.addActionListener(this);
			insM.add(mi);

		} else {
			mi = new JMenuItem("Normal");
			mi.addActionListener(this);
			insM.add(mi);

			mi = new JMenuItem("Full");
			mi.addActionListener(this);
			insM.add(mi);
		}


		menu.add(insM);
	}

	//それなりにウィンドウサイズを微調整
	public void relativefit(PBounds tempAfterLayoutBounds) {
		double a = tempAfterLayoutBounds.width;
		double b = tempAfterLayoutBounds.height;
		//現在のウィンドウ面積
		Dimension s = Toolkit.getDefaultToolkit().getScreenSize().getSize();
		double sq = s.getHeight()*s.getWidth()/6;
		double r = Math.sqrt(sq)*2;
		double w = a*r/(a+b);
		double h = b*r/(a+b);
		srcjframe.setSize(new Dimension((int)w,(int)h));
		
	}
}
