/* -*- indent-tabs-mode: nil; -*-
 *
 * This file is part of the Mango recipe management distribution.
 * Copyright (c) 2000-2003, Roland Jesse
 * All rights reserved.
 *
 * For license and copyright information see the file COPYRIGHT or 
 * <http://www.opensource.org/licenses/bsd-license.html>.
 *
 * $Id: Menu.cpp,v 1.2 2003/05/11 14:47:35 rjesse Exp $
 */

#include "Menu.h"
#include "Recipe.h"

void
MenuRecipe::clear()
{
   AmbrosiaFingerprint fp;
   m_recipeId = fp;
   m_title = QString::null;
   m_amount = 0;
   m_persons = 0;
   m_description = QString::null;
}

Menu::Menu()
{
}

Menu::Menu( const Menu& right)
{
   m_title = right.title();
   m_recipes = right.getRecipes();
   m_customData = right.getCustomData();
}

void
Menu::clear()
{
   m_title = QString::null;
   m_recipes.clear();
   m_customData.clear();
}

QString
Menu::title() const
{
   return m_title;
}

void 
Menu::setTitle( const QString& s)
{
   m_title = s;
}

void
Menu::addRecipe( const Recipe& r)
{
   MenuRecipe* mrecipe = new MenuRecipe();
   mrecipe->setRecipeId( AmbrosiaFingerprint( r.fingerprint()));
   mrecipe->setTitle( r.title());
   mrecipe->setAmount( 1);
   mrecipe->setPersons( r.yieldQty());
   m_recipes.append( *mrecipe);
}

void
Menu::addMenuRecipe( const MenuRecipe& mr)
{
   m_recipes.append( mr);
}

Menu::MenuRecipes
Menu::getRecipes() const
{
   return m_recipes;
}

Menu::CustomData
Menu::getCustomData() const
{
   return m_customData;
}

void
Menu::setCustomData( const Menu::CustomData& cd)
{
   m_customData = cd;
}

/*!
  Adds some custom data to this menu.
  The custom data is associated with \a key and contains \a value.
*/
void
Menu::addCustomData( const QString& key, const QString& value)
{
   m_customData.insert( key, value);
}

/*****************************************************************************
  Menu stream functions
 *****************************************************************************/

/*!
  \relates Menu
  Writes the menu \a menu to the stream \a s.
*/
QDataStream&
operator<<( QDataStream& s, const Menu& menu)
{
   s << menu.title();            // title of the menu
   Menu::MenuRecipes mrecipes = menu.getRecipes();
   s << (Q_UINT32) mrecipes.count();   // number of recipes in the menu
   // for each recipe, store: recipe id, amount, number of persons, description
   Menu::MenuRecipes::const_iterator mit = mrecipes.begin();
   while (mit != mrecipes.end()) {
      s << (*mit).recipeId();
      s << (Q_INT32) (*mit).amount();
      s << (Q_INT32) (*mit).persons();
      s << (*mit).description();
      mit++;
   }
   s << menu.getCustomData();       // all custom data

   return s;
}

/*!
   \relates Menu
   Reads \a menu from stream \a s.
 */
QDataStream&
operator>>( QDataStream& s, Menu& menu)
{
   QString str;
   s >> str;
   menu.setTitle( str);
   Q_UINT32 ui;
   s >> ui;
   for (uint r = 0; r < ui; r++) {
      MenuRecipe mr;
      s >> str;
      mr.setRecipeId( str);
      Q_INT32 si;
      s >> si;
      mr.setAmount( si);
      s >> si;
      mr.setPersons( si);
      s >> str;
      mr.setDescription( str);
      menu.addMenuRecipe( mr);
   }
   Menu::CustomData cd;
   s >> cd;
   menu.setCustomData( cd);

   return s;
}
