package jp.ac.kyutech.mns.ist;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.lang.SystemUtils;

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

public class ConfigPanel_Draw extends ConfigPanel implements Runnable, SSHandler {
	private static final long serialVersionUID = -1308877143307915884L;
	Thread t;
	final SimpleATN satn;
	JTextField teacherPenID, targetMatrixSheet;
	public static Font sans30 = new Font("sansserif", Font.BOLD, 30);
	JLabel status, status2;
	/**
	 * 教師のペンID
	 */
	public int tpid = 0;
	/**
	 * 教師が書き込む学生のID
	 */
	public int target4teacher = 0;

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

		//設定画面用の画面
		tabIcon = QuickMenuActionManager.createNavigationIcon("general/Edit"+"24");
		tabTitle = "教師の書き込み";

		//		setBorder(BorderFactory.createTitledBorder("背景画像"));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));


		JPanel currentP = new JPanel();

		currentP.setBorder(BorderFactory.createTitledBorder("教員が使用するペンの設定"));
		//		currentP.setLayout(new BoxLayout(currentP, BoxLayout.Y_AXIS));
		//		currentP.setAlignmentX(CENTER_ALIGNMENT);

		currentP.add(new JLabel("教員が使用するペンの番号:"));
		teacherPenID = new JTextField("   0");
		teacherPenID.setFont(sans30);
		currentP.add(teacherPenID);

		JButton ok = new JButton("設定");
		ok.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				update();
			}
		});
		currentP.add(ok);
		add(currentP);



		status = new JLabel(" ");
		add(status);


		add(new JLabel(" "));
		add(new JLabel(" "));
		add(new JLabel(" "));
		JPanel currentP2 = new JPanel();
		currentP2.setBorder(BorderFactory.createTitledBorder("生徒の筆記にアノテーションする方法"));
		currentP2.add(new JLabel("筆席マップを教師用ペンでタップしてから，"));
		currentP2.add(new JLabel("生徒の筆記に書き込んでください．（実験中）"));
		add(currentP2);

		JButton jb = new JButton("CSVに書き出し（実験中）");
		jb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ArrayList<String> buffer = new ArrayList<String>();
				for(Note n: satn.notes.values()){
					n.exportToCSV(buffer);
				}
				FileReadWriter.putLines("simpleatn.csv", buffer);
				JOptionPane.showMessageDialog(satn, "simpleatn.csv に書き出しました．");

				Runtime runtime = Runtime.getRuntime();
				try{
					runtime.exec("cmd.exe /c start " + ".");
				}catch(IOException ex){
					ex.printStackTrace();
				}
			}
		});
		add(jb);

		JButton jb2 = new JButton("丁寧度CSV書出し");
		jb2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				outputteinei();
			}
		});
		add(jb2);

		/*		JPanel currentP2 = new JPanel();

		currentP2.setBorder(BorderFactory.createTitledBorder("転送設定シートの番号"));
//		currentP.setLayout(new BoxLayout(currentP, BoxLayout.Y_AXIS));
//		currentP.setAlignmentX(CENTER_ALIGNMENT);

		currentP2.add(new JLabel("転送設定シートの番号:"));
		targetMatrixSheet = new JTextField("   6");
		targetMatrixSheet.setFont(sans30);
		currentP2.add(targetMatrixSheet);

		JButton ok2 = new JButton("設定");
		ok2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				update();
			}
		});
		currentP2.add(ok);
		add(currentP2);

		status2 = new JLabel(" ");
		add(status2);*/
	}
	public void outputteinei(){
		ArrayList<String> buffer = new ArrayList<String>();
		buffer.add("penid,paperid,keikamsec,keikawariai,within10sec,within30sec,within60sec,timegap,ramer,varspeed/log10(ramer),point/dist,point,dist,x0,y0,pos");
		for(Note n: satn.notes.values()){
			n.exportToCSVTeinei(buffer);
		}
		FileReadWriter.putLines("teinei.csv", buffer);

		ArrayList<String> buffer2 = new ArrayList<String>();
		buffer2.add("penid,pos,avgpoint,avgdist,avgvsplog,varvsplog,avgppd,varppd,varppdr0,varppdr1,varppdr2,varppdr3,varppdr4,varppdr5,cnt,cntr0,cntr1,cntr2,cntr3,cntr4,cntr5,avgramer,varramer,ansavgrank,ansavg,ansvarrank,ansvar");
		for(Note n: satn.notes.values()){
			n.exportToCSVVarppdr(buffer2);
		}
		FileReadWriter.putLines("varppdr.csv", buffer2);


		JOptionPane.showMessageDialog(satn, "teinei.csv に書き出しました．");

		Runtime runtime = Runtime.getRuntime();
		try{
			if (SystemUtils.IS_OS_WINDOWS){
				runtime.exec("cmd.exe /c start " + ".");
			} else if (SystemUtils.IS_OS_MAC){
				runtime.exec("open .");
			}
		}catch(IOException ex){
			ex.printStackTrace();
		}
	}
	
	public JFrame findActiveParent(){
		if (frame.isShowing()) return frame;
		else return satn;
	}
	public void update(){
		String teacherPenIDStr = teacherPenID.getText();
		try{
			tpid = Integer.parseInt(teacherPenIDStr.trim());
		}catch(NumberFormatException nfex){
			status.setText("教師が使用するペンの番号は，まだ設定されていません");
			return;
		}
		if (tpid>0) {
			status.setText("教師が使用するペンの番号を "+tpid+"番に設定しました");
		} else {
			status.setText("教師が使用するペンの番号は，まだ設定されていません");
		}

	}

	@Override
	public void run() {
	}
	@Override
	public void process(ShortStroke ss) {
		System.out.println("Draw");
		if (ss.penid == this.tpid){
			System.out.println("Draw on delegate");
			Note n = satn.getNote(this.target4teacher);
			ShortStroke copyss = ss.clone();
			copyss.penid = this.target4teacher;
			copyss.color = 0xff0000;
			n.addStroke(copyss,true);
		}
	}
	@Override
	public void processPrevious(ShortStroke ss) {

	}

}
