// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef tools_sg_f2plot
#define tools_sg_f2plot

// Connexion tools/func to sg/plotter.

#include "plottables"

//#include "../func"

#include "../words"
#include "../S_STRING"
#include "../forit"

#ifdef TOOLS_MEM
#include "../mem"
#endif

namespace tools {
namespace sg {

template <class T>
class f1d2plot : public virtual func1D {
public:
  TOOLS_T_SCLASS(T,tools::sg::f1d2plot)
  virtual void* cast(const std::string& a_class) const {
    if(void* p = cmp_cast<f1d2plot>(this,a_class)) {return p;}
    return func1D::cast(a_class);
  }
public: //plottable
  virtual plottable* copy() const {return new f1d2plot(*this);}
  virtual bool is_valid() const {return true;}
  virtual const std::string& name() const {return m_name;}
  virtual void set_name(const std::string& a_s) {m_name = a_s;}
  virtual const std::string& title() const {return m_title;}
  virtual const std::string& legend() const {return m_legend;}
  virtual void set_legend(const std::string& a_s) {m_legend = a_s;}

  virtual void infos(const std::string& a_opts,std::string& a_sinfos) const {
    a_sinfos.clear();
    std::string f_lf("\n");
    std::vector<std::string> _words;
    words(a_opts," ",false,_words);
    tools_vforcit(std::string,_words,it) {
      if(((*it)=="name") && m_name.size()) {
        if(a_sinfos.size()) a_sinfos += f_lf;
        a_sinfos += "Name\n";
        a_sinfos += m_name;
      }
    }
  }
public: //func1D
  virtual bool value(float a_x,float& a_v) const {
    if(!m_data.in_domain(a_x)) {a_v = 0;return false;}
    a_v = (float)m_data.value(a_x);
    return true;
  }

  virtual unsigned int x_steps() const {return m_data.xdomain_number_of_steps();}
  virtual float x_min() const {return (float)m_data.xdomain_min();}
  virtual float x_max() const {return (float)m_data.xdomain_max();}

public:
  f1d2plot(const T& a_data):m_data(a_data){
#ifdef TOOLS_MEM
    mem::increment(s_class().c_str());
#endif
  }
  virtual ~f1d2plot(){
#ifdef TOOLS_MEM
    mem::decrement(s_class().c_str());
#endif
  }
public:
  f1d2plot(const f1d2plot& a_from)
  :plottable(a_from),func1D(a_from)
  ,m_data(a_from.m_data)
  ,m_name(a_from.m_name)
  ,m_legend(a_from.m_legend)
  ,m_title(a_from.m_title)
  {
#ifdef TOOLS_MEM
    mem::increment(s_class().c_str());
#endif
  }
  f1d2plot& operator=(const f1d2plot& a_from){
    m_name = a_from.m_name;
    m_legend = a_from.m_legend;
    m_title = a_from.m_title;
    return *this;
  }
public:
  void set_title(const std::string& a_s) {m_title = a_s;} //for gopaw.
protected:
  const T& m_data;
  std::string m_name;
  std::string m_legend;
  std::string m_title;
};

template <class T>
class f1d2plot_cp : public f1d2plot<T> {
public:
  TOOLS_T_SCLASS(T,tools::sg::f1d2plot_cp)
  virtual void* cast(const std::string& a_class) const {
    if(void* p = cmp_cast<f1d2plot_cp>(this,a_class)) {return p;}
    return f1d2plot<T>::cast(a_class);
  }
public:
  f1d2plot_cp(const T& a_data)
  :f1d2plot<T>(m_cp)
  ,m_cp(a_data)
  {}
  virtual ~f1d2plot_cp(){}
public:
  f1d2plot_cp(const f1d2plot_cp& a_from)
  :plottable(a_from)
  ,func1D(a_from)
  ,f1d2plot<T>(m_cp)
  ,m_cp(a_from.m_cp)
  {}
  f1d2plot_cp& operator=(const f1d2plot_cp& a_from){
    f1d2plot<T>::operator=(a_from);
    m_cp = a_from.m_cp;
    return *this;
  }
protected:
  T m_cp;
};

template <class T>
class f2d2plot : public virtual func2D {
public:
  TOOLS_T_SCLASS(T,tools::sg::f2d2plot)
  virtual void* cast(const std::string& a_class) const {
    if(void* p = cmp_cast<f2d2plot>(this,a_class)) {return p;}
    return func2D::cast(a_class);
  }
public: //plottable
  virtual plottable* copy() const {return new f2d2plot(*this);}
  virtual bool is_valid() const {return true;}
  virtual const std::string& name() const {return m_name;}
  virtual void set_name(const std::string& a_s) {m_name = a_s;}
  virtual const std::string& title() const {return m_title;}
  virtual const std::string& legend() const {return m_legend;}
  virtual void set_legend(const std::string& a_s) {m_legend = a_s;}

  virtual void infos(const std::string& a_opts,std::string& a_sinfos) const {
    a_sinfos.clear();
    std::string f_lf("\n");
    std::vector<std::string> _words;
    words(a_opts," ",false,_words);
    tools_vforcit(std::string,_words,it) {
      if(((*it)=="name") && m_name.size()) {
        if(a_sinfos.size()) a_sinfos += f_lf;
        a_sinfos += "Name\n";
        a_sinfos += m_name;
      }
    }
  }
public: //func2D
  virtual bool value(float a_x,float a_y,float& a_v) const {
    if(!m_data.in_domain(a_x,a_y)) {a_v = 0;return false;}
    a_v = (float)m_data.value(a_x,a_y);
    return true;
  }

  virtual unsigned int x_steps() const {return m_data.xdomain_number_of_steps();}
  virtual float x_min() const {return m_data.xdomain_min();}
  virtual float x_max() const {return m_data.xdomain_max();}
  virtual unsigned int y_steps() const {return m_data.ydomain_number_of_steps();}
  virtual float y_min() const {return m_data.ydomain_min();}
  virtual float y_max() const {return m_data.ydomain_max();}
public:
  f2d2plot(const T& a_data):m_data(a_data){
#ifdef TOOLS_MEM
    mem::increment(s_class().c_str());
#endif
  }
  virtual ~f2d2plot(){
#ifdef TOOLS_MEM
    mem::decrement(s_class().c_str());
#endif
  }
public:
  f2d2plot(const f2d2plot& a_from)
  :plottable(a_from),func2D(a_from)
  ,m_data(a_from.m_data)
  ,m_name(a_from.m_name)
  ,m_legend(a_from.m_legend)
  ,m_title(a_from.m_title)
  {
#ifdef TOOLS_MEM
    mem::increment(s_class().c_str());
#endif
  }
  f2d2plot& operator=(const f2d2plot& a_from){
    m_name = a_from.m_name;
    m_legend = a_from.m_legend;
    m_title = a_from.m_title;
    return *this;
  }
public:
  void set_title(const std::string& a_s) {m_title = a_s;} //for gopaw.
protected:
  const T& m_data;
  std::string m_name;
  std::string m_legend;
  std::string m_title;
};

template <class T>
class f2d2plot_cp : public f2d2plot<T> {
public:
  TOOLS_T_SCLASS(T,tools::sg::f2d2plot_cp)
  virtual void* cast(const std::string& a_class) const {
    if(void* p = cmp_cast<f2d2plot_cp>(this,a_class)) {return p;}
    return f2d2plot<T>::cast(a_class);
  }
public:
  f2d2plot_cp(const T& a_data)
  :f2d2plot<T>(m_cp)
  ,m_cp(a_data)
  {}
  virtual ~f2d2plot_cp(){}
public:
  f2d2plot_cp(const f2d2plot_cp& a_from)
  :plottable(a_from)
  ,func2D(a_from)
  ,f2d2plot<T>(m_cp)
  ,m_cp(a_from.m_cp)
  {}
  f2d2plot_cp& operator=(const f2d2plot_cp& a_from){
    f2d2plot<T>::operator=(a_from);
    m_cp = a_from.m_cp;
    return *this;
  }
protected:
  T m_cp;
};

}}

#endif
