#ifndef __CURRENCY_H
#define __CURRENCY_H

#ifndef __REAL_H
#include <real.h>
#endif

#ifndef TRectype
class TRectype;
#endif

enum exchange_type { _exchange_base, _exchange_contro, _exchange_undefined };

class TExchange : public TSortable
{ 
  char _val[4];     // Codice valuta
  real _exchange;   // Cambio personalizzato
  exchange_type _et;// Cambio normale o contro euro
  
protected:  
  void copy(const TExchange& exc);

public:
  virtual int compare(const TSortable& obj) const;
  bool same_value_as(const TExchange& exc) const;
  
  TExchange& operator=(const TExchange& exc) { copy(exc); return *this; }
  TExchange& operator=(const TRectype& rec) { set(rec); return *this; }

  void set(const char* val = NULL, const real& exc = ZERO, exchange_type et = _exchange_undefined);
  void set(const TRectype& rec);
  
  exchange_type get_type() const { return _et; }
  bool is_firm_value() const;
  bool is_euro_value() const;
  
  const char* get_value() const { return _val; }
  const real& get_change(exchange_type& et) const;
  real get_base_change() const;
  real get_contro_change() const;
  int decimals(bool price = false) const;

  TExchange(const char* val = NULL, const real& exc = ZERO, exchange_type et = _exchange_undefined);
  TExchange(const TExchange& exc) { copy(exc); }
  TExchange(const TRectype& rec);
  virtual ~TExchange() { }
};
                      
class TCurrency : public TSortable
{        
  TExchange _chg;   // Cambio
  real _num;        // Valore dell'importo
  bool _price;      // Prezzo unitario o no

protected:
  virtual int compare(const TSortable& s) const;
  void copy(const TCurrency& cur);

public:
  static const TString& get_base_val();
  static const TString& get_firm_val();
  static const TString& get_euro_val();
  static int get_base_dec(bool price = false);
  static int get_firm_dec(bool price = false);
  static int get_euro_dec(bool price = false);
  static const real& get_firm_change(exchange_type& ce);
  static const real& get_euro_change();
  
  // serve per la personalizzazione cgp4, per stampare il bilancio in euro
  static void force_firm_val(const char* val);
  static void force_cache_update();  // Azzeramento cache
  
  void set_price(bool p) { _price = p; }
  bool is_price() const { return _price; }

  void force_value(const char* newval, const real& newchange = ZERO, exchange_type et = _exchange_undefined);
  void change_value(const TExchange& exc);
  void change_value(const char* newval, const real& newchange = ZERO, exchange_type et = _exchange_undefined);
  void change_to_base_val() { change_value(get_base_val()); }
  void change_to_firm_val() { change_value(get_firm_val()); }
  void change_to_euro_val() { change_value(get_euro_val()); }
  
  const char* get_value() const { return _chg.get_value(); }
  bool is_base_value() const;
  bool is_firm_value() const;
  bool same_value_as(const TCurrency& cur) const { return _chg.same_value_as(cur._chg); }

  void set_num(const real& num) { _num = num; _num.round(decimals()); }
  const real& get_num() const { return _num; }
  
  void set_exchange(const TExchange& exc) { _chg = exc; }
  const TExchange& get_exchange() const { return _chg; }

  const real& get_change(exchange_type& et) const { return _chg.get_change(et); }
  real get_base_change() const { return _chg.get_base_change(); }
  real get_contro_change() const { return _chg.get_contro_change(); }

  const TCurrency& operator=(const TCurrency& cur) { copy(cur); return *this; }

  TCurrency& operator += (const TCurrency& num);
  TCurrency  operator -  (const TCurrency& num) const;
  TCurrency& operator -= (const TCurrency& num);
  TCurrency  operator +  (const TCurrency& num) const;
  TCurrency& operator *= (const real& num);
  TCurrency  operator *  (const real& num) const;
  TCurrency& operator /= (const real& num);
  TCurrency  operator /  (const real& num) const;
  TCurrency  operator -  () const { TCurrency c(*this); c._num = -c._num; return c; }
  bool is_zero() const { return _num.is_zero(); }
  int sign() const { return _num.sign(); }
  TCurrency abs() const;

  const char* string(bool dotted = false) const;
  void read(const TRectype& rec, const char* field, const char *val = NULL, const char *exchange = NULL, const char* et = NULL);
  void write(TRectype& rec, const char* field, const char *val = NULL, const char *exchange = NULL, const char* et = NULL, bool forceval = false) const;
  int decimals() const;

  TCurrency(bool price = false) : _price(price) { }
  TCurrency(const TCurrency& cur) { copy(cur); }
  TCurrency(const real& num, const char* val = "", const real& exchg = ZERO, exchange_type et = _exchange_undefined, bool price = false);
  TCurrency(const real& num, const TExchange& chg, bool price = false);
  virtual ~TCurrency() { }
};

class TPrice : public TCurrency
{
public:
  const TPrice& operator=(const TPrice& cur) { copy(cur); return *this; }

  TPrice() : TCurrency(TRUE) { }
  TPrice(const TPrice& price) { copy(price); }
  TPrice(const real& num, const char* val = "", const real& exc = ZERO, exchange_type ext = _exchange_undefined) 
        : TCurrency(num, val, exc, ext, TRUE) { }
  virtual ~TPrice() { }
};                                               

inline bool is_firm_value(const char * value) { return value == NULL || *value == '\0' || TCurrency::get_firm_val() == value;}
inline bool is_true_value(const char * value) { return !is_firm_value(value);}
inline bool is_euro_value(const char * value) { return (value && *value) ? TCurrency::get_euro_val() == value : TCurrency::get_euro_val() == TCurrency::get_firm_val();}
bool same_values(const char * valuea, const char * valueb);

// Funzione di levello super-basso: usate solo se sapete bene cosa state facendo
real change_currency(const real& num, 
                     const char* fromval, const real& fromchg, exchange_type fromeuro,
                     const char* toval, const real& tochg, exchange_type toeuro = _exchange_undefined,
                     int price_round = 0);

#endif