package jp.ac.kyutech.mns.ist;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

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

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

public class ConfigPanel_Filter extends ConfigPanel implements Runnable {
	private static final long serialVersionUID = -1308877143307915884L;
	Thread t;
	final SimpleATN satn;
	
	
	public ArrayList<BaseSSFilter> filters;
	public SelectableJTable<BaseSSFilter> table;
	
	public void addRegionFilter(){
		
	}
	
	
	JLabel current;
	public ConfigPanel_Filter(final SimpleATN satn, JFrame parent){
		super(parent);
		this.satn = satn;
		filters = new ArrayList<BaseSSFilter>();
//		t = new Thread(this);
//		t.start();
		run();
		
		//設定画面用の画面
		tabIcon = QuickMenuActionManager.createNavigationIcon("development/J2EEApplication"+"24");
		tabTitle = "フィルタ";
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		JPanel p3 = new JPanel();
		p3.setBorder(BorderFactory.createTitledBorder("現在設定されているフィルタ"));
		p3.setLayout(new BorderLayout());

		table = new SelectableJTable<BaseSSFilter>(filters,
				new String[]{"info",""},
				new String[]{"info",""},
				new String[]{"hide","show","check","sieve","sievePair"},
				new int[]{200,500},
				null);
		p3.add(table, BorderLayout.CENTER);
		
		add(p3);
		update();

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

	@Override
	public void run() {
	}
	
	//ファイルから読み込むときは，フラッシュしない
	public void addRegionFilterByFile(int penid, int sheetnum, Rectangle2D rect, int level, String lecid){
		RegionSSFilter rf = RegionSSFilter.rssf.get(level);
		if (rf == null){
			rf = new RegionSSFilter("neat", lecid, level);
			RegionSSFilter.rssf.put(level, rf);
			filters.add(rf);
		}
		Sheet s = satn.getNote(penid).getSheet(sheetnum);
		RegionSS filterPath = new RegionSS(rect);
		filterPath.setPaint(rf.getColor());
		filterPath.setStrokePaint(null);
		filterPath.setStroke(new BasicStroke(0.0f));
		filterPath.setTransparency(0.3f);
		s.addChild(filterPath);
		
		rf.addRegion(s.getNote().penid, s.getNum(), filterPath);

		table.repaint();
	}
	//手動で追加するときは，確認の為フラッシュさせる
	public void addRegionFilter(Sheet s, PPath selection, int keyCode) {
		
		RegionSSFilter rf = RegionSSFilter.rssf.get(keyCode-KeyEvent.VK_0);
		String lecid = "";
		if (SimpleATN.theapp.openingLecture == null){
			lecid = SimpleATN.theapp.openingLectureid;
		} else {
			lecid = String.valueOf(SimpleATN.theapp.openingLecture.id);
		}
		if (rf == null){
			rf = new RegionSSFilter("neat", lecid, keyCode-KeyEvent.VK_0);
			RegionSSFilter.rssf.put(keyCode-KeyEvent.VK_0, rf);
			filters.add(rf);
		}
		
		RegionSS filterPath = new RegionSS(s.convert(selection));
		filterPath.setPaint(rf.getColor());
		filterPath.setStrokePaint(null);
		filterPath.setStroke(new BasicStroke(0.0f));
		filterPath.setTransparency(0.3f);
		s.addChild(filterPath);
		
		rf.addRegion(s.getNote().penid, s.getNum(), filterPath);
		filterPath.checkSS();
		
		table.repaint();
	}
	public void saveBeforeExit() {
		RegionSSFilter.saveBeforeExit();
	}

}
