
package panels;

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

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import frames.IMainFrame;
import resources.Customer;
import resources.CustomersModel;

//-------------------------------------------------------------------------
// overview of customers
public class MainPanel extends JPanel {

	// frame
	private IMainFrame mainFrame;

	// model
	private CustomersModel model;

	// table
	private JTable panelTable;
	private DefaultTableModel panelTableModel;
	private String[] columnNames = { "name", "point", "total" };

	// buttons
	private JButton addButton;
	private JButton removeButton;
	private JButton showHistoryButton;

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// constructor
	// -------------------------------------------------------------------------
	//
	public MainPanel(IMainFrame mainFrame, CustomersModel model) {

		super(new BorderLayout());

		// initialize frame
		this.mainFrame = mainFrame;

		// initialize model
		this.model = model;
//		customerTable = new HashMap<Integer, Customer>();

		// initialize table
		panelTableModel = new DefaultTableModel(columnNames, 0);
		panelTable = new JTable(panelTableModel);
		// deny to edit cell
		panelTable.setDefaultEditor(Object.class, null);

		// add buttons
		addButton = new JButton("Add");
		removeButton = new JButton("Remove");
		showHistoryButton = new JButton("Show History");

		// add listener
		addButton.addActionListener(new AddActionHandler());
		removeButton.addActionListener(new RemoveActionHandler());
		showHistoryButton.addActionListener(new ShowHistoryActionHandler());

		JPanel buttonPanel = new JPanel();
		buttonPanel.add(addButton, BorderLayout.WEST);
		buttonPanel.add(removeButton, BorderLayout.EAST);
		add(buttonPanel, BorderLayout.CENTER);

		// add show history
		JPanel showButtonPanel = new JPanel();
		FlowLayout flowLayout = new FlowLayout();
		showButtonPanel.add(showHistoryButton, BorderLayout.SOUTH);
		showButtonPanel.setLayout(flowLayout);
		add(showButtonPanel, BorderLayout.SOUTH);

		// add scroll menu
		JScrollPane listScrollPane = new JScrollPane(panelTable);
		add(listScrollPane, BorderLayout.NORTH);

	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// public
	// -------------------------------------------------------------------------
	//
	public void updateTableByTransition() {
		for (int i = 0; i < panelTableModel.getRowCount(); i++) {
			// name, points, total
			panelTableModel.setValueAt(model.getCustomers().get(i).getName(), i, 0);
			panelTableModel.setValueAt(model.getCustomers().get(i).getPoints(), i, 1);
			panelTableModel.setValueAt(model.getCustomers().get(i).getTotal(), i, 2);
		}
	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// private
	// -------------------------------------------------------------------------
	//
	private void transitionToHistory(int index) {
		mainFrame.showHistoryPanel(index);
	}

	// -------------------------------------------------------------------------
	//
	private Customer createNewCustomerByName(String name) {
		Customer customer = new Customer(name + model.getCustomers().size());
		return customer;
	}

	// -------------------------------------------------------------------------
	//
	private Object[] generateCustomerRow(Customer customer) {
		Object[] data = { customer.getName(), customer.getPoints(), customer.getTotal() };
		return data;
	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// interface
	// -------------------------------------------------------------------------
	// add button's event
	private class AddActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Customer customer = createNewCustomerByName("name");
			model.getCustomers().add(customer);
			panelTableModel.addRow(generateCustomerRow(customer));
		}
	}

	// -------------------------------------------------------------------------
	// remove button's event
	private class RemoveActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			if (panelTable.getSelectedRow() < 0)
				return;

			if (1 < panelTable.getRowCount()) {
				model.getCustomers().remove(panelTable.getSelectedRow());
				panelTableModel.removeRow(panelTable.getSelectedRow());
			}
		}
	}

	// -------------------------------------------------------------------------
	// transition to history
	private class ShowHistoryActionHandler implements ActionListener {

		// -------------------------------------------------------------------------
		//
		public void actionPerformed(ActionEvent e) {
			if (-1 < panelTable.getSelectedRow())
				transitionToHistory(panelTable.getSelectedRow());
		}
	}

	public JButton getAddButton() {
		return addButton;
	}

	public JButton getRemoveButton() {
		return removeButton;
	}

	public JButton getShowHistoryButton() {
		return showHistoryButton;
	}

	public JTable getPanelTable() {
		return panelTable;
	}
}