#ifndef __REPORT_H
#define __REPORT_H

#ifndef __WINDOW_H
#include <window.h>
#endif

#ifndef __IMAGE_H
#include <image.h>
#endif

#ifndef __XML_H
#include <xml.h>
#endif

#ifndef __RECORDSET_H
#include <recset.h>
#endif

#ifndef __ALEX_H
#include <alex.h>
#endif

#ifndef __ISAM_H
class TRectype;
#endif

///////////////////////////////////////////////////////////
// TReport_font
///////////////////////////////////////////////////////////

// Font di base per i calcoli di impaginazione
#ifdef WIN32
#define DEFAULT_FONT_NAME "Courier New" 
#else
#define DEFAULT_FONT_NAME "Courier" 
#endif

class TReport_font : public TSortable
{
  enum { DEFAULT_FONT_SIZE = 10 };

  TString80 _name;
  int _size, _cpi;
  XVT_FONT_STYLE_MASK _style;

  WINDOW _win_mapped;
  XVT_FNTID _fontid;

protected:
  virtual int compare(const TSortable& s) const;
  void copy(const TReport_font& f);

public:
  const TString& name() const { return _name; }
  int size() const { return _size; }
  XVT_FONT_STYLE_MASK style() const { return _style; }
  int cpi() const { return _cpi; } 
  XVT_FNTID get_xvt_font(const TWindow& win) const;
  XVT_FNTID get_preview_font(const TWindow& win, const TSize& res) const;
  void unmap();

  void save(TXmlItem& root) const;
  bool load(const TXmlItem& root);

  void adapt(const TReport_font& oldfont, const TReport_font& newfont);
  void create(const char* name, int size, XVT_FONT_STYLE_MASK style);
  TReport_font& operator=(const TReport_font& f) { copy(f); return *this; }
  TReport_font();
  TReport_font(const TReport_font& f);
  virtual ~TReport_font();
};


class TBook;
class TReport;
class TReport_field;

///////////////////////////////////////////////////////////
// Cache varie
///////////////////////////////////////////////////////////

class TReport_expr;

class TReport_expr_cache : public TCache
{
  TReport* _report;

protected:
  virtual TObject* key2obj(const char* key);

public:
  TReport_expr& operator[](const char* key);

  void set_report(TReport* rep) { _report = rep; }
  TReport_expr_cache() : _report(NULL) { }
};

class TReport_image_cache : public TCache
{
protected:
  virtual TObject* key2obj(const char* key);

public:
  TImage* image(const TString& name);
  TReport_image_cache();
};

///////////////////////////////////////////////////////////
// TReport_script
///////////////////////////////////////////////////////////

class TReport_script : public TObject
{
  TBytecode* _bc;  // Chesire's cat class
  TString _src, _desc;

protected:
  void destroy();
  TString& translate_message(TReport& report) const;
  void copy(const TReport_script& rs);

public:
  virtual bool ok() const { return _src.full(); }
  void set(const char* source);
  const TString& get() const { return _src; }
  void set_description(const char* d) { _desc = d; }

  bool compile(TReport& report);
  bool execute(TReport& report);
  bool execute(TReport_field& rf);

  void save(TXmlItem& root, const char* tag) const;
  bool load(const TXmlItem& root, const char* tag);
  
  TReport_script& operator =(const TReport_script& rs) { copy(rs); return *this; }
  TReport_script();
  TReport_script(const TReport_script& rs) { copy(rs); }
  virtual ~TReport_script();
};

///////////////////////////////////////////////////////////
// TReport_section
///////////////////////////////////////////////////////////

enum TReport_draw_mode { rdm_print, rdm_preview };

struct TReport_size : public TSize
{
  TReport_size(const TSize&) { CHECK(false, "Invalid TReport_size conversion"); }

public:
  void reset() { x = y = 0; } 
  TReport_size& operator+=(const TReport_size& p) { x+=p.x; y+=p.y; return *this; }
  TReport_size() {}
  TReport_size(int sx, int sy) : TSize(sx, sy) { }
  TReport_size(const TReport_size& s) : TSize(s) { }
};

// Coordinate (x,y) espresse in (1/100 di carattere, 1/100 di riga)
struct TReport_pnt : public TPoint
{
private:
  TReport_pnt(const TPoint&) { CHECK(false, "Invalid TReport_pnt conversion"); }

public:
  TReport_pnt& operator+=(const TReport_size& p) { x+=p.x; y+=p.y; return *this; }
  TReport_pnt& operator-=(const TReport_size& p) { x-=p.x; y-=p.y; return *this; }
  TReport_size operator-(const TReport_pnt& p) const { return TReport_size(x-p.x, y-p.y); }
  
  TReport_pnt() {}
  TReport_pnt(int cx, int cy) : TPoint(cx, cy) { }
  TReport_pnt(const TReport_pnt& p) : TPoint(p) {}
};


class TReport_rct : public TReport_pnt
{
  TReport_size _size;

protected:
  void normalize() 
  { 
    if (_size.x < 0) { _size.x=-_size.x; x-=_size.x; }
    if (_size.y < 0) { _size.y=-_size.y; y-=_size.y; }
  }

public:
  const TReport_size& size() const { return _size; }
  int width() const { return _size.x; }
  int height() const { return _size.y; }
  int left() const { return x; }
  int top() const { return y; }
  int right() const { return x+_size.x; }
  int bottom() const { return y+_size.y; }
  void set_height(int h) { _size.y = h; normalize(); }
  void set_width(int w) { _size.x = w; normalize(); }
  void set_empty() { _size.reset(); }
  bool is_empty() const { return _size.x == 0 || _size.y == 0; }
  void set(const TReport_pnt& p, const TReport_size& s) { x=p.x; y=p.y; _size=s; normalize(); }
  void deflate(int dx, int dy) { x+=dx; y+=dy; _size.x-=2*dx; _size.y-=2*dy; normalize(); }
  bool contains(const TReport_pnt& p) const { return p.x >= x && p.x <= right() && p.y > y && p.y <= bottom(); }
  bool contains(const TReport_rct& r) const;
  bool intersects(const TReport_rct& r) const;
  void merge(const TReport_rct& rct);

  TReport_rct() {}
  TReport_rct(int cx, int cy, int w, int h) : TReport_pnt(cx, cy), _size(w, h) { normalize(); }
};


class TReport_section : public TArray
{
  TReport& _report;

  char _type;   // Head,Body,Tail
  int _level;   // 0,1,2,...
  TReport_pnt _pos;   // Posizione assoluta in centesimi, default (0,0)
  TReport_size _size; // Dimensioni in centesimi, default (0,0)
  TString _condition, _groupby;
  bool _page_break, _hidden_if_needed, _can_break, _keep_with_next, _repeat;
  bool _hidden, _deactivated;
  COLOR _fgcolor, _bgcolor, _shcolor;
  int _border, _radius, _shade_angle;
  PAT_STYLE _pattern;
  TReport_script _prescript, _postscript;

  TReport_font *_font, *_print_font;
  TRecordset* _recordset;

protected:
  virtual const char* class_name() const { return "ReportSection"; }
  TReport_section* father_section() const;
  bool print_tools(TBook& book) const;

public:
  virtual int add(TObject* obj);
  virtual int add(const TObject& obj);
  TReport_field& field(int i) { return *(TReport_field*)objptr(i); }
  const TReport_field& field(int i) const { return *(TReport_field*)objptr(i); }
  int find_field_pos(int id);
  TReport_field* find_field(int id);
  TReport& report() { return _report; }
 
  char type() const { return _type; }
  int level() const { return _level; }
  int code(TString& code) const; // code = _type+_level = "B12"

  int width() const { return _size.x; }
  int height() const { return _size.y; }
  void set_width(short w) { _size.x = w; }
  void set_height(short h) { _size.y = h; }
  const TReport_size& size() const { return _size; }
  TReport_size compute_size() const;
  bool compute_rect(TReport_rct& rct) const;
  const TReport_pnt& pos() const { return _pos; }
  void set_pos(const TReport_pnt& p) { _pos = p; }

  void set_fore_color(COLOR c) { _fgcolor = c; }
  COLOR fore_color() const { return _fgcolor; }
  void set_back_color(COLOR c) { _bgcolor = c; }
  COLOR back_color() const { return _bgcolor; }
  void set_shade_color(COLOR c) { _shcolor = c; }
  COLOR shade_color() const { return _shcolor; }

  void set_border(int b) { _border = b; }
  short border() const { return _border; }
  void set_pattern(PAT_STYLE p) { _pattern = p; }
  PAT_STYLE pattern() const { return _pattern; }
  void set_radius(int r) { _radius = r; }
  int radius() const { return _radius; }
  void set_shade_angle(int a) { _shade_angle = a; }
  int shade_angle() const { return _shade_angle; }
  
  bool page_break() const { return _page_break; }
  void force_page_break(bool pb) { _page_break = pb; }
  void set_repeat_on_page(bool r) { _repeat = r; }
  bool repeat_on_page() const { return _repeat; }

  const TString& condition() const { return _condition; }
  void set_condition(const char* str) { _condition = str; }

  const TString& grouped_by() const { return _groupby; }
  void group_by(const char* gb) { _groupby = gb; }
  
  bool hidden_if_needed() const { return _hidden_if_needed; }
  void hide_if_needed(bool h) { _hidden_if_needed = h; }
  bool can_be_broken() const;
  void can_break(bool k) { _can_break = k; }
  bool keep_with_next() const;
  void keep_with_next(bool k) { _keep_with_next = k; }
  bool hidden() const { return _hidden; }
  bool shown() const { return !hidden(); }
  void show(bool on) { _hidden = !on; }
  void hide() { show(false); }
  bool deactivated() const { return _deactivated; }
  bool active() const { return !deactivated(); }
  void activate(bool on = true) { _deactivated = !on; }
  void deactivate() { activate(false); }

  const TString& prescript() const;
  void set_prescript(const char* src);
  const TString& postscript() const;
  void set_postscript(const char* src);

  void update_recordset_parent(); // Internal use only
  virtual bool set_recordset(TRecordset* rs);
  virtual bool set_recordset(const TString& sql);
  TRecordset* recordset() const { return _recordset; }
  bool get_record_field(const char* name, TVariant& var) const;

  bool has_font() const { return _font != NULL; }
  const TReport_font& font() const;
  const TReport_font& print_font() const;

  void set_font(const TReport_font& f);
  void unmap_font();
  void compute_print_font(const TReport_font& oldfont, const TReport_font& newfont);

  bool load_fields();
  void init_dynamic_heights(const TBook& book);
  bool execute_prescript(const TBook& book);
  bool execute_postscript();
  void print(TBook& book) const;
  void print_clipped(TBook& book, long top, long bottom) const;

  void save(TXmlItem& report) const;
  void load(const TXmlItem& sec);

  TReport_section(TReport& r, char t, int l);
  virtual ~TReport_section();
};

struct TReport_array_item;

class TReport_field : public TSortable
{
  TReport_section* _section;
  int _id;
  char _type;       // Text, String, Numeric, Price, Valuta, Date, Boolean, Line, Rectangle, Image
  TReport_rct _rct; // In centesimi
  COLOR _fgcolor, _bgcolor, _txcolor, _shcolor; // Foreground (pen), background (brush), text, shadow
  int _border, _radius, _shadow_offset, _shade_angle;
  PAT_STYLE _pattern;
  char _halign, _valign;
  bool _dynamic_height;
  TBit_array _groups;
  TString _picture, _codval, _link;
  TString _field, _alt_field;
  TVariant _var;
  TReport_script _prescript, _postscript;
  TArray _list; // Elementi di un campo lista

  TReport_font *_font, *_print_font;
  bool _hidden, _deactivated, _hide_zeroes, _selected;

  TReport_rct _draw_rct; // In centesimi
  bool _draw_hidden, _draw_deactivated;

protected:
  virtual const char* class_name() const { return "ReportField"; }
  virtual int compare(const TSortable& s) const;
  void copy(const TReport_field& rf);
  TFieldtypes var_type() const;
  const TString& formatted_text() const;
  void get_currency(TCurrency& cur) const;
  TReport_array_item* get_array_item() const;

public:
  virtual TObject* dup() const { return new TReport_field(*this); } 
  TReport_field& operator=(const TReport_field& rf) { copy(rf); return *this; }

  TReport_section& section() const { return *_section; } 
  void set_section(TReport_section* sec) { _section = sec; } 

  bool has_font() const { return _font != NULL; }
  const TReport_font& font() const;
  const TReport_font& print_font() const;
  void set_font(const TReport_font& f);
  void unmap_font();
  void compute_print_font(const TReport_font& oldfont, const TReport_font& newfont);
  
  const TString& picture() const { return _picture; }
  void set_picture(const char* str) { _picture = str; }

  const TString& field() const { return _field; }
  void set_field(const char* str) { _field = str; }
  
  const TString& alternate_field() const { return _alt_field; }
  void set_alternate_field(const char* str) { _alt_field = str; }

  const TVariant& get() const { return _var; }
  void set(const char* str);
	void set(const TString& str);
  void set(const TVariant& var);

  bool load_field();
  bool execute_prescript();
  bool execute_postscript();

  void get_list(TString_array& list) const;
  void set_list(const TString_array& list);

  int id() const { return _id; }
  void set_id(int id) { _id = id; }
  char type() const { return _type; }
  const char* type_name() const;
  void set_type(char t) { _type = t; }
  void set_pos(long x, long y);
  void set_row(long y) { _rct.y = y; }
  void set_column(long x) { _rct.x = x; }
  void set_size(long w, long h);
  void set_width(long dx) { _rct.set_width(dx); }
  void set_height(long dy) { _rct.set_height(dy); }
  const TReport_rct& get_rect() const { return _rct; }

  void set_dynamic_height(bool dh);
  bool dynamic_height() const;

  bool hidden() const { return _hidden; }
  bool shown() const { return !hidden(); }
  void show(bool on) { _hidden = !on; }
  void hide() { show(false); }
  bool deactivated() const { return _deactivated; }
  bool active() const { return !deactivated(); }
  void activate(bool on) { _deactivated = !on; }
  void deactivate() { activate(false); }
  bool zeroes_hidden() const;
  void hide_zeroes(bool hz) { _hide_zeroes = hz; }

  bool draw_hidden() const { return _draw_hidden; }
  bool draw_deactivated() const { return _draw_deactivated; }
  void set_draw_hidden(bool h) { _draw_hidden  = h; }
  void set_draw_deactivated(bool d) { _draw_deactivated = d; }

  void set_draw_pos(long x, long y);
  void set_draw_size(long x, long y);

  const TReport_rct& compute_draw_rect(const TBook& book);
  const TReport_rct& get_draw_rect() const;

  void set_groups(const TString& groups);
  void add_groups(const TString& groups);
  void del_groups(const TString& groups);
  const TString& groups() const;
  bool in_group(int group) const;

  void set_codval(const char* cod) { _codval = cod; }
  const TString& codval() const { return _codval; }
  void set_link(const char* l) { _link = l; }
  const TString& link() const { return _link; }  // TABLE.FIELD

  void set_fore_color(COLOR c) { _fgcolor = c; }
  COLOR fore_color() const { return _fgcolor; }
  void set_back_color(COLOR c) { _bgcolor = c; }
  COLOR back_color() const { return _bgcolor; }
  void set_text_color(COLOR c) { _txcolor = c; }
  COLOR text_color() const { return _txcolor; }
  void set_shade_color(COLOR c) { _shcolor = c; }
  COLOR shade_color() const { return _shcolor; }
  COLOR link_color() const;
  
  void set_border(int b) { _border = b; }
  short border() const { return _border; }
  void set_pattern(PAT_STYLE p) { _pattern = p; }
  PAT_STYLE pattern() const { return _pattern; }
  void set_radius(int r) { _radius = r; }
  short radius() const { return _radius; }
  void set_shadow_offset(int s);
  int shadow_offset() const { return _shadow_offset; }
  void set_shade_angle(int a) { _shade_angle = a; }
  int shade_angle() const { return _shade_angle; }

  void set_horizontal_alignment(char a) { _halign = a; }
  char horizontal_alignment() const { return _halign; }
  void set_vertical_alignment(char a) { _valign = a; }
  char vertical_alignment() const { return _valign; }
  
  const TString& prescript() const;
  void set_prescript(const char* src);
  const TString& postscript() const;
  void set_postscript(const char* src);

  void select(bool ok = true) { _selected = ok; }
  bool selected() const { return _selected; }
  void offset(const TReport_size& off);

  bool print_tools(TBook& book) const;
  const TReport_rct& print_rect(const TReport_rct& rect, TBook& book) const;
  const TReport_rct& print_rect(TBook& book) const;
  void print(TBook& book) const;

  void save(TXmlItem& root) const;
  bool load(const TXmlItem& root);

  TReport_field* next() const;
  TReport_field* prev() const;

  TReport_field(TReport_section* sec);
  TReport_field(const TReport_field& rf);
  virtual ~TReport_field();
};

class TReport_link : public TSortable
{
  TString _table;
  TAssoc_array _fields;
  RCT _rct;

protected:
  virtual int compare(const TSortable& s) const;

public:
  const TString& table() const { return _table; }
  void set(const char* field, const TString& value);
  const TString& get(const char* field) const;
  void add_rect(const RCT& rct); // Aggiunge un rettangolo al link
  int hit_test(const PNT& p) const;

  TAssoc_array& fields() const { return (TAssoc_array&)_fields; }

  TReport_link(const char* table);
  virtual ~TReport_link() { }
};

// Internal usage only
typedef void (*FLDMSG_FUNC)(TReport_field& rf, void* jolly);

class TReport : public TAlex_virtual_machine
{
  TAssoc_array _sections;
  TFilename _path; 
  TString _description;
  TReport_font _font, _print_font; // Font
  int _cpi, _lpi;                  // Characters and Lines per inch
  TToken_string _include;          // ex: calib
  TString8 _class;                 // ex: ca3300a
  TString _command_line;           // ex: ca3 -2
  TReport_script _prescript, _postscript;
  TString_array _params;
  TRecordset* _recordset;
  TReport_expr_cache _expressions;
  word _rep_page, _book_page, _rep_copy, _rep_copies;
  bool _use_printer_font, _save_last_printer;
  int _orientation;
  TString_array _allegati;
  TReport_field* _curr_field;
  bool _page_split, _page_merge;

protected:
  virtual const char* class_name() const { return "Report"; }
  virtual size_t get_usr_words(TString_array& words) const;
  virtual bool execute_usr_word(unsigned int opcode, TVariant_stack& stack);
  virtual bool get_usr_val(const TString& name, TVariant& var) const;
  virtual bool set_usr_val(const TString& name, const TVariant& var);

  virtual KEY run_form(TMask& msk);
  virtual KEY run_form(const TString& msk);
  
  bool do_message(const TVariant& var, FLDMSG_FUNC msg, void* jolly);

  void do_isam_read_output(const TRectype& rec, TToken_string& out);
  void msg_isam_read(TVariant_stack& stack);
  void msg_table_read(TVariant_stack& stack);
  void msg_firm(TVariant_stack& stack);
  
  void build_section_key(char type, int level, TString& key) const;
  short get_num_attr(const TXmlItem& item, const char* attr, short def = 0) const;
  COLOR get_col_attr(const TXmlItem& item, const char* attr, COLOR defcol = COLOR_BLACK) const;
  void load_sections(const TXmlItem& xml);
  void save_section(const TReport_section& rs, TXmlItem& item) const;

  void update_recordset_parent(); // Internal use only
  bool get_report_field(const TString& name, TVariant& var) const;
  bool get_record_field(const TString& name, TVariant& var) const;

public:
  TReport_section* find_section(char type, int level) const;
  TReport_section& section(char type, int level);
  bool kill_section(char type, int level);
  int find_max_level(char type) const;

  virtual bool on_link(const TReport_link& link);
  virtual bool use_mask() { return true;}

  const TReport_font& font() const { return _font; } 
  const TReport_font& print_font() const;
  void set_use_printer_font(bool on) { _use_printer_font = on; }
  bool use_printer_font() const { return _use_printer_font; }
  void set_save_last_printer(bool on) { _save_last_printer = on; }
  bool save_last_printer() const { return _save_last_printer; }
  void set_orientation(int orion) { _orientation = orion; }
  int orientation() const { return _orientation; }
  const TString& get_class() const { return _class; }
  void set_class(const char* classe) { _class = classe; }
  const TString& command_line() const { return _command_line; }
  void set_command_line(const char* cmd) { _command_line = cmd; }
  
  void set_font(const TReport_font& f) { _font = f; }
  void unmap_font();
  void load_printer_font();

  int user_defined_cpi() const { return _cpi; }
  int cpi() const;
  int lpi() const;
  void set_cpi(int cpi) { _cpi = cpi; }
  void set_lpi(int lpi) { _lpi = lpi; }

  int print_cpi() const;
  int print_lpi() const;

  virtual bool set_recordset(const TString& sql);
  virtual bool set_recordset(TRecordset* sql);
  virtual TRecordset* recordset() const { return _recordset; }
  bool evaluate_atom(const char* atom, TVariant& var);
  bool evaluate(const char* expr, TVariant& var, TFieldtypes force_type);

  const TString& prescript() const;
  void set_prescript(const char* src);
  const TString& postscript() const;
  void set_postscript(const char* src);
  
  virtual bool execute_dot(const TVariant& var);
  virtual bool compile_prescript();
  virtual bool execute_prescript();
  virtual bool execute_postscript();

  void set_params(const TString_array& p) { _params = p; }
  const TString_array& params() const { return _params; }

  void set_libraries(const char* inc) { _include = inc; }
  TToken_string& get_libraries() { return _include; }
  virtual void include_libraries(bool reload = false);

  void set_description(const char* d) { _description = d; }
  const TString& description() const { return _description; }

  void set_allegates(const TString_array& all) { _allegati = all; }
  const TString_array& allegates() const { return _allegati; }

  bool page_split_allowed() const { return _page_split; }
  void allow_page_split(bool ps) { _page_split = ps; }

  bool page_merge_allowed() const { return _page_merge; }
  void allow_page_merge(bool pm) { _page_merge = pm; }

  const TFilename& filename() const { return _path; }
  bool save(const char* fname) const;
  bool load(const char* fname);

  // Used by TReport_printer
  void set_page(word r, word p) { _rep_page = r; _book_page = p; } 
  void set_copy(word c, word s) { _rep_copy = c; _rep_copies = s; } 
	word page() const { return _rep_page; }
	virtual word last_printed_page() const { return 0; }
	
  void set_curr_field(TReport_field* fld) { _curr_field = fld; }
  TReport_field* curr_field() const { return _curr_field; }

  int parse_field(const char* code, char& type, int& level, int& id) const;
  TReport_field* field(const char* code);

  void report2mask(TMask & m) const;
  void mask2report(const TMask & m);

  void destroy();

  bool print_or_preview();
  bool print();
  bool preview();
  bool archive(bool signature);

  TReport();
  virtual ~TReport();
};

#endif