#ifndef CDSTATE_H
#define CDSTATE_H

/*
 * @(#)CDstate.h
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 2001, 2002 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDWserver.cpp for details.
 *
 * Jörg Haeger, 10.05.2001
 */

/**
 * The state of a CD.
 *
 * @version 20030303
 * @author Jörg P. M. Haeger
 */
struct CDstate {
	int blocks0, blocks1, blocks2;
	int blockSize;
	int imageSizeInBlocks;
	int isBlank, isClosed, isRW;
	int extentsWrittenKB;
	int firstTrackIsData;
	int lastSession0, lastSession1;
	int manufacturerIndex;
	int speed;

	CDstate() {
		reset();
	}

	int blocksToKBytes(int blocks) {
		return ((blockSize / 512) * blocks) / 2;
	}

	const char *getCapacityStr(char *str) {
		// lead-in
		int free = blocks2 - 4500;
		if (blocks1 == blocks0)
			// first lead-out
			free -= 6750;
		else
			// subsequent lead-outs
			free -= 2250;
		if (free < 0)
			free = 0;

		sprintf(str, "capacity %d %d %d",
			blocksToKBytes(blocks0),
			blocksToKBytes(blocks1),
			blocksToKBytes(free));
		return str;
	}

	const char *getLogStr(char *str) {
		sprintf(str, "%-4d %4d %4d %14s %2s %6d %6d %6d %2d",
			manufacturerIndex, -1, -1,
			"unknown", isRW? "RW": "R",
			blocks0, blocks1, blocks2, speed);
		return str;
	}

	int getSize() {
		if (blocks1 > blocks2)
			return 1024 * blocksToKBytes(blocks1 - blocks2);
		else
			return 0;
	}

	int getSizeKB() {
		if (blocks1 > blocks2)
			return blocksToKBytes(blocks1 - blocks2);
		else
			return 0;
	}

	int isLastSessionInfoValid() {
		return lastSession0 >= 0 && lastSession1 >= 0;
	}

	void reset() {
		blocks0 = blocks1 = blocks2 = 0;
		blockSize = 2 * 1024;
		imageSizeInBlocks = 0;
		isClosed = 0;
		isRW = 0;
		extentsWrittenKB = 0;
		firstTrackIsData = 0;
		lastSession0 = lastSession1 = -1;
		manufacturerIndex = -2;
		speed = 1;
	}
};

#endif
