/* 
 * © 2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifndef _DIFF_CHANGE_H
#define _DIFF_CHANGE_H

#include <iostream>
#include "grtpp.h"


namespace grt
{

// 
enum ChangeType
{
  SimpleValue,
  ValueAdded,
  ValueRemoved,

  ObjectModified,
  ObjectAttrModified,

  ListModified,

  ListItemAdded,
  ListItemModified,
  //ListStableItemModified, // to be discussed
  ListItemRemoved,
  ListItemOrderChanged,

  DictModified,

  DictItemAdded,
  DictItemModified,
  DictItemRemoved,
};

class DiffChange;

typedef std::vector<DiffChange*> ChangeList;

struct MYSQLGRT_PUBLIC ChangeSet
{
  typedef ChangeList::const_iterator const_iterator;
  typedef ChangeList::const_reverse_iterator const_reverse_iterator;

  ChangeList changes;

  ~ChangeSet();

  inline const_iterator begin() const { return changes.begin(); }
  inline const_iterator end() const { return changes.end(); }

  inline const_reverse_iterator rbegin() const { return changes.rbegin(); }
  inline const_reverse_iterator rend() const { return changes.rend(); }

  inline void append(DiffChange *change) { if(change) changes.push_back(change); }

  inline bool empty() const { return changes.empty(); }
};



// Base class for any specific change in grt instances
class MYSQLGRT_PUBLIC DiffChange
{
protected:
  DiffChange* _parent;

  DiffChange(ChangeType atype) : _parent(NULL), type(atype), _disabled(false) {}

  ChangeType type;
  bool _disabled;
public:
  virtual ~DiffChange() { }

  void set_parent(DiffChange* parent) {_parent = parent;}
  DiffChange* parent() {return _parent;}

  bool is_disabled() const {return _disabled;}
  void set_disabled(bool disabled) {_disabled= disabled;}

  virtual const ChangeSet* subchanges() const {return NULL;}
  
  // apply change to value
  virtual void apply(ValueRef& v)= 0;

  // dumps to stdout text for of change tree
  virtual void dump_log(int level) const
  {
    std::cout << std::string(level, ' ');
    std::cout << get_type_name() << std::endl;
  }
  
  ChangeType get_change_type() const;

  // string representation of ChangeType
  std::string get_type_name() const
  {
#define CASE(v) case v: return #v;
    switch(type)
    {
    CASE(SimpleValue);
    CASE(ValueAdded);
    CASE(ValueRemoved);
    CASE(ObjectModified);
    CASE(ObjectAttrModified);
    CASE(ListModified);
    CASE(ListItemAdded);
    CASE(ListItemModified);
    CASE(ListItemRemoved);
    CASE(ListItemOrderChanged);
    CASE(DictModified);
    CASE(DictItemAdded);
    CASE(DictItemModified);
    CASE(DictItemRemoved);
    }
    return "unknown";
  }
};


//////////////////////////////////////////////////////////////
// @class MultiChange 
//////////////////////////////////////////////////////////////
//
// @brief Contains set of nested changes
//
//////////////////////////////////////////////////////////////
class MYSQLGRT_PUBLIC MultiChange : public DiffChange
{
protected:
  ChangeSet _changes;
public:
  MultiChange(ChangeType atype, ChangeSet& changes) : DiffChange(atype), _changes(changes) 
  {
    for (ChangeSet::const_iterator iter= _changes.begin(); iter != _changes.end(); ++iter)
    {
      (*iter)->set_parent(this);
    }
  }
  virtual ~MultiChange() { /*TODO release changes*/ }

  virtual const ChangeSet* subchanges() const {return &_changes;}

  void apply(ValueRef& v)
  {   
    if (is_disabled())
      return;

    for (ChangeSet::const_iterator iter= _changes.begin(); iter != _changes.end(); ++iter)
    {
      (*iter)->apply(v);
    }
  }

  void dump_log(int level) const
  {
    std::cout << std::string(level, ' ');
    std::cout << get_type_name() << std::endl;
    for (ChangeSet::const_iterator iter= _changes.begin(); iter != _changes.end(); ++iter)
    {
      (*iter)->dump_log(level+1);
    }
  }

};

}
#endif
