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

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import ee.ut.math.tvt2008.salessystem.domain.data.Sale;
import ee.ut.math.tvt2008.salessystem.util.Translations;

/**
 * Purchase history model.
 */
public class PurchaseHistoryTableModel extends SalesSystemTableModel<Sale> {
	private static final long serialVersionUID = 1L;

	private static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

	public PurchaseHistoryTableModel() {
		super(new String[] {
				Translations.getString("main.id"),
				Translations.getString("main.client"),
				Translations.getString("main.time"),
				Translations.getString("main.sum")});
	}

	@Override
	protected Object getColumnValue(Sale sale, int columnIndex) {
		switch (columnIndex) {
		case 0:
			return sale.getId();
		case 1:
			return sale.getClient() != null ? sale.getClient().getFirstName() : 
				Translations.getString("sale.unknownclient.name");
		case 2:
			return DATE_FORMAT.format(sale.getSellingTime());
		case 3:
			return sale.getSum();
		}
		throw new IllegalArgumentException("Column index out of range");
	}

	@Override
	public String toString() {
		final StringBuffer buffer = new StringBuffer();

		for (int i = 0; i < headers.length; i++)
			buffer.append(headers[i] + "\t");
		buffer.append("\n");

		for (final Sale sale : rows) {
			buffer.append(sale.getId() + "\t");
			buffer.append(sale.getClient() != null ? sale.getClient().getFirstName() : "" + "\t");
			buffer.append(sale.getSum() + "\t");
			buffer.append("\n");
		}

		return buffer.toString();
	}
}
