// ---------------------------------------------------------------------------
// - HtmlTable.hpp                                                           -
// - afnix:www library - html table 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-2005 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  AFNIX_HTMLTABLE_HPP
#define  AFNIX_HTMLTABLE_HPP

#ifndef  AFNIX_OUTPUT_HPP
#include "Output.hpp"
#endif

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

#ifndef  AFNIX_HTMLELEMENT_HPP
#include "HtmlElement.hpp"
#endif

namespace afnix {

  /// The HtmlTd class is the table data class. The class is designed to hold
  /// a string value with some html tags. 
  /// @author amaury darsch
  
  class HtmlTd : public HtmlElement {
  private:
    /// the data value
    String d_data;
    
  public:
    /// create an empty data element
    HtmlTd (void);
    
    /// create a data element by value
    /// @žaram lval the literal value
    HtmlTd (const Literal& lval);
    
    /// @return the class name
    String repr (void) const;
    
    /// set the data element
    /// @param lval the literal value to set
    void setdata (const Literal& lval);

    /// @return the data element
    String getdata (void) const;

    /// emit the data element into a buffer
    /// @param buf the recepting buffer
    void emit (Buffer& buf) const;

    /// generate a new table data object
    /// @param argv the argument vector
    static Object* mknew (Vector* argv);
    
    /// apply this table data with a set of arguments and a quark
    /// @param robj  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);
  private:
    // make the copy constructor private
    HtmlTd (const HtmlTd&);
    // make the assignment operator private
    HtmlTd& operator = (const HtmlTd&);
  };
  
  /// The HtmlTr class is the html table row class. The class is built
  /// by inserting table data element. Each table data element is designed
  /// to hold a string value and eventually some tags.
  /// @author amaury darsch
  
  class HtmlTr : public HtmlElement {
  private:
    /// the data element
    Vector d_tbld;
    
  public:
    /// create an empty row
    HtmlTr (void);
    
    /// @return the class name
    String repr (void) const;
    
    /// @return the number of data elements
    long length (void) const;

    /// add a table data to this row
    /// @param td the table data to add
    void adddata (HtmlTd* td);

    /// add a literal data to this row
    /// @param lval the literal data to add
    void adddata (const Literal& lval);

    /// @return a table data object by index
    HtmlTd* getdata (const long idnex) const;

    /// emit the table row into a buffer
    /// @param buf the recepting buffer
    void emit (Buffer& buf) const;

    /// generate a new table row object
    /// @param argv the argument vector
    static Object* mknew (Vector* argv);
    
    /// apply this table row with a set of arguments and a quark
    /// @param robj  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);
    
  private:
    // make the copy constructor private
    HtmlTr (const HtmlTr&);
    // make the assignment operator private
    HtmlTr& operator = (const HtmlTr&);
  };
  
  /// The HtmlTable is a generic class used to produce dynamic html tables.
  /// The class is constructed as an array of rows. Each row accepts one or
  /// several data. The table is structured in three parts. The first part
  /// the table head. The second part or main part is the table body and the
  /// third part is the table foot.
  /// @author amaury darsch

  class HtmlTable : public HtmlElement {    
  protected:
    /// the table head rows
    Vector d_thrs;
    /// the table body rows
    Vector d_tbrs;
    /// the table foot rows
    Vector d_tfrs;
    /// the table caption
    String d_caption;

  public:
    /// create an empty html table
    HtmlTable (void);

    /// create a html table with a print table
    /// @param ptbl the print table to convert
    HtmlTable (const PrintTable& ptbl);

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

    /// set the table caption
    /// @param caption the caption to set
    void setcaption (const String& caption);

    /// @return the table caption
    String getcaption (void) const;

    /// @return the head length
    long gethlen (void) const;

    /// @return the body length
    long getblen (void) const;

    /// @return the foot length
    long getflen (void) const;

    /// add a row to the table head
    /// @param tr the table row to add
    void addhead (HtmlTr* tr);

    /// add a row to the table body
    /// @param tr the table row to add
    void addbody (HtmlTr* tr);

    /// add a row to the table foot
    /// @param tr the table row to add
    void addfoot (HtmlTr* tr);

    /// add a print table in this table
    /// @param ptbl the print table used for update
    void addtbl (const PrintTable& ptbl);

    /// emit the table into a buffer
    /// @param buf the recepting buffer
    void emit (Buffer& buf) const;

    /// generate a new html table object
    /// @param argv the argument vector
    static Object* mknew (Vector* argv);

    /// apply this html table with a set of arguments and a quark
    /// @param robj  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);

  private:
    // make the copy constructor private
    HtmlTable (const HtmlTable&);
    // make the assignment operator private
    HtmlTable& operator = (const HtmlTable&);
  };
}

#endif
