/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef PROGRAMFILTER_HPP
#define PROGRAMFILTER_HPP PROGRAMFILTER_HPP

#include "nmm/nmm_prefix.hpp"

#include <iostream>
#include <list>
#include <string>

#include "nmm/comm/serialize/SerializedValue.hpp"
#include "nmm/comm/serialize/TValue.hpp"

#include "nmm/comm/TSAddress.hpp"
#include "nmm/utils/time/LocalTime.hpp"
#include "ChannelDescription.hpp"


namespace NMM{

  /** \class ProgramFilter
   * This class represents a Filter holding search-criterias for search programs in TVGuide
   */

  class ProgramFilter : public SerializedValue{

  private:
    NMM::TValue<string> filtername;
    
    NMM::TValue<LocalTime> starttime;
    NMM::TValue<LocalTime> endtime;
    NMM::TValue<string> phrase;

    NMM::TValue<list<string> > categories;
    NMM::TValue<list<ChannelDescription> > channels;

    NMM::TValue<nmm_bool> starttimeEdited; //Only needed for EPGSearchState in MMBox yet

    NMM::TValue<nmm_bool> getCurrentEvents;
    NMM::TValue<nmm_bool> getFollowingEvents;

    NMM::TValue<nmm_bool> startTimeEmpty;
    NMM::TValue<nmm_bool> endTimeEmpty;

    //Per-To_peer-variables
    NMM::TValue<nmm_uint32>  id;
    NMM::TValue<list<string> > visitedPeerHosts;

    NMM::TValue<nmm_int32>           hops;
  public:
    ProgramFilter();
    ProgramFilter(const ProgramFilter&);
    ~ProgramFilter();

    /**
     *Set a name for this ProgramFilter
     *@param string name
     */
    void setFilterName(string name){filtername = name;}

    /**
     *Get the name of this ProgramFilter
     *@return filtername
     */
    string getFilterName(){return filtername.getRValue();}

    /**
     *Set the start-time at wich point the search should begin
     *@param string starttime
     */
    void setStartTime(string st);
    /**
     *Get the start-time set
     *@return start-time
     */
    void setStartTime(const LocalTime &t);
    /**
     *Get the start-time set
     *@return start-time
     */
    LocalTime getStartTime() const;

    bool getStartTimeEmpty() const { return startTimeEmpty.getRValue();}
    /**
     *Set the end-time at wich point the search should stop
     *@param string endtime
     */
    void setEndTime(const LocalTime &t);
    /**
     *Set the end-time at wich point the search should stop
     *@param string endtime
     */
    void setEndTime(string et);
    /**
     *Get the end-time set
     *@return string end-time
     */
    LocalTime getEndTime() const;
    bool getEndTimeEmpty() const {return endTimeEmpty.getRValue();}

    /**
     *Set a string wich the program-informations should contain
     *@param string-pattern
     */
    void setSearchPhrase(string p);
    /**
     *Get the SearchPhrase set
     *@return string-pattern
     */
    string getSearchPhrase() const;

    /**
     *Add a category in wich the resulting programs should fit in
     *@param category
     */
    void addCategory(string c);
    /**
     *Get the categories set
     *@return list<string> categories
     */
    list<string> getCategories() const;
    
    /**
     *Add a channel to search inly programms on this channel.
     *@param ChannelDescription
     */
    void addChannel(ChannelDescription c);
    /**
     *Get the channel-IDs set
     *@return list<ChannelDescription> channels
     */
    list<ChannelDescription> getChannels() const;
    /**
     *Get the channel-IDs set
     *@return list<ChannelDescription> channels
     */
    list<ChannelDescription> getChannels();

    void setStarttimeEdited(bool b){starttimeEdited = b;}
    bool getStarttimeEdited(){ return starttimeEdited.getRValue();}

    /**
     * adds a PeerHost that is vidited by this ProgramFilter. 
     * This funktion is used to create a history of already visited hosts when searching in the network for information
     */
    void addPeerHost(string name);
    /**
     * test if this ProgramFilter has already visited the given host
     */
    bool hasPeerHost(string name);
    /**
     * Clears the list of already visited hosts
     */
    void clearPeerHosts();

    void setGetCurrentEvents(bool b){getCurrentEvents = b;}
    bool getGetCurrentEvents(){return getCurrentEvents.getRValue();}

    void setGetFollowingEvents(bool b){getFollowingEvents = b;}
    bool getGetFollowingEvents(){return getFollowingEvents.getRValue();}

    void setID(unsigned int i){id = i;}
    unsigned int getID() const {return id.getRValue();}

    void increaseHops(){hops.getRValue()++;}
    void decreseHops(){hops.getRValue()--;}
    int getHops(){return hops.getRValue();}

    //Interface SerialzedValue
    ProgramFilter* copy();

    bool operator==(const ProgramFilter &pf) const;

  private:
    void registerValues();
    LocalTime stringToLocalTime(string s);

  };

  OValueStream& operator<<(OValueStream& os, const list<ProgramFilter>& pf);
  IValueStream& operator>>(IValueStream& is, list<ProgramFilter>& pf);

}

#include "nmm/nmm_suffix.hpp"

#endif
