package com.ethon.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

import com.ethon.DataBase;
import com.ethon.io.DataReader;
import com.ethon.mode.DataPoint;
import com.ethon.plugin.Hasta;
import com.ethon.tools.CoordTransfer;
import com.ethon.tools.ImageGenerator;

/**
 * 主体窗口框架
 * 
 * @author ethonchan ethonchan@gmail.com
 * 
 */
public class DataField extends JFrame {
	private static final long serialVersionUID = 1L;

	private static ImagePanel ipanel;
	static BufferedImage tempImg;

	public DataField() {
		// 设置UI--START
		String ui = UIManager.getSystemLookAndFeelClassName();
		LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();
		for (LookAndFeelInfo lk : info) {
			if (lk.getName().equals("Nimbus")) {
				ui = lk.getClassName();
				break;
			}
		}
		try {
			UIManager.setLookAndFeel(ui);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		// 设置UI--END

		// 设置菜单栏--START
		MenuBar mb = new MenuBar();
		setJMenuBar(mb);
		// 设置菜单栏--END

		// 设置窗口大小和位置--START
		Dimension dim = getToolkit().getScreenSize();
		int sw = (int) dim.getWidth() / 9;
		int sh = (int) dim.getHeight() / 9;
		setLocation(sw, sh);
		setSize(sw * 7, sh * 7);
		// 设置窗口大小和位置--END

		int len = CoordTransfer.getLenOfImagePanel(this);
		tempImg = ImageGenerator.drawImage(null, len);

		// 设置显示面板--START
		ipanel = new ImagePanel(DataField.this);
		add(ipanel);
		// 设置显示面板--END

		setTitle("DataField Clustering Tool");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
	}

	/**
	 * 窗口的菜单栏
	 * 
	 * @author Alex
	 * 
	 */
	class MenuBar extends JMenuBar {
		private static final long serialVersionUID = 1L;

		MenuBar() {
			JMenu menu1 = new JMenu("File");
			JMenuItem item11 = new JMenuItem("Open");
			item11.addActionListener(new FileOpenListener());
			JMenuItem item12 = new JMenuItem("Exit");
			item12.addActionListener(new ExitListener());

			JMenu menu2 = new JMenu("Clustering");
			JMenuItem item23 = new JMenuItem("Hasta Clustering Algorithm");
			item23.addActionListener(new SECSNClusterListener());

			menu1.add(item11);
			menu1.add(item12);

			menu2.add(item23);

			add(menu1);
			add(menu2);
		}
	}

	/**
	 * 刷新窗口图像
	 * 
	 * @param image
	 */
	public static void updateImagePanel(BufferedImage image) {
		tempImg = image;
		ipanel.update(image);
	}

	public static BufferedImage getImage() {
		return tempImg;
	}

	/**
	 * 重置数据场
	 */
	public void resetDataField() {

	}

	/**
	 * 读取数据文件
	 * 
	 * @author ethonchan@gmail.com
	 * 
	 */
	class FileOpenListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			JFileChooser jfc = new JFileChooser();
			int value = jfc.showOpenDialog(DataField.this);
			if (value == JFileChooser.APPROVE_OPTION) {
				resetDataField();
				DataReader reader = new DataReader();
				File file = jfc.getSelectedFile();
				DataField.this.setTitle("DataField Clustering Tool--"
						+ file.getName());
				DataBase.getInstance().clear();
				DataPoint[] points = reader.getDataPoints(file, 0);// 读取数据

				CoordTransfer ctf = new CoordTransfer();
				int len = CoordTransfer.getLenOfImagePanel(DataField.this);
				ctf.initCoord(points, len);

				BufferedImage image = ImageGenerator.drawImage(null, len);
				image = ImageGenerator.show_DataPoints_on_Image(points, image);
				updateImagePanel(image);
			}

		}
	}

	/**
	 * 退出程序
	 * 
	 * @author ethonchan@gmail.com
	 * 
	 */
	private class ExitListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}

	/**
	 * 改进后的Spatial Distance Cluster算法
	 * 
	 * @author ethonchan@gmail.com
	 * 
	 */

	class SECSNClusterListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			String input = JOptionPane
					.showInputDialog("Please input the Grid-number Parameter k");
			if (input == null)
				return;
			int K = Integer.parseInt(input.trim());

			input = JOptionPane
					.showInputDialog("Please input the Impact-factor Parameter ifp");
			if (input == null)
				return;
			int stepParam = Integer.parseInt(input.trim());

			Hasta nm = new Hasta(K, stepParam);
			nm.process();
		}

	}
}
