#pragma once

#ifndef TPARAMCONTAINER_INCLUDED
#define TPARAMCONTAINER_INCLUDED

#include <memory>

#include "tparam.h"
//#include "tfx.h"
#include "tcommon.h"

#undef DVAPI
#undef DVVAR
#ifdef TPARAM_EXPORTS
#define DVAPI DV_EXPORT_API
#define DVVAR DV_EXPORT_VAR
#else
#define DVAPI DV_IMPORT_API
#define DVVAR DV_IMPORT_VAR
#endif

class TIStream;
class TOStream;
class TParamObserver;
class TParam;

class DVAPI TParamVar {
  std::string m_name;
  bool m_isHidden;
  // Flag for an obsolete parameter used for maintaining backward-compatiblity.
  // - The obsolete parameter will call a special function
  // (TFx::onObsoleteParameterLoaded) on loaded which enables to do some special
  // action. (e.g. converting to a new parameter etc.)
  // - The obsolete parameter will not be saved.
  bool m_isObsolete;
  TParamObserver *m_paramObserver;

public:
  TParamVar(std::string name, bool hidden = false, bool obsolete = false)
      : m_name(name)
      , m_isHidden(hidden)
      , m_isObsolete(obsolete)
      , m_paramObserver(0) {}
  virtual ~TParamVar() {}
  virtual TParamVar *clone() const = 0;
  std::string getName() const { return m_name; }
  bool isHidden() const { return m_isHidden; }
  void setIsHidden(bool hidden) { m_isHidden = hidden; }
  bool isObsolete() const { return m_isObsolete; }
  virtual void setParam(TParam *param) = 0;
  virtual TParam *getParam() const     = 0;
  void setParamObserver(TParamObserver *obs);
};

template <class T>
class TParamVarT final : public TParamVar {
  TParamP m_var;

public:
  TParamVarT(std::string name, TParamP var, bool hidden = false,
             bool obsolete = false)
      : TParamVar(name, hidden, obsolete), m_var(var) {}
  TParamVarT(std::string name, T *var, bool hidden = false,
             bool obsolete = false)
      : TParamVar(name, hidden, obsolete), m_var(var) {}
  void setParam(TParam *param) override { m_var = TParamP(param); }

  TParam *getParam() const override { return m_var.getPointer(); }
  TParamVar *clone() const override {
    return new TParamVarT<T>(getName(), m_var, isHidden(), isObsolete());
  }
};

class DVAPI TParamContainer {
  class Imp;
  std::unique_ptr<Imp> m_imp;

public:
  TParamContainer();
  ~TParamContainer();

  void add(TParamVar *var);

  int getParamCount() const;

  bool isParamHidden(int index) const;

  TParam *getParam(int index) const;
  std::string getParamName(int index) const;
  TParam *getParam(std::string name) const;
  TParamVar *getParamVar(std::string name) const;
  const TParamVar *getParamVar(int index) const;

  void unlink();
  void link(const TParamContainer *src);
  void copy(const TParamContainer *src);

  void setParamObserver(TParamObserver *);
  TParamObserver *getParamObserver() const;

private:
  TParamContainer(const TParamContainer &);
  TParamContainer &operator=(const TParamContainer &);
};

#endif
