#ifndef CDSTATE_H
#define CDSTATE_H

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

#include "String.h"

/**
 * The state of a CD.
 *
 * @version 20031017
 * @author Jörg P. M. Haeger
 */
class CDstate {
	private: int isBlankAttr;
	public: int blocks0, blocks1, blocks2;
	int blockSize;
	int imageSizeInBlocks;
	int isClosed, isRW;
	int extentsWrittenKB;
	int firstTrackIsData;
	int lastSession0, lastSession1;
	int manufacturerIndex;
	String mediaType;
	String preparerID;
	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;
	}

	public: bool isBlank() {
		return isBlankAttr == 1;
	}

	public: bool isBlankUnknown() {
		return isBlankAttr == -1;
	}

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

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

	public: void setIsBlank(bool yes) {
		if (yes)
			isBlankAttr = 1;
		else
			isBlankAttr = 0;
	}
};

#endif
