package ee.ut.math.tvt2008.salessystem.ui.dialog;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.JTableHeader;

import ee.ut.math.tvt2008.salessystem.ui.model.SalesSystemModel;
import ee.ut.math.tvt2008.salessystem.util.Translations;

/**
 * Show detailed info about a sale.
 */
public class SaleInfoDialog extends JFrame {
	private static final long serialVersionUID = 1L;

	private Long currentSaleId;
	private SalesSystemModel model;
	
	public SaleInfoDialog(SalesSystemModel model) {
		this.model = model;
		
		JTable table = new JTable(model.getPurchaseInfoTableModel());
		
		JTableHeader header = table.getTableHeader();
		header.setReorderingAllowed(false);

		JScrollPane scrollPane = new JScrollPane(table);

		getContentPane().add(scrollPane);
		
		setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		
		setTitle(Translations.getString("saleinfo.title"));
		setSize(500, 200);
	}

	public void updateInfo(Long id) {
		// if sale is already loaded then do nothing
		if (currentSaleId != null && currentSaleId.longValue() == id.longValue())
			return;

		currentSaleId = id;
		
		// update purchase info
		model.updatePurchaseInfoDataModel(id);
	}
}
