#include "cdio.h"

namespace wrappers
{
	namespace cdio
	{

		device::device(void):
				m_cdio(0)
		{
		}

		device::device(const std::string& device):
				m_cdio(0)
		{
			open(device);
		}

		device::~device()
		{
			close();
		}

		bool device::open(const std::string& device)
		{
			close();
			return (m_cdio = cdio_open_cd(device.c_str())) != 0;
		}

		void device::close(void)
		{
			if (m_cdio != 0) {
				cdio_destroy(m_cdio);
				m_cdio = 0;
			}
		}

		bool device::eject(void)
		{
			return (cdio_eject_media(&m_cdio) == 0);
		}

		media_status device::get_media_status(void)
		{
			if (m_cdio == 0)
				return error;

			discmode_t mode = cdio_get_discmode(m_cdio);
			switch (mode) {
			case CDIO_DISC_MODE_CD_DA:
			case CDIO_DISC_MODE_CD_DATA:
			case CDIO_DISC_MODE_CD_XA:
			case CDIO_DISC_MODE_CD_MIXED:
				return no_dvd;

			case CDIO_DISC_MODE_DVD_RW:
			case CDIO_DISC_MODE_DVD_PRW:
			//case CDIO_DISC_MODE_DVD_OTHER: // might be double layer
			//case CDIO_DISC_MODE_NO_INFO:   // let growisofs decide
				return writeable;

			case CDIO_DISC_MODE_DVD_R:
			case CDIO_DISC_MODE_DVD_PR:
				{
					track_t track = cdio_get_first_track_num(m_cdio);
					if (track == CDIO_INVALID_TRACK)
						return writeable;
				}

			case CDIO_DISC_MODE_DVD_ROM:
			case CDIO_DISC_MODE_DVD_RAM:
				return readable;

			default:
				return unknown;
			}
		}

	}
}
