package cnp.ew.tdemo;

import java.util.*;
import java.io.*;
import java.awt.*;

import cnp.ew.displayer.*;
import cnp.ew.converter.*;
import cnp.ew.lightweight.*;
import cnp.ew.util.*;

public class CpQuickenTransaction implements CpSelectableDisplayable, CpFocusDisplayable
{
    public String ref;
    public String category;
    public String memo;
    public String cleared;
    public int amount;
    public String payee;
    public Date date;
    public int runningTotal;

    boolean showFocus;
    public boolean selected;

    static CpDateToStringConverter dateConverter = new CpDateToStringConverter();

    public CpQuickenTransaction()
    {
    }

    public CpQuickenTransaction(String newRef, String newCategory, String newMemo, String newCleared, int newAmount, String newPayee, Date newDate) {
        ref = newRef;
        category = newCategory;
        memo = newMemo;
        cleared = newCleared;
        amount = newAmount;
        payee = newPayee;
        date = newDate;
    }

    public int runningTotal(int amountToAdd)
    {
        runningTotal = amount + amountToAdd;
        return runningTotal;
    }

    public boolean getIsSelected()
    {
        return selected;
    }

    public void setIsSelected(boolean newIsSelected)
    {
        selected = newIsSelected;
    }

    public void setShowFocus(boolean showFocus)
    {
        this.showFocus = showFocus;
    }

    public boolean getShowFocus()
    {
        return showFocus;
    }

    public void setModel(Object model) {}
    public Object getModel() { return null; }

    public void paintIn(CpLightweightComponent c, Graphics g, Rectangle r) {}

    public Dimension preferredSize(CpLightweightComponent c)
    {
        return new Dimension(500, 20);
    }

	public void paintIn(CpLightweightComponent c, Graphics g, int x, int y, int w, int h)
	{
	    FontMetrics fontMetrics = g.getFontMetrics();
		int fontAscent = fontMetrics.getAscent() + 2;
		int lineHeight = 17;
		int bottom = (2 * lineHeight) - 1;

		g.setColor(Color.white);
		g.fillRect(x, y, w, lineHeight);
        g.setColor(new Color(255, 255, 128));
        g.fillRect(x, y + lineHeight, w, lineHeight);
        g.setColor(Color.gray);
        int lines[] = {55, 82, 312, 367, 379, 434};
        for (int i = 0; i < 6; i++ ) {
            g.fillRect(x + lines[i], y, 3, bottom);
        }
        g.drawLine(x + 350, y, x + 350, y + bottom);
        g.drawLine(x + 417, y, x + 417, y + bottom);
        g.drawLine(x + 467, y, x + 467, y + bottom);
        g.drawLine(x + 484, y, x + 484, y + bottom);
		g.setColor(Color.black);
        g.drawString(dateConverter.convert(date), x + 3, y + fontAscent);
        g.drawString(ref, x + 60, y + fontAscent);
        if (selected) {
            Font f = c.getFont();
	        g.setColor(Color.blue);
	        g.setFont(new Font(f.getName(), f.getStyle() | Font.BOLD, f.getSize()));
	    }
        g.drawString(payee, x + 88, y + fontAscent);
        g.setFont(c.getFont());
		g.setColor(Color.black);
        g.drawString(memo, x + 88, y + lineHeight + fontAscent);
        g.drawString(category, x + 212, y + lineHeight + fontAscent);

    /*    if (amount.floatValue() < 0.0 ) {
            g.drawString((new Integer(amount.intValue())).toString(), x + 437, y + fontAscent);
            g.drawString((new Integer((int)((amount.floatValue() * 100) % 100))).toString(), x + 492, y + fontAscent);
        } else {
            g.drawString((new Integer(amount.intValue())).toString(), x + 529, y + fontAscent);
            g.drawString((new Integer((int)((amount.floatValue() * 100) % 100))).toString(), x + 584, y + fontAscent);
        }
        if (cleared != null) {
            g.drawString(cleared, x + 509, y + fontAscent);
        }
        runningTotal = new Float(runningTotal.floatValue() + amount.floatValue());
        g.drawString((new Integer(runningTotal.intValue())).toString(), x + 603, y + fontAscent);
        g.drawString((new Integer((int)((runningTotal.floatValue() * 100) % 100))).toString(), x + 658, y + fontAscent);
    */

        int amx;
        String money;
        if (amount < 0) {
            amx = x + 318;
            money = (new Integer(-amount / 100)).toString();
        } else {
            amx = x + 385;
            money = (new Integer(amount / 100)).toString();
        }

        g.drawString(money, x + amx + 30 - fontMetrics.stringWidth(money), y + fontAscent);
        money = new Integer(Math.abs(amount) % 100).toString();
        if (money.length() < 2) {
            money = "0" + money;
        }
        g.drawString(money, x + amx + 48 - fontMetrics.stringWidth(money), y + fontAscent);

        if (cleared != null) {
            if (cleared.equals("X")) {
                g.drawString("R", x + 369, y + fontAscent);
                g.drawString("R", x + 370, y + fontAscent);  // Cheap Hack for BOLD
            } else {
                g.drawString(cleared, x + 371, y + fontAscent);
            }
        }
        if (runningTotal < 0) {
            g.setColor(Color.red);
        }
        money = (new Integer(runningTotal / 100)).toString();
        g.drawString(money, x + 464 - fontMetrics.stringWidth(money), y + fontAscent);
        money = new Integer(Math.abs(runningTotal) % 100).toString();
        if (money.length() < 2) {
            money = "0" + money;
        }
        g.drawString(money, x + 482 - fontMetrics.stringWidth(money), y + fontAscent);

        g.setColor(Color.gray);
        g.drawLine(x, y + bottom, x + w, y + bottom);
        g.setColor(Color.black);

        if (showFocus) {
            CpFocusRect.drawFocusRect(g, x, y, w, h - 1);
        }

	}
}

