/*
 * @(#)SessionSizeView.java
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 1999-2004 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDcreator.java for details.
 */

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A session size counter component.
 *
 * @version 20041006
 * @author Jörg P. M. Haeger
 */
class SessionSizeView extends JComponent {
	SessionSizeView() {
		setDoubleBuffered(true);
		setBackground(Color.LIGHT_GRAY);
		setFont(new Font("Courier", 0, 12));
		setToolTipText(CDcreator.i18n("TTsessionSize"));
		addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				Project.CDRoot.clearCache();
				recount();
			}
		});
	}

	synchronized public void recount() {
		if (counter != null) {
			recounts++;
			State.stopRecount = true;
			return;
		}

		State.stopRecount = false;
		counter = new Thread() {
			public void run() {
				Project.CDRoot.recount();
				try {
					Thread.sleep(200);
				} catch (Exception e) {}
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						counterTerminated();
					}
				});
			}
		};

		repainter = new Thread() {
			int i = 0;
			public void run() {
				while (repainter != null) {
					try {
						Thread.sleep(100);
					} catch (Exception e) {}
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							repaint(100, 0, 0, getWidth(), getHeight());
							if (++i%10 == 0 && tableModel != null)
								tableModel.fireTableRowsUpdated(
									0, tableModel.getRowCount());
						}
					});
				}
			}
		};

		Project.numOfFiles = 0;
		Project.size = 0;
		Project.transferSize = 0;
		paintImmediately(0, 0, getWidth(), getHeight());
		CDcreator.view.setBurnButtonEnabled(false);

		counter.start();
		repainter.start();
		recounts = 0;
	}

	synchronized public void counterTerminated() {
		counter = null;
		repainter = null;
		CDcreator.view.setBurnButtonEnabled(true);
		repaint(1000, 0, 0, getWidth(), getHeight());
//		if (update != null)
//			update.updateUI();
		if (tableModel != null)
			tableModel.fireTableRowsUpdated(0, tableModel.getRowCount());
		if (recounts > 0)
			recount();
	}

	public int getFilesNum() {
		return Project.numOfFiles;
	}

	public int getKBytesNum() {
		return (int)(Project.size / 1024);
	}

	public void paint(Graphics g) {
//		g.clearRect(0, 0, getWidth(), getHeight());
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(0, 0, getWidth(), getHeight());
		if (counter != null)
			g.setColor(Color.red);
		else
			g.setColor(Color.gray);
		g.fillArc(1, 1, getHeight()-4, getHeight()-4, 0, 360);
		g.setColor(Color.black);

		String filesStr = String.valueOf(Project.numOfFiles);
		String sizeStr;
		if (Mode.audioCD()) {
			long time = Project.size;
			long seconds = time % 60;
			long minutes = time / 60;
			sizeStr = minutes + ":";
			if (seconds < 10)
				sizeStr += "0";
			sizeStr += seconds;
		}
		else {
			long n = Project.size / 1024;
			if (n < 1024)
				sizeStr = n + " KB";
			else
				if (Project.isTooBig()) {
					sizeStr = "> " + n / 1024 / 1024 + " GByte";
					filesStr = "> " + filesStr;
				}
				else
					sizeStr = n / 1024 + " MB";
		}

		g.drawString(
			sizeStr + " in "
			+ filesStr + " " + CDcreator.i18n("files"),
			getHeight() + 10, 10);

		int xx = getWidth() - 20 - 20, x;
		if (Mode.audioCD())
			x = (int)(xx * Project.size
				/ (74 * 60 * Mode.mediumSize / 650));
		else
			x = (int)(xx * Project.size
				/ (1024l * 1024 * Mode.mediumSize));
		g.setColor(Color.green);
		if (x <= xx) {
			g.fill3DRect(30, 12, x, 8, true);
			g.setColor(Color.gray);
			g.fill3DRect(30 + x, 12, xx - x, 8, true);
		}
		else {
			g.fill3DRect(30, 12, xx, 8, true);
			g.setColor(Color.red);
			g.fill3DRect(30 + xx, 12, x - xx, 8, true);
		}
	}

	private Thread counter = null, repainter = null;
	private int recounts = 0;
//	static JComponent update = null;
	static javax.swing.table.AbstractTableModel tableModel;
}
