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

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import jp.ac.kyutech.mns.ist.ConfigPanel_Seatmap;
import jp.ac.kyutech.mns.ist.SimpleATN;
import jp.ac.kyutech.mns.ist.util.PrintPrintable;
import edu.umd.cs.piccolo.PNode;

public class SheetPrinter extends JFrame {
	private static final long serialVersionUID = 5548394183560045355L;
	SheetPrinterCanvas canvas;

	ArrayList<MyTextField> fields;
	JFrame setupFrame;
	ATNProperty prop;

	JCheckBoxMenuItem debug;

	ArrayList<PNode> nonPrintableNodes;

	ConfigPanel_Seatmap smap;
	public SheetPrinter(SimpleATN satn, ConfigPanel_Seatmap smap){
		super("Sheet Printer");
		prop = new ATNProperty();
		this.smap = smap;

		canvas = new SheetPrinterCanvas(this, satn);
		getContentPane().add(canvas);
		canvas.updateFocusPath(prop);
		fields = new ArrayList<MyTextField>();

		initMenu();

		pack();
		validate();
		setVisible(true);
		setState(JFrame.ICONIFIED);

//		setSize(592,876);
//		setSize(592,880);
		canvas.fit();

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	String[] target = new String[]{"top","bottom","left","right"};
	String[] targetpos = new String[]{BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.WEST, BorderLayout.EAST};

	public void initMenu(){
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");

		JMenuItem setup = new JMenuItem("余白の設定");
		setup.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				showSetupWindow();
			}		
		});
		fileMenu.add(setup);

		JMenuItem printpreview = new JMenuItem("印刷領域の確認");
		printpreview.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
//				canvas.setPreferredSize(new Dimension(584,816));
//				setSize(592,880);
				pack();
				validate();
				canvas.fit();
			}		
		});
		fileMenu.add(printpreview);

//		JMenuItem wholetest = new JMenuItem("(wholetest)");
//		wholetest.addActionListener(new ActionListener(){
//		public void actionPerformed(ActionEvent arg0) {
////		setSize(592,880);
//		pack();
//		validate();
//		canvas.fullFit();
//		}		
//		});
//		fileMenu.add(wholetest);

		JMenuItem print = new JMenuItem("印刷");
		print.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				doPrint();
			}		
		});
		fileMenu.add(print);


		JMenuItem exit = new JMenuItem("閉じる");
		exit.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}		
		});
		fileMenu.add(exit);

		debug = new JCheckBoxMenuItem("デバッグ位置あわせ");
		debug.addChangeListener(new ChangeListener(){
			public void stateChanged(ChangeEvent arg0) {
				System.out.println("changed to "+debug.isSelected());
				canvas.debugPath.setVisible(debug.isSelected());
			}
		});
		fileMenu.addSeparator();
		fileMenu.add(debug);

		menuBar.add(fileMenu);

		setJMenuBar(menuBar);


		setupFrame = new JFrame("印刷設定");
		int idx = 0;
		for(String t:target){
			MyTextField jtf = new MyTextField("printmargin_"+t, prop);
			fields.add(jtf);
			JPanel jp = new JPanel();
			jp.add(jtf);
			jp.setBorder(BorderFactory.createTitledBorder(t));
			setupFrame.add(jp, targetpos[idx]);
			idx++;
		}
		JButton apply = new JButton("設定する");
		setupFrame.add(apply, BorderLayout.CENTER);
		setupFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		apply.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				for(MyTextField tf: fields){
					tf.apply(prop);
				}
				setupFrame.setVisible(false);
				canvas.updateFocusPath(prop);
				canvas.fit();

				SheetPrinter.this.setState(JFrame.NORMAL);
				SheetPrinter.this.doPrint();
			}
		});
		setupFrame.pack();
		setupFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	}
	public void doPrint(){
		nonPrintableNodes = new ArrayList<PNode>();
		if (!debug.isSelected()){
//			canvas.sheet.backgroundImg.setVisible(false);
			canvas.focusPath.setVisible(false);
		}
//		for (Object o : canvas.sheet.studentpanel.getChildrenReference()) {
//			if (o instanceof RegionPath){
//				RegionPath rp = (RegionPath)o;
//				if (rp.recogClassName.equals("Null")||rp.recogClassName.equals("Mask")){
//					nonPrintableNodes.add((PNode) rp);
//					rp.setVisible(false);
//				}
//			}
//		}

		PrintPrintable.print(canvas.cam);
		postPrint();
	}
	public void postPrint(){
//		canvas.sheet.backgroundImg.setVisible(true);
		canvas.focusPath.setVisible(true);
		for(PNode pn: nonPrintableNodes){
			pn.setVisible(true);
		}
	}

	public void showSetupWindow(){
		for(MyTextField tf: fields){
			tf.load(prop);
		}
		setupFrame.setVisible(true);
	}
	public String getPrintMarginInfo() {
		StringBuffer sb = new StringBuffer();
		sb.append("上部："+prop.printmargin_top+"  左："+prop.printmargin_left+"  右："+prop.printmargin_right+"  下部："+prop.printmargin_bottom);
		return sb.toString();
	}
}

class MyTextField extends JTextField {
	private static final long serialVersionUID = 1L;
	String target;
	public MyTextField(String t, ATNProperty sp){
		super(7);
		target = t;

		load(sp);
	}
	public void load(ATNProperty sp){
		Field f;
		try {
			f = sp.getClass().getField(target);
			setText(String.valueOf(f.get(sp)));
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	public void apply(ATNProperty sp){
		Field f;
		try {
			f = sp.getClass().getField(target);
			f.set(sp, Double.parseDouble(getText()));
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
}
