package listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import dao.RoomDao;
import entity.Room;
import entity.RoomType;
import frame.AddRoomDialog;
import frame.MainPanel;

public class AddRoomListener implements ActionListener {
	private JComboBox jcbRoomType;
	private JTextField jtfRoomNumber, jtfEndNumber, jtbFloor;
	private JButton cancel;

	public AddRoomListener(JComboBox jcbRoomType, JTextField jtfRoomNumber,
			JTextField jtfEndNumber, JTextField jtbFloor, JButton cancel) {
		this.jcbRoomType = jcbRoomType;
		this.jtfRoomNumber = jtfRoomNumber;
		this.jtfEndNumber = jtfEndNumber;
		this.jtbFloor = jtbFloor;
		this.cancel = cancel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == cancel) {
			AddRoomDialog.instance().dispose();
		} else {
			if (jtfEndNumber.isVisible()) {
				if (jtfRoomNumber.getText().equals("")
						|| jtfEndNumber.getText().equals("")
						|| jtbFloor.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "Çë½«ÐÅÏ¢ÌîÐ´ÍêÕû£¡");
					return;
				}
				int nums = 0;
				int errNums = 0;
				int roomId = Integer.parseInt(jtfRoomNumber.getText());
				int endId = Integer.parseInt(jtfEndNumber.getText());
				if (roomId > endId) {
					JOptionPane.showMessageDialog(null, "¿ªÊ¼·¿ºÅÓ¦±È½áÊø·¿ºÅÐ¡£¡");
					return;
				}
				while (roomId <= endId) {
					if (RoomDao.instance().getRoom(roomId) == null) {
						++nums;
						int roomType = ((RoomType) jcbRoomType
								.getSelectedItem()).getId();
						RoomDao.instance().addRoom(
								new Room(roomId, Integer.parseInt(jtbFloor
										.getText()), roomType));
					}else{
						++errNums;
					}
					++roomId;
				}
				AddRoomDialog.instance().dispose();
				MainPanel.instance().refresh();
				JOptionPane
						.showMessageDialog(null, "<html>³É¹¦Ìí¼Ó <b><font size=8>"
								+ nums + "</font></b> ¸ö·¿¼ä£¡" + ((errNums > 0)?"ÆäÖÐ <b><font size=8>"
								+ errNums + "</font></b> ¸ö·¿¼äÒÑ´æÔÚ¡£":""));
			} else {
				if (jtfRoomNumber.getText().equals("")
						|| jtbFloor.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "Çë½«ÐÅÏ¢ÌîÐ´ÍêÕû£¡");
					return;
				}
				int roomId = Integer.parseInt(jtfRoomNumber.getText());
				if (RoomDao.instance().getRoom(roomId) != null) {
					JOptionPane.showMessageDialog(null,
							"<html>·¿ºÅ<b><font size=8>" + roomId
									+ "</font></b>ÒÑ´æÔÚ£¡", "ÌáÊ¾",
							JOptionPane.ERROR_MESSAGE);
					return;
				}
				int roomType = ((RoomType) jcbRoomType.getSelectedItem())
						.getId();
				RoomDao.instance().addRoom(
						new Room(roomId, Integer.parseInt(jtbFloor.getText()),
								roomType));
				MainPanel.instance().refresh();
				AddRoomDialog.instance().dispose();
				JOptionPane.showMessageDialog(null, "Ìí¼Ó³É¹¦£¡");
			}
		}
	}
}
