#include "stdafx.h" 
#include "../BookStoreLib.h" 
#include "../BookStoreLib/Bookstore.h" 

using namespace LtXmlLib16;

// forward declarations
void SimpleTestBookStoreLibCBookstore(LPCTSTR);
void WriteTest();
void ReadTest();

/// <summary>
/// The main entry point for the application.
/// </summary>
int main(int argc, char* argv[])
{
	WriteTest();
	ReadTest();

	_tprintf(_T("\n\nPress a key to finish"));
	getchar();

	return 0;
}
		
void WriteTest()
{
	try
	{
		_tprintf(_T("\nCreating an XML document...\n"));

		// to create the XML document from scratch
		BookStoreLib::CBookstorePtr spBS = BookStoreLib::CBookstore::CreateInstance();
		BookStoreLib::CBookTypePtr spBook = spBS->GetBook()->Add();
		spBook->SetTitle(_T("The Autobiography of Benjamin Franklin"));
		spBook->SetPrice(8.99);
		spBook->SetPublicationdate(LtXmlLib16::CDateTime(1981, 5, 11));
		spBook->SetISBN(_T("1-861003-11-0"));
		spBook->GetAuthor()->SetFirst_name(_T("Benjamin"));
		spBook->GetAuthor()->SetLast_name(_T("Franklin"));
		spBook->SetGenre(_T("autobiography"));
		std::tstring strXml = spBook->ToXml();

		_tprintf(_T("\nThis is the XML:\n%s"), strXml.c_str());
	}
	catch (CLtException& e)
	{
		// Note : exceptions are likely to contain inner exceptions
		// that provide further detail about the error, GetFullMessage
		// concatantes the messages from them all.
		_tprintf(_T("Error - %s\n"), e.GetFullMessage().c_str());
	}
}

void ReadTest()
{
	try
	{
		_tprintf(_T("\nReading an existing XML document...\n"));

		// to create the XML document from scratch
		BookStoreLib::CBookstorePtr spBS = BookStoreLib::CBookstore::CreateInstance();
		spBS->FromXmlFile(_T("..\\..\\..\\BookStoreSample.xml"));

		for (BookStoreLib::CBookTypeCol::iterator itr = spBS->GetBook()->begin();
			 itr != spBS->GetBook()->end();
			 itr++)
		{
			BookStoreLib::CBookTypePtr spBook = *itr;

			_tprintf(_T("Book Title           %s\n"), spBook->GetTitle().c_str());
			_tprintf(_T("    Price            %f\n"), spBook->GetPrice());
			_tprintf(_T("    Author           %s %s\n"), spBook->GetAuthor()->GetFirst_name().c_str(), spBook->GetAuthor()->GetLast_name().c_str());
			if (spBook->IsValidPublicationdate())
				_tprintf(_T("    Publicationdate  %s\n"), spBook->GetPublicationdate().ToString().c_str());
			else                    
				_tprintf(_T("    Publicationdate  Not Listed\n"));
			if (spBook->IsValidISBN())
				_tprintf(_T("    ISBN             %s\n"), spBook->GetISBN().c_str());
			else                    
				_tprintf(_T("    ISBN             Not Listed\n"));
			if (spBook->IsValidGenre())
				_tprintf(_T("    Genre            %s\n"), spBook->GetGenre().c_str());
			else                    
				_tprintf(_T("    Genre            Not Listed\n"));
		}
	}
	catch (CLtException& e)
	{
		// Note : exceptions are likely to contain inner exceptions
		// that provide further detail about the error, GetFullMessage
		// concatantes the messages from them all.
		_tprintf(_T("Error - %s\n"), e.GetFullMessage().c_str());
	}
}