// ---------------------------------------------------------------------------
// - Sheet.hpp                                                               -
// - afnix:sps module - sheet class definition                               -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  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.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2008 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  AFNIX_SHEET_HPP
#define  AFNIX_SHEET_HPP

#ifndef  AFNIX_STRVEC_HPP
#include "Strvec.hpp"
#endif

#ifndef  AFNIX_RECORD_HPP
#include "Record.hpp"
#endif

#ifndef  AFNIX_PRINTTABLE_HPP
#include "PrintTable.hpp"
#endif

namespace afnix {

  /// The Sheet class is a collection of records. Because of the record
  /// format, a sheet look like a 2-dimensional array of cells. Like the
  /// record, the sheet is defined with a name and a vector of records.
  /// For format purpose, the sheet has also three record fields, namelly,
  /// the info, head, and foot record.
  /// @author amaury darsch

  class Sheet : public Persist {
  private:
    /// the sheet name
    String  d_name;
    /// the sheet info
    String  d_info;
    /// the sheet tags
    Strvec  d_tags;
    /// the sheet markers
    Strvec  d_mark;
    /// the sheet signature
    Strvec  d_sign;
    /// the record header
    Record  d_head;
    /// the record footer
    Record  d_foot;
    /// the record body
    Vector  d_body;
    
  public:
    /// create an empty sheet
    Sheet (void);

    /// create a new sheet by name
    /// @param name the sheet name
    Sheet (const String& name);

    /// create a new sheet by name and info
    /// @param name the sheet name
    /// @param info the sheet info
    Sheet (const String& name, const String& info);

    /// copy construct this sheet
    /// @param that the sheet to copy
    Sheet (const Sheet& that);

    /// @return the object name
    String repr (void) const;

    /// @return a clone of this object
    Object* clone (void) const;

    /// @return the format serial id
    t_byte serialid (void) const;

    /// serialize this sheet
    /// @param os the output stream
    void wrstream (Output& os) const;

    /// deserialize this sheet
    /// @param is the input stream
    void rdstream (Input& os);

    /// reset this sheet
    void reset (void);

    /// @return the sheet name
    String getname (void) const;

    /// set the sheet name
    /// @param name the name to set
    void setname (const String& name);

    /// @return the sheet info
    String getinfo (void) const;

    /// set the sheet info
    /// @param info the info to set
    void setinfo (const String& info);

    /// add a sheet tag
    /// @param tag the tag to add
    void addtag (const String& tag);
    
    /// add a vector of literals in the sheet tag decsriptor
    /// @param argv the literal vector to add
    void addtag (const Vector* argv);

    /// @return the tags descriptor length
    long tagslen (void) const;

    /// @return true if the sheet tag exists
    bool istag (const String& tag) const;

    /// get a tag by index
    /// @param index the tag index
    String gettag (const long index) const;

    /// set the tags descriptor by index and literal
    /// @param index the tag index to set
    /// @param lobj  the literal object to set
    void settag (const long index, Literal* lobj);

    /// find a tag index by ag
    /// @param tag the tag to find
    long findtag (const String& tag) const;

    /// add a sheet marker
    /// @param mark  the amrker to add
    void addmark (const String& mark);

    /// add a vector of literals in the sheet markers
    /// @param argv the literal vector to add
    void addmark (const Vector* argv);

    /// @return the sheet marker length
    long marklen (void) const;

    /// @return true if the sheet marker exists
    bool ismark (const String& mark) const;

    /// get a marker by index
    /// @param index the marker index
    String getmark (const long index) const;

    /// set the sheet marker by index and literal
    /// @param index the marker index to set
    /// @param lobj  the literal object to set
    void setmark (const long index, Literal* lobj);

    /// find a marker index by mark
    /// @param mark the mark to find
    long findmark (const String& mark) const;

    /// add a sheet sign
    /// @param sign the sign to add
    void addsign (const String& sign);

    /// add a vector of literals in the sheet signature
    /// @param argv the literal vector to add
    void addsign (const Vector* argv);

    /// @return the sheet signature length
    long signlen (void) const;

    /// @return true if the sheet sign exists
    bool issign (const String& sign) const;

    /// get a sign by index
    /// @param index the sign index
    String getsign (const long index) const;

    /// set the sheet sign by index and literal
    /// @param index the sign index to set
    /// @param lobj  the literal object to set
    void setsign (const long index, Literal* lobj);

    /// find a sign index by sign
    /// @param sign the sign to find
    long findsign (const String& mark) const;

    /// add a new object in the header (cell or literal)
    /// @param object the object to add
    void addhead (Object* object);

    /// add a vector of object in the header descriptor
    /// @param argv the object vector
    void addhead (const Vector* argv);

    /// @return a header cell by index
    Cell* gethead (const long index) const;

    /// @return a header literal by index
    Literal* maphead (const long index) const;

    /// set the record header by index and literal
    /// @param index the info index to set
    /// @param lobj  the literal object to set
    void sethead (const long index, Literal* lobj);

    /// add a new object in the footer (cell or literal)
    /// @param object the object to add
    void addfoot (Object* object);

    /// add a vector of object in the footer descriptor
    /// @param argv the object vector
    void addfoot (const Vector* argv);

    /// @return a footer cell by index
    Cell* getfoot (const long index) const;

    /// @return a footer cell value by index
    Literal* mapfoot (const long index) const;

    /// set the record footer by index and literal
    /// @param index the info index to set
    /// @param lobj  the literal object to set
    void setfoot (const long index, Literal* lobj);

    /// @return the sheet length
    long length (void) const;

    /// @return the number of columns
    long getcols (void) const;

    /// add a new record in this sheet
    /// @param rcd the record to add
    void add (Record* rcd);

    /// @return a record by index
    Record* get (const long index) const;

    /// @return a cell by row and column
    Cell* get (const long row, const long col) const;

    /// @return a cell literal by row and column
    Literal* map (const long row, const long col) const;

    /// set a record in this sheet by index
    /// @param index the record index
    /// @param rcd   the record to set
    void set (const long index, Record* rcd);

    /// set an object by row and column
    /// @param row    the record index
    /// @param col    the cell index in the record
    /// @param object the cell to set
    void set (const long row, const long col , Object* object);

    /// add a vector of objects in this sheet
    /// @param argv the vector to add
    void adddata (const Vector* argv);

    /// remove a record by index
    /// @param index the record index to remove
    void remove (const long index);

    /// import data in the sheet
    /// @param is the input stream
    void import (Input* is);

    /// convert a sheet into a print sheet
    /// @param max the maximum number of rows
    /// @param start the row start index
    /// @param flag  the use string or literal represenntation
    PrintTable* convert (long max, long start, bool flag) const;

    /// sort this record by ascending or descending order and column
    /// @param col  the column used for sorting
    /// @param mode the ascending mode
    void sort (const long col, const bool mode);

    /// link a sheet column into this sheet
    /// @param sheet the reference sheet
    /// @param col   the column index
    void lkcol (const Sheet* sheet, const long col);

    /// check if a row exists by column index and literal
    /// @param col  the column to check
    /// @param lobj the literal to check
    bool isrow (const long col, const Literal& lobj) const;

    /// find a row by column index and literal
    /// @param col  the column to check
    /// @param lobj the literal to check
    long rfind (const long col, const Literal& lobj) const;
    
    /// find a row by column index and literal or throw an exception
    /// @param col  the column to check
    /// @param lobj the literal to check
    long rlookup (const long col, const Literal& lobj) const;

  private:
    // make the assignment operator private
    Sheet& operator = (const Sheet&);

  public:
    /// create a new object in a generic way
    /// @param argv the argument vector
    static Object* mknew (Vector* argv);

    /// @return true if the given quark is defined
    bool isquark (const long quark, const bool hflg) const;

    /// apply this object with a set of arguments and a quark
    /// @param robj  the current runnable
    /// @param nset  the current nameset    
    /// @param quark the quark to apply these arguments
    /// @param argv  the arguments to apply
    Object* apply (Runnable* robj, Nameset* nset, const long quark,
                   Vector* argv);
  };
}

#endif
